-
Notifications
You must be signed in to change notification settings - Fork 31
/
swagger_test.go
159 lines (136 loc) · 3.49 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package swagger
import (
"net/http"
"sync"
"testing"
"github.com/gofiber/fiber/v2"
"github.com/swaggo/swag"
)
type mockedSwag struct{}
func (s *mockedSwag) ReadDoc() string {
return `{
"swagger": "2.0",
"info": {
"description": "This is a sample server.",
"title": "Swagger Example API",
"termsOfService": "http://swagger.io/terms/",
"contact": {
"name": "API Support",
"url": "http://www.swagger.io/support",
"email": "support@swagger.io"
},
"license": {
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
},
"version": "1.0"
},
"host": "petstore.swagger.io",
"basePath": "/v2",
"paths": {}
}`
}
var (
registrationOnce sync.Once
)
func Test_Swagger(t *testing.T) {
app := fiber.New()
registrationOnce.Do(func() {
swag.Register(swag.Name, &mockedSwag{})
})
app.Get("/swag/*", HandlerDefault)
tests := []struct {
name string
url string
statusCode int
contentType string
location string
}{
{
name: "Should be returns status 200 with 'text/html' content-type",
url: "/swag/index.html",
statusCode: 200,
contentType: "text/html",
},
{
name: "Should be returns status 200 with 'application/json' content-type",
url: "/swag/doc.json",
statusCode: 200,
contentType: "application/json",
},
{
name: "Should be returns status 200 with 'image/png' content-type",
url: "/swag/favicon-16x16.png",
statusCode: 200,
contentType: "image/png",
},
{
name: "Should return status 301",
url: "/swag/",
statusCode: 301,
location: "/swag/index.html",
},
{
name: "Should return status 404",
url: "/swag/notfound",
statusCode: 404,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, tt.url, nil)
if err != nil {
t.Fatal(err)
}
resp, err := app.Test(req)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != tt.statusCode {
t.Fatalf(`StatusCode: got %v - expected %v`, resp.StatusCode, tt.statusCode)
}
if tt.contentType != "" {
ct := resp.Header.Get("Content-Type")
if ct != tt.contentType {
t.Fatalf(`Content-Type: got %s - expected %s`, ct, tt.contentType)
}
}
if tt.location != "" {
location := resp.Header.Get("Location")
if location != tt.location {
t.Fatalf(`Location: got %s - expected %s`, location, tt.location)
}
}
})
}
}
func Test_Swagger_Proxy_Redirect(t *testing.T) {
app := fiber.New()
registrationOnce.Do(func() {
swag.Register(swag.Name, &mockedSwag{})
})
// Use new handler since the prefix is created only once per handler
app.Get("/swag/*", New())
statusCode := 301
location := "/custom/path/swag/index.html"
t.Run("Should return status 301 with proxy redirect", func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "/swag/", nil)
if err != nil {
t.Fatal(err)
}
req.Header.Set("X-Forwarded-Prefix", "/custom/path/")
resp, err := app.Test(req)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != statusCode {
t.Fatalf(`StatusCode: got %v - expected %v`, resp.StatusCode, statusCode)
}
if location != "" {
responseLocation := resp.Header.Get("Location")
if responseLocation != location {
t.Fatalf(`Location: got %s - expected %s`, responseLocation, location)
}
}
})
}