-
Notifications
You must be signed in to change notification settings - Fork 73
/
spec_resource_test.go
executable file
·63 lines (49 loc) · 1.72 KB
/
spec_resource_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
package restfulspec
import (
"net/http/httptest"
"testing"
restful "github.com/emicklei/go-restful/v3"
)
// nolint:paralleltest
func TestBuildSwagger(t *testing.T) {
path := "/testPath"
ws1 := new(restful.WebService)
ws1.Path(path)
ws1.Route(ws1.GET("").To(dummy))
ws2 := new(restful.WebService)
ws2.Path(path)
ws2.Route(ws2.DELETE("").To(dummy))
c := Config{}
c.WebServices = []*restful.WebService{ws1, ws2}
s := BuildSwagger(c)
if !(s.Paths.Paths[path].Get != nil && s.Paths.Paths[path].Delete != nil) {
t.Errorf("Swagger spec should have methods for GET and DELETE")
}
}
// nolint:paralleltest
func TestEnablingCORS(t *testing.T) {
ws := NewOpenAPIService(Config{})
wc := restful.NewContainer().Add(ws)
recorder := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/", nil)
origin := "somewhere.over.the.rainbow"
request.Header[restful.HEADER_Origin] = []string{origin}
wc.Dispatch(recorder, request)
responseHeader := recorder.Result().Header.Get(restful.HEADER_AccessControlAllowOrigin)
if responseHeader != origin {
t.Errorf("The CORS header was set to the wrong value, expected: %s but got: %s", origin, responseHeader)
}
}
// nolint:paralleltest
func TestDisablingCORS(t *testing.T) {
ws := NewOpenAPIService(Config{DisableCORS: true})
wc := restful.NewContainer().Add(ws)
recorder := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/", nil)
request.Header[restful.HEADER_Origin] = []string{"somewhere.over.the.rainbow"}
wc.Dispatch(recorder, request)
responseHeader := recorder.Result().Header.Get(restful.HEADER_AccessControlAllowOrigin)
if responseHeader != "" {
t.Errorf("The CORS header was set to %s but it was disabled so should not be set", responseHeader)
}
}