-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update params_test.go, fixed test to account for added Accept header …
…accross all api
- Loading branch information
1 parent
37dba83
commit e458325
Showing
1 changed file
with
30 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,43 @@ | ||
package fuego | ||
package fuego_test | ||
|
||
import ( | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/go-fuego/fuego" | ||
"github.com/go-fuego/fuego/option" | ||
) | ||
|
||
func TestParsePathParams(t *testing.T) { | ||
require.Equal(t, []string(nil), parsePathParams("/")) | ||
require.Equal(t, []string(nil), parsePathParams("/item/")) | ||
require.Equal(t, []string{"user"}, parsePathParams("POST /item/{user}")) | ||
require.Equal(t, []string{"user"}, parsePathParams("/item/{user}")) | ||
require.Equal(t, []string{"user", "id"}, parsePathParams("/item/{user}/{id}")) | ||
require.Equal(t, []string{"$"}, parsePathParams("/item/{$}")) | ||
require.Equal(t, []string{"user"}, parsePathParams("POST alt.com/item/{user}")) | ||
} | ||
func TestParams(t *testing.T) { | ||
t.Run("All options", func(t *testing.T) { | ||
s := fuego.NewServer() | ||
|
||
func BenchmarkParsePathParams(b *testing.B) { | ||
b.Run("empty", func(b *testing.B) { | ||
for range b.N { | ||
parsePathParams("/") | ||
} | ||
}) | ||
route := fuego.Get(s, "/test", func(c fuego.ContextNoBody) (string, error) { | ||
name := c.QueryParam("name") | ||
age := c.QueryParamInt("age") | ||
isok := c.QueryParamBool("is_ok") | ||
|
||
b.Run("several path params", func(b *testing.B) { | ||
for range b.N { | ||
parsePathParams("/item/{user}/{id}") | ||
} | ||
}) | ||
} | ||
return name + strconv.Itoa(age) + strconv.FormatBool(isok), nil | ||
}, | ||
option.Query("name", "Name", fuego.ParamRequired(), fuego.ParamDefault("hey"), fuego.ParamExample("example1", "you")), | ||
option.QueryInt("age", "Age", fuego.ParamNullable(), fuego.ParamDefault(18), fuego.ParamExample("example1", 1)), | ||
option.QueryBool("is_ok", "Is OK?", fuego.ParamDefault(true), fuego.ParamExample("example1", true)), | ||
) | ||
|
||
func FuzzParsePathParams(f *testing.F) { | ||
f.Add("/item/{user}") | ||
f.Add("/item/") | ||
f.Add("/item/{user}/{id}") | ||
f.Add("POST /item/{user}") | ||
f.Add("") | ||
require.NotNil(t, route) | ||
require.NotNil(t, route.Params) | ||
require.Len(t, route.Params, 4) | ||
require.Equal(t, "Name", route.Params["name"].Description) | ||
require.True(t, route.Params["name"].Required) | ||
require.Equal(t, "hey", route.Params["name"].Default) | ||
require.Equal(t, "you", route.Params["name"].Examples["example1"]) | ||
require.Equal(t, "string", route.Params["name"].GoType) | ||
|
||
f.Fuzz(func(t *testing.T, data string) { | ||
parsePathParams(data) | ||
require.Equal(t, "Age", route.Params["age"].Description) | ||
require.True(t, route.Params["age"].Nullable) | ||
require.Equal(t, 18, route.Params["age"].Default) | ||
require.Equal(t, "integer", route.Params["age"].GoType) | ||
}) | ||
} |