Skip to content

Commit c694628

Browse files
committed
fix: refactor Swagger tests to support multiple frameworks and enhance schema handling
1 parent f8e0614 commit c694628

2 files changed

Lines changed: 152 additions & 121 deletions

File tree

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,62 @@
11
package swaggerspectests
22

3-
// import (
4-
// "net/http"
5-
// "net/http/httptest"
6-
// "testing"
7-
8-
// "github.com/stretchr/testify/assert"
9-
10-
// simplapi "github.com/go-simpl/simplapi"
11-
// _ "github.com/go-simpl/simplapi/pkg/framework/fiberframework"
12-
// )
13-
14-
// func TestOpenAPIJsonURL(t *testing.T) {
15-
// app := simplapi.New()
16-
// fApp := app.GetApp()
17-
18-
// req := httptest.NewRequest(http.MethodGet, "/openapi.json", nil)
19-
20-
// response, err := fApp.TestRequest(req)
21-
// assert.NoError(t, err)
22-
23-
// assert.Equal(t, http.StatusOK, response.StatusCode)
24-
// }
25-
26-
// func TestTryURL(t *testing.T) {
27-
// app := simplapi.New()
28-
// fApp := app.GetApp()
29-
30-
// req := httptest.NewRequest(http.MethodGet, "/try", nil)
31-
32-
// response, err := fApp.TestRequest(req)
33-
// assert.NoError(t, err)
34-
35-
// assert.Equal(t, http.StatusOK, response.StatusCode)
36-
// }
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
10+
simplapi "github.com/go-simpl/simplapi"
11+
_ "github.com/go-simpl/simplapi/pkg/framework/fiberframework"
12+
"github.com/go-simpl/simplapi/tests/utils"
13+
)
14+
15+
func TestOpenAPIJsonURL(t *testing.T) {
16+
frameworks := utils.GetAllFrameworks()
17+
for _, framework := range frameworks {
18+
app := simplapi.New(framework)
19+
app.Sync()
20+
fApp := app.GetApp()
21+
22+
req := httptest.NewRequest(http.MethodGet, "/openapi.json", nil)
23+
24+
response, err := fApp.TestRequest(req)
25+
assert.NoError(t, err)
26+
27+
assert.Equal(t, http.StatusOK, response.StatusCode)
28+
}
29+
}
30+
31+
func TestTryURL(t *testing.T) {
32+
frameworks := utils.GetAllFrameworks()
33+
for _, framework := range frameworks {
34+
app := simplapi.New(framework)
35+
app.Sync()
36+
fApp := app.GetApp()
37+
{
38+
req := httptest.NewRequest(http.MethodGet, "/_try/stoplight", nil)
39+
40+
response, err := fApp.TestRequest(req)
41+
assert.NoError(t, err)
42+
43+
assert.Equal(t, http.StatusOK, response.StatusCode)
44+
}
45+
{
46+
req := httptest.NewRequest(http.MethodGet, "/_try/swagger", nil)
47+
48+
response, err := fApp.TestRequest(req)
49+
assert.NoError(t, err)
50+
51+
assert.Equal(t, http.StatusOK, response.StatusCode)
52+
}
53+
{
54+
req := httptest.NewRequest(http.MethodGet, "/_try/redoc", nil)
55+
56+
response, err := fApp.TestRequest(req)
57+
assert.NoError(t, err)
58+
59+
assert.Equal(t, http.StatusOK, response.StatusCode)
60+
}
61+
}
62+
}
Lines changed: 92 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,94 @@
11
package swaggerspectests
22

3-
// import (
4-
// "mime/multipart"
5-
// "net/http"
6-
// "testing"
7-
8-
// simplapi "github.com/go-simpl/simplapi"
9-
// _ "github.com/go-simpl/simplapi/pkg/framework/fiberframework"
10-
// )
11-
12-
// type JsonInput struct {
13-
// ContentType string `header:"Content-Type" example:"application/json"`
14-
// Body struct {
15-
// Name string `json:"name" example:"John Doe"`
16-
// Age int `json:"age" example:"25"`
17-
// IsAdmin *bool `json:"is_admin" example:"false"`
18-
// Rating float64 `json:"rating" example:"4.5"`
19-
// Tags []string `json:"tags" example:"[\"tag1\", \"tag2\"]"`
20-
// } `body:"json"`
21-
// }
22-
23-
// type FormInput struct {
24-
// Body struct {
25-
// Name string `form:"name" example:"John Doe"`
26-
// Age int `form:"age" example:"25"`
27-
// IsAdmin *bool `form:"is_admin" example:"false"`
28-
// Rating float64 `form:"rating" example:"4.5"`
29-
// } `body:"urlencoded"`
30-
// }
31-
32-
// type MultipartInput struct {
33-
// Body struct {
34-
// Name string `form:"name" example:"John Doe"`
35-
// Age int `form:"age" example:"25"`
36-
// IsAdmin *bool `form:"is_admin" example:"false"`
37-
// Rating float64 `form:"rating" example:"4.5"`
38-
// Picture *multipart.FileHeader `form:"picture" example:"picture.jpg"`
39-
// } `body:"multipart"`
40-
// }
41-
42-
// type Response1 struct {
43-
// Status string `json:"status" example:"OK"`
44-
// Nested []struct {
45-
// Field1 string `json:"field1" example:"value1"`
46-
// Field2 string `json:"field2" example:"value2"`
47-
// Random complex64 `json:"random"`
48-
// } `json:"nested"`
49-
// }
50-
51-
// func (r *Response1) GetStatusCode() int {
52-
// return http.StatusOK
53-
// }
54-
55-
// type Response2 struct {
56-
// Status string `json:"status" example:"NOT_OK"`
57-
// }
58-
59-
// func (r *Response2) GetStatusCode() int {
60-
// return http.StatusOK
61-
// }
62-
63-
// type Response3 struct {
64-
// Status string `json:"status" example:"OK"`
65-
// Nested []struct {
66-
// Field1 string `json:"field1" example:"value1"`
67-
// Field2 string `json:"field2" example:"value2"`
68-
// } `json:"nested"`
69-
// }
70-
71-
// func (r *Response3) GetStatusCode() int {
72-
// return http.StatusOK
73-
// }
74-
75-
// func TestAllPossibleSchemaStuff(t *testing.T) {
76-
// app := simplapi.New()
77-
78-
// app.POST("/json", nil, func(input JsonInput) (*Response1, *Response2, *Response3, error) {
79-
// return nil, nil, nil, nil
80-
// })
81-
82-
// app.POST("/form", nil, func(input FormInput) (*Response1, *Response2, *Response3, error) {
83-
// return nil, nil, nil, nil
84-
// })
85-
86-
// app.POST("/multipart", nil, func(input MultipartInput) (*Response1, *Response2, *Response3, error) {
87-
// return nil, nil, nil, nil
88-
// })
89-
// }
3+
import (
4+
"mime/multipart"
5+
"net/http"
6+
"testing"
7+
8+
simplapi "github.com/go-simpl/simplapi"
9+
"github.com/go-simpl/simplapi/tests/utils"
10+
)
11+
12+
type JsonInput struct {
13+
ContentType string `header:"Content-Type" example:"application/json"`
14+
Body struct {
15+
Name string `json:"name" example:"John Doe"`
16+
Age int `json:"age" example:"25"`
17+
IsAdmin *bool `json:"is_admin" example:"false"`
18+
Rating float64 `json:"rating" example:"4.5"`
19+
Tags []string `json:"tags" example:"[\"tag1\", \"tag2\"]"`
20+
} `body:"json"`
21+
}
22+
23+
type FormInput struct {
24+
Body struct {
25+
Name string `form:"name" example:"John Doe"`
26+
Age int `form:"age" example:"25"`
27+
IsAdmin *bool `form:"is_admin" example:"false"`
28+
Rating float64 `form:"rating" example:"4.5"`
29+
} `body:"urlencoded"`
30+
}
31+
32+
type MultipartInput struct {
33+
Body struct {
34+
Name string `form:"name" example:"John Doe"`
35+
Age int `form:"age" example:"25"`
36+
IsAdmin *bool `form:"is_admin" example:"false"`
37+
Rating float64 `form:"rating" example:"4.5"`
38+
Picture *multipart.FileHeader `form:"picture" example:"picture.jpg"`
39+
} `body:"multipart"`
40+
}
41+
42+
type Response1 struct {
43+
Status string `json:"status" example:"OK"`
44+
Nested []struct {
45+
Field1 string `json:"field1" example:"value1"`
46+
Field2 string `json:"field2" example:"value2"`
47+
Random complex64 `json:"random"`
48+
} `json:"nested"`
49+
}
50+
51+
func (r *Response1) GetStatusCode() int {
52+
return http.StatusOK
53+
}
54+
55+
type Response2 struct {
56+
Status string `json:"status" example:"NOT_OK"`
57+
}
58+
59+
func (r *Response2) GetStatusCode() int {
60+
return http.StatusOK
61+
}
62+
63+
type Response3 struct {
64+
Status string `json:"status" example:"OK"`
65+
Nested []struct {
66+
Field1 string `json:"field1" example:"value1"`
67+
Field2 string `json:"field2" example:"value2"`
68+
} `json:"nested"`
69+
}
70+
71+
func (r *Response3) GetStatusCode() int {
72+
return http.StatusOK
73+
}
74+
75+
func TestAllPossibleSchemaStuff(t *testing.T) {
76+
frameworks := utils.GetAllFrameworks()
77+
for _, framework := range frameworks {
78+
app := simplapi.New(framework)
79+
80+
app.POST("/json", func(input JsonInput) (*Response1, *Response2, *Response3, error) {
81+
return nil, nil, nil, nil
82+
})
83+
84+
app.POST("/form", func(input FormInput) (*Response1, *Response2, *Response3, error) {
85+
return nil, nil, nil, nil
86+
})
87+
88+
app.POST("/multipart", func(input MultipartInput) (*Response1, *Response2, *Response3, error) {
89+
return nil, nil, nil, nil
90+
})
91+
92+
app.Sync()
93+
}
94+
}

0 commit comments

Comments
 (0)