@@ -14,20 +14,20 @@ func TestJSONTypeMismatchErrors(t *testing.T) {
1414 app := fiber .New ()
1515 oapi := New (app )
1616
17- type CreateWorkspaceRequest struct {
18- Description string `json:"description,omitempty" validate:"omitempty,max=255"`
19- IpsWhitelist []string `json:"ips_whitelist ,omitempty" validate:"dive,cidrv4|ip4_addr"`
17+ type CreateRequest struct {
18+ Description string `json:"description,omitempty" validate:"omitempty,max=255"`
19+ Ips []string `json:"ips ,omitempty" validate:"dive,cidrv4|ip4_addr"`
2020 }
2121
22- type CreateWorkspaceResponse struct {
22+ type CreateResponse struct {
2323 Message string `json:"message"`
2424 }
2525
26- Post (oapi , "/workspaces " , func (c * fiber.Ctx , input CreateWorkspaceRequest ) (CreateWorkspaceResponse , TestError ) {
27- return CreateWorkspaceResponse {Message : "Workspace created" }, TestError {}
26+ Post (oapi , "/test " , func (c * fiber.Ctx , input CreateRequest ) (CreateResponse , TestError ) {
27+ return CreateResponse {Message : "created" }, TestError {}
2828 }, OpenAPIOptions {
29- OperationID : "create-workspace " ,
30- Summary : "Create a new workspace " ,
29+ OperationID : "create" ,
30+ Summary : "Create a new entry " ,
3131 })
3232
3333 tests := []struct {
@@ -54,8 +54,8 @@ func TestJSONTypeMismatchErrors(t *testing.T) {
5454 errorContains : "invalid type for field 'description'" ,
5555 },
5656 {
57- name : "Invalid request - ips_whitelist contains number" ,
58- body : `{"ips_whitelist ": [123]}` ,
57+ name : "Invalid request - ips contains number" ,
58+ body : `{"ips ": [123]}` ,
5959 expectedStatus : 400 ,
6060 errorContains : "invalid type" ,
6161 },
@@ -66,28 +66,28 @@ func TestJSONTypeMismatchErrors(t *testing.T) {
6666 },
6767 {
6868 name : "Valid request with valid IPs" ,
69- body : `{"description": "Test", "ips_whitelist ": ["192.168.1.0/24", "10.0.0.1"]}` ,
69+ body : `{"description": "Test", "ips ": ["192.168.1.0/24", "10.0.0.1"]}` ,
7070 expectedStatus : 200 ,
7171 },
7272 }
7373
7474 for _ , tt := range tests {
7575 t .Run (tt .name , func (t * testing.T ) {
76- req := httptest .NewRequest ("POST" , "/workspaces " , strings .NewReader (tt .body ))
76+ req := httptest .NewRequest ("POST" , "/test " , strings .NewReader (tt .body ))
7777 req .Header .Set ("Content-Type" , "application/json" )
7878 resp , err := app .Test (req )
7979 if err != nil {
8080 t .Fatalf ("Expected no error, got %v" , err )
8181 }
8282
83+ body , _ := io .ReadAll (resp .Body )
84+ bodyStr := string (body )
85+
8386 if resp .StatusCode != tt .expectedStatus {
84- body , _ := io .ReadAll (resp .Body )
85- t .Errorf ("Expected status %d, got %d. Body: %s" , tt .expectedStatus , resp .StatusCode , string (body ))
87+ t .Errorf ("Expected status %d, got %d. Body: %s" , tt .expectedStatus , resp .StatusCode , bodyStr )
8688 }
8789
8890 if tt .errorContains != "" {
89- body , _ := io .ReadAll (resp .Body )
90- bodyStr := string (body )
9191 if ! strings .Contains (bodyStr , tt .errorContains ) {
9292 t .Errorf ("Expected error to contain '%s', got %s" , tt .errorContains , bodyStr )
9393 }
0 commit comments