Skip to content

Commit

Permalink
Merge pull request #16 from Moranilt/feature/handler-vars-and-query
Browse files Browse the repository at this point in the history
feat: change decoder config for vars and query variables
  • Loading branch information
Moranilt committed Mar 19, 2024
2 parents 5072707 + a30a1f6 commit dc8b466
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
29 changes: 27 additions & 2 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,23 @@ func (h *HandlerMaker[ReqT, RespT]) WithVars() *HandlerMaker[ReqT, RespT] {
return h
}
vars := mux.Vars(h.request)
err := mapstructure.Decode(vars, &h.requestBody)
cfg := &mapstructure.DecoderConfig{
WeaklyTypedInput: true,
Result: &h.requestBody,
}

decoder, err := mapstructure.NewDecoder(cfg)
if err != nil {
h.setError(ErrNotValidBodyFormat, err.Error())
return h
}

err = decoder.Decode(vars)
if err != nil {
h.setError(ErrNotValidBodyFormat, err.Error())
return h
}

return h
}

Expand All @@ -123,11 +135,24 @@ func (h *HandlerMaker[ReqT, RespT]) WithQuery() *HandlerMaker[ReqT, RespT] {
for name, q := range query {
queryVars[name] = q[0]
}
err := mapstructure.Decode(queryVars, &h.requestBody)

cfg := &mapstructure.DecoderConfig{
WeaklyTypedInput: true,
Result: &h.requestBody,
}

decoder, err := mapstructure.NewDecoder(cfg)
if err != nil {
h.setError(ErrNotValidBodyFormat, err.Error())
return h
}

err = decoder.Decode(queryVars)
if err != nil {
h.setError(ErrNotValidBodyFormat, err.Error())
return h
}

return h
}

Expand Down
33 changes: 31 additions & 2 deletions handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type mockRequest struct {
Name string `json:"name,omitempty" mapstructure:"name"`
Phone string `json:"phone,omitempty" mapstructure:"phone"`
Message string `json:"message,omitempty" mapstructure:"message"`
Age int `json:"age,omitempty" mapstructure:"age"`
}

type mockRecipient struct {
Expand Down Expand Up @@ -146,9 +147,10 @@ func TestHandler(t *testing.T) {
})

t.Run("Run with Vars request", func(t *testing.T) {
routePath := "/test-route/{phone}"
routePath := "/test-route/{phone}/{age}"
vars := map[string]string{
"{phone}": "phone_number",
"{age}": "10",
}

testHandler := makeMockedFunction(func(request mockRequest) *mockResponse {
Expand All @@ -158,6 +160,12 @@ func TestHandler(t *testing.T) {
}
}

if request.Age != 10 {
return &mockResponse{
Info: "age is not 10",
}
}

return &mockResponse{
Info: successInfo,
}
Expand Down Expand Up @@ -190,6 +198,12 @@ func TestHandler(t *testing.T) {
}
}

if request.Age != 10 {
return &mockResponse{
Info: "age is not 10",
}
}

return &mockResponse{
Info: successInfo,
}
Expand All @@ -210,15 +224,17 @@ func TestHandler(t *testing.T) {

query := url.Values{}
query.Set("phone", "phone_number")
query.Set("age", "10")

test := newTestHandleController(routePath, testHandler, responseValidator, nil, nil, query, nil)
test.Run(t, logger)
})

t.Run("Run with Json and Vars request", func(t *testing.T) {
routePath := "/test-route/{phone}"
routePath := "/test-route/{phone}/{age}"
vars := map[string]string{
"{phone}": "phone_number",
"{age}": "10",
}

testHandler := makeMockedFunction(func(request mockRequest) *mockResponse {
Expand All @@ -228,6 +244,12 @@ func TestHandler(t *testing.T) {
}
}

if request.Age != 10 {
return &mockResponse{
Info: "age is not 10",
}
}

if request.Name == "" {
return &mockResponse{
Info: errNameRequired,
Expand Down Expand Up @@ -282,6 +304,12 @@ func TestHandler(t *testing.T) {
}
}

if request.Age != 10 {
return &mockResponse{
Info: "age is not 10",
}
}

if request.Name == "" {
return &mockResponse{
Info: errNameRequired,
Expand Down Expand Up @@ -332,6 +360,7 @@ func TestHandler(t *testing.T) {

query := url.Values{}
query.Set("message", "message")
query.Set("age", "10")

test := newTestHandleController(routePath, testHandler, responseValidator, body, vars, query, nil)
test.Run(t, logger)
Expand Down

0 comments on commit dc8b466

Please sign in to comment.