-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec_test.go
72 lines (57 loc) · 1.66 KB
/
spec_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/getkin/kin-openapi/openapi3"
validator "github.com/rayterrill/httptest-openapi/openapi3"
)
//this test should fail - response body is incorrect (missing id)
func Test_Handler_Contract(t *testing.T) {
doc, err := openapi3.NewLoader().LoadFromFile("openapi.yaml")
if err != nil {
t.Error("unexpected test setup error reading OpenAPI contract", err)
}
t.Run("Validate Spec", func(t *testing.T) {
//validate spec
err = doc.Validate(context.Background())
if err != nil {
t.Error("OpenAPI contract failed to compile", err)
}
})
t.Run("GET PETS FAIL", func(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("content-type", "application/json")
w.WriteHeader(200)
w.Write([]byte(`{"name": "test"}`))
}
rr := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/pets/123", nil)
v := validator.Validator{Openapi: doc}
handler(rr, req)
err = v.Validate(rr, req)
//this fails - intentionally. if the message matches, dont fail the test
if err != nil {
if !strings.Contains(err.Error(), "property \"id\" is missing") {
t.Error(err)
}
}
})
t.Run("GET PETS SUCCESS", func(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("content-type", "application/json")
w.WriteHeader(200)
w.Write([]byte(`{"id": 2, "name": "test"}`))
}
rr := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/pets/123", nil)
v := validator.Validator{Openapi: doc}
handler(rr, req)
err = v.Validate(rr, req)
if err != nil {
t.Error(err)
}
})
}