-
Notifications
You must be signed in to change notification settings - Fork 13
/
swagger_test.go
115 lines (95 loc) · 2.71 KB
/
swagger_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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package vertex_test
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/EverythingMe/vertex"
"github.com/EverythingMe/vertex/middleware"
"github.com/EverythingMe/vertex/swagger"
)
type UserHandler struct {
Id string `schema:"id" required:"true" doc:"The Id Of the user" in:"path"`
Name string `schema:"name" maxlen:"100" required:"true" doc:"The Name Of the user"`
}
func (h UserHandler) Handle(w http.ResponseWriter, r *vertex.Request) (interface{}, error) {
return fmt.Sprintf("Your name is %s and id is %s", h.Name, h.Id), nil
}
func assertEqual(t *testing.T, v1, v2 interface{}, msg ...string) {
if len(msg) == 0 {
msg = append(msg, "Equality Assertion failed: \n%#v !=\n%#v")
}
switch reflect.TypeOf(v1).Kind() {
case reflect.String, reflect.Struct, reflect.Slice, reflect.Map:
if !reflect.DeepEqual(v1, v2) {
t.Errorf(msg[0], v1, v2)
return
}
default:
if v1 != v2 {
t.Errorf(msg[0], v1, v2)
return
}
}
}
func TestSwagger(t *testing.T) {
//t.SkipNow()
a := &vertex.API{
Name: "testung",
Version: "1.0",
Doc: "Our fancy testung API",
Title: "Testung API!",
Middleware: middleware.DefaultMiddleware,
Renderer: vertex.JSONRenderer{},
AllowInsecure: true,
Routes: vertex.Routes{
{
Path: "/user/{id}",
Description: "Get User Info by id or name",
Handler: UserHandler{},
Methods: vertex.GET,
},
{
Path: "/func/handler",
Description: "Test handling by a pure func",
Methods: vertex.POST,
Handler: vertex.HandlerFunc(func(w http.ResponseWriter, r *vertex.Request) (interface{}, error) {
return "WAT WAT", nil
}),
},
},
}
srv := vertex.NewServer(":9947")
srv.AddAPI(a)
s := httptest.NewServer(srv.Handler())
defer s.Close()
u := fmt.Sprintf("http://%s%s", s.Listener.Addr().String(), a.FullPath("/swagger"))
t.Log(u)
res, err := http.Get(u)
if err != nil {
t.Errorf("Could not get swagger data")
}
defer res.Body.Close()
// b, err := ioutil.ReadAll(res.Body)
// fmt.Println(string(b))
var sw swagger.API
dec := json.NewDecoder(res.Body)
if err = dec.Decode(&sw); err != nil {
t.Errorf("Could not decode swagger def: %s", err)
}
swexp := a.ToSwagger(s.Listener.Addr().String())
assertEqual(t, sw.Basepath, swexp.Basepath)
assertEqual(t, sw.Consumes, swexp.Consumes)
assertEqual(t, sw.Host, swexp.Host)
assertEqual(t, sw.Info, swexp.Info)
assertEqual(t, sw.Produces, swexp.Produces)
assertEqual(t, sw.Schemes, swexp.Schemes)
assertEqual(t, sw.SwaggerVersion, swexp.SwaggerVersion)
for k, v := range swexp.Paths {
v2 := sw.Paths[k]
assertEqual(t, v, v2, "Path mismatch \n%#v\n%#v")
}
//fmt.Println(sw)
}