Skip to content

Commit dc5e234

Browse files
authored
Merge pull request #28 from padiazg/fix-lint-and-test
Fix lint and test
2 parents 9c222aa + 1ff72af commit dc5e234

File tree

5 files changed

+72
-77
lines changed

5 files changed

+72
-77
lines changed

build_test.go

Lines changed: 61 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,55 @@ import (
55
"testing"
66
)
77

8+
const (
9+
buildStreamTestWant = `openapi: 3.0.1
10+
info:
11+
title: Test
12+
description: Test object
13+
termsOfService: ""
14+
contact:
15+
email: ""
16+
license:
17+
name: ""
18+
url: ""
19+
version: ""
20+
externalDocs:
21+
description: ""
22+
url: ""
23+
servers: []
24+
tags: []
25+
paths: {}
26+
components:
27+
schemas:
28+
schema_testing:
29+
properties:
30+
EnumProp:
31+
description: short desc
32+
enum:
33+
- enum
34+
- test
35+
- strSlc
36+
type: enum
37+
intProp:
38+
default: 1337
39+
description: short desc
40+
format: int64
41+
type: integer
42+
type: ""
43+
xml:
44+
name: XML entry test
45+
securitySchemes:
46+
ses_scheme_testing:
47+
flows:
48+
implicit:
49+
authorizationUrl: http://petstore.swagger.io/oauth/dialog
50+
scopes:
51+
read:pets: Read Pets
52+
write:pets: Write to Pets
53+
in: not empty
54+
`
55+
)
56+
857
func TestUnitBuild(t *testing.T) {
958
t.Parallel()
1059

@@ -114,6 +163,8 @@ func TestUnitGetPathFromFirstElem(t *testing.T) {
114163
// QUICK CHECK TESTS ARE COMING WITH NEXT RELEASE.
115164

116165
func TestOAS_BuildStream(t *testing.T) {
166+
t.Parallel()
167+
117168
tests := []struct {
118169
name string
119170
oas *OAS
@@ -124,27 +175,19 @@ func TestOAS_BuildStream(t *testing.T) {
124175
name: "success",
125176
oas: &OAS{
126177
OASVersion: "3.0.1",
127-
Info: Info{
128-
Title: "Test",
129-
Description: "Test object",
130-
},
178+
Info: Info{Title: "Test", Description: "Test object"},
131179
Components: Components{
132180
Component{
133181
Schemas: Schemas{Schema{
134182
Name: "schema_testing",
135183
Properties: SchemaProperties{
136184
SchemaProperty{
137-
Name: "EnumProp",
138-
Type: "enum",
139-
Description: "short desc",
140-
Enum: []string{"enum", "test", "strSlc"},
185+
Name: "EnumProp", Type: "enum", Description: "short desc",
186+
Enum: []string{"enum", "test", "strSlc"},
141187
},
142188
SchemaProperty{
143-
Name: "intProp",
144-
Type: "integer",
145-
Format: "int64",
146-
Description: "short desc",
147-
Default: 1337,
189+
Name: "intProp", Type: "integer", Format: "int64",
190+
Description: "short desc", Default: 1337,
148191
},
149192
},
150193
XML: XMLEntry{Name: "XML entry test"},
@@ -156,70 +199,20 @@ func TestOAS_BuildStream(t *testing.T) {
156199
Type: "implicit",
157200
AuthURL: "http://petstore.swagger.io/oauth/dialog",
158201
Scopes: SecurityScopes{
159-
SecurityScope{
160-
Name: "write:pets",
161-
Description: "Write to Pets",
162-
},
163-
SecurityScope{
164-
Name: "read:pets",
165-
Description: "Read Pets",
166-
},
202+
SecurityScope{Name: "write:pets", Description: "Write to Pets"},
203+
SecurityScope{Name: "read:pets", Description: "Read Pets"},
167204
},
168205
}},
169206
}},
170207
},
171208
},
172209
},
173210
wantErr: false,
174-
wantW: `openapi: 3.0.1
175-
info:
176-
title: Test
177-
description: Test object
178-
termsOfService: ""
179-
contact:
180-
email: ""
181-
license:
182-
name: ""
183-
url: ""
184-
version: ""
185-
externalDocs:
186-
description: ""
187-
url: ""
188-
servers: []
189-
tags: []
190-
paths: {}
191-
components:
192-
schemas:
193-
schema_testing:
194-
$ref: ""
195-
properties:
196-
EnumProp:
197-
description: short desc
198-
enum:
199-
- enum
200-
- test
201-
- strSlc
202-
type: enum
203-
intProp:
204-
default: 1337
205-
description: short desc
206-
format: int64
207-
type: integer
208-
type: ""
209-
xml:
210-
name: XML entry test
211-
securitySchemes:
212-
ses_scheme_testing:
213-
flows:
214-
implicit:
215-
authorizationUrl: http://petstore.swagger.io/oauth/dialog
216-
scopes:
217-
read:pets: Read Pets
218-
write:pets: Write to Pets
219-
in: not empty
220-
`,
211+
wantW: buildStreamTestWant,
221212
},
222213
}
214+
215+
t.Parallel()
223216
for _, tt := range tests {
224217
t.Run(tt.name, func(t *testing.T) {
225218
w := &bytes.Buffer{}

examples/stream_output/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func main() {
2424
apiSetExternalDocs(&apiDoc)
2525
apiSetComponents(&apiDoc)
2626

27-
apiDoc.AddRoute(docs.Path{
27+
apiDoc.AddRoute(&docs.Path{
2828
Route: "/users",
2929
HTTPMethod: "POST",
3030
OperationID: "createUser",
@@ -43,7 +43,7 @@ func main() {
4343
},
4444
})
4545

46-
apiDoc.AddRoute(docs.Path{
46+
apiDoc.AddRoute(&docs.Path{
4747
Route: "/users",
4848
HTTPMethod: "GET",
4949
OperationID: "getUser",

routing.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ func (oas *OAS) GetPathByIndex(index int) *Path {
4040
}
4141

4242
// AddRoute is used for add API documentation routes.
43-
func (oas *OAS) AddRoute(path Path) {
44-
oas.Paths = append(oas.Paths, path)
43+
func (oas *OAS) AddRoute(path *Path) {
44+
oas.Paths = append(oas.Paths, *path)
4545
}

routing_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,19 +305,19 @@ func TestOAS_AddRoute(t *testing.T) {
305305
tests := []struct {
306306
name string
307307
oas *OAS
308-
path Path
308+
path *Path
309309
wantPaths Paths
310310
}{
311311
{
312312
name: "success-no-existing-paths",
313313
oas: &OAS{},
314-
path: pathGetUser,
314+
path: &pathGetUser,
315315
wantPaths: Paths{pathGetUser},
316316
},
317317
{
318318
name: "success-existing-paths",
319319
oas: &OAS{Paths: Paths{pathGetUser}},
320-
path: pathCreateUser,
320+
path: &pathCreateUser,
321321
wantPaths: Paths{pathGetUser, pathCreateUser},
322322
},
323323
}

server.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const (
1818
defaultIndexPath = "/index.html"
1919
fwSlashSuffix = "/"
2020
sigContSleeperMilliseconds = 20
21+
readHeaderTimeout = 60 * time.Second // same than nginx
2122
)
2223

2324
// ConfigSwaggerUI represents a structure which will be used to pass required configuration params to
@@ -156,8 +157,9 @@ func (c *ConfigSwaggerUI) initializeDefaultHTTPServer() {
156157
fileServer := http.FileServer(c.initFS)
157158

158159
c.httpServer = &http.Server{
159-
Addr: fmt.Sprintf(":%s", c.Port),
160-
Handler: http.StripPrefix(strings.TrimRight(c.Route, fwSlashSuffix), fileServer),
160+
Addr: fmt.Sprintf(":%s", c.Port),
161+
Handler: http.StripPrefix(strings.TrimRight(c.Route, fwSlashSuffix), fileServer),
162+
ReadHeaderTimeout: readHeaderTimeout,
161163
}
162164
}
163165

0 commit comments

Comments
 (0)