-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix handling of request input that can accept form or json (#213)
- Loading branch information
Showing
6 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/swaggest/usecase" | ||
) | ||
|
||
type formOrJSONInput struct { | ||
Field1 string `json:"field1" formData:"field1" required:"true"` | ||
Field2 int `json:"field2" formData:"field2" required:"true"` | ||
Field3 string `path:"path" required:"true"` | ||
} | ||
|
||
func (formOrJSONInput) ForceJSONRequestBody() {} | ||
|
||
func formOrJSON() usecase.Interactor { | ||
type formOrJSONOutput struct { | ||
F1 string `json:"f1"` | ||
F2 int `json:"f2"` | ||
F3 string `json:"f3"` | ||
} | ||
|
||
u := usecase.NewInteractor(func(ctx context.Context, input formOrJSONInput, output *formOrJSONOutput) error { | ||
output.F1 = input.Field1 | ||
output.F2 = input.Field2 | ||
output.F3 = input.Field3 | ||
|
||
return nil | ||
}) | ||
|
||
u.SetTags("Request") | ||
u.SetDescription("This endpoint can accept both form and json requests with the same input structure.") | ||
|
||
return u | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/bool64/httptestbench" | ||
"github.com/valyala/fasthttp" | ||
) | ||
|
||
func Benchmark_formOrJSON(b *testing.B) { | ||
r := NewRouter() | ||
|
||
srv := httptest.NewServer(r) | ||
defer srv.Close() | ||
|
||
b.Run("form", func(b *testing.B) { | ||
httptestbench.RoundTrip(b, 50, func(i int, req *fasthttp.Request) { | ||
req.Header.SetMethod(http.MethodPost) | ||
req.SetRequestURI(srv.URL + "/form-or-json/abc") | ||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
req.SetBody([]byte(`field1=def&field2=123`)) | ||
}, func(i int, resp *fasthttp.Response) bool { | ||
return resp.StatusCode() == http.StatusOK | ||
}) | ||
}) | ||
|
||
b.Run("json", func(b *testing.B) { | ||
httptestbench.RoundTrip(b, 50, func(i int, req *fasthttp.Request) { | ||
req.Header.SetMethod(http.MethodPost) | ||
req.SetRequestURI(srv.URL + "/form-or-json/abc") | ||
req.Header.Set("Content-Type", "application/json") | ||
req.SetBody([]byte(`{"field1":"string","field2":0}`)) | ||
}, func(i int, resp *fasthttp.Response) bool { | ||
return resp.StatusCode() == http.StatusOK | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters