-
Notifications
You must be signed in to change notification settings - Fork 5
/
spec_handlers_test.go
70 lines (50 loc) · 1.87 KB
/
spec_handlers_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
package oas
import (
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func TestDynamicSpecHandler(t *testing.T) {
doc := loadDocFile(t, "testdata/petstore_1.yml")
h := NewDynamicSpecHandler(doc)
rr := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/foo", nil)
req.Header.Set("X-Forwarded-Host", "foo.bar.com")
req.Header.Set("X-Forwarded-Proto", "https")
h.ServeHTTP(rr, req)
writtenDoc := loadDocBytes(rr.Body.Bytes())
if writtenDoc.Spec().Host != "foo.bar.com" {
t.Errorf("Expected host to be foo.bar.com but got %q", writtenDoc.Spec().Host)
}
if !reflect.DeepEqual(writtenDoc.Spec().Schemes, []string{"https"}) {
t.Errorf("Expected schemes to be [https] but got %v", writtenDoc.Spec().Schemes)
}
// check that original spec fields remain same
if doc.Spec().Host != "petstore.swagger.io" {
t.Errorf("Expected original spec host hasn't changed but got %v", doc.Spec().Host)
}
if !reflect.DeepEqual(doc.Spec().Schemes, []string{"http"}) {
t.Errorf("Expected original spec schemes hasn't changed but got %v", doc.Spec().Schemes)
}
}
func TestStaticSpecHandler(t *testing.T) {
doc := loadDocFile(t, "testdata/petstore_1.yml")
doc.Spec().Info.Version = "1.2.3"
doc.Spec().Host = "foo.bar.com"
doc.Spec().Schemes = []string{"https"}
h := NewStaticSpecHandler(doc)
rr := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/foo", nil)
h.ServeHTTP(rr, req)
writtenDoc := loadDocBytes(rr.Body.Bytes())
if writtenDoc.Spec().Info.Version != "1.2.3" {
t.Errorf("Expected version to be 1.2.3 but got %q", writtenDoc.Spec().Info.Version)
}
if writtenDoc.Spec().Host != "foo.bar.com" {
t.Errorf("Expected host to be foo.bar.com but got %q", writtenDoc.Spec().Host)
}
if !reflect.DeepEqual(writtenDoc.Spec().Schemes, []string{"https"}) {
t.Errorf("Expected schemes to be [https] but got %v", writtenDoc.Spec().Schemes)
}
}