Skip to content

Commit

Permalink
Merge pull request #25 from HUK-COBURG/bugfix/multiple-method-route-e…
Browse files Browse the repository at this point in the history
…rror

Fix routing error
  • Loading branch information
crissi98 authored Nov 5, 2021
2 parents 7aa884e + 89a14eb commit 32e2b40
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
8 changes: 4 additions & 4 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
type Router struct {
baseRouter routers.Router
errMapper *errorMapper
implementations map[*routers.Route]requestHandler
implementations map[routers.Route]requestHandler
}

// NewRouter creates a new Router with the path of a OpenAPI specification file in YAML or JSON format.
Expand All @@ -32,7 +32,7 @@ func NewRouter(swaggerPath string) (*Router, error) {
return &Router{
baseRouter: router,
errMapper: &errorMapper{errorMapping: make(map[reflect.Type]*HTTPError)},
implementations: make(map[*routers.Route]requestHandler),
implementations: make(map[routers.Route]requestHandler),
}, nil
}

Expand All @@ -50,7 +50,7 @@ func (router *Router) ServeHTTP(writer http.ResponseWriter, request *http.Reques
response.write(writer)
return
}
handler, ok := router.implementations[route]
handler, ok := router.implementations[*route]
if ok {
validationInput := &openapi3filter.RequestValidationInput{
Request: request,
Expand Down Expand Up @@ -113,7 +113,7 @@ func (router *Router) AddRequestHandlerWithAuthFunc(method string, path string,
options.AuthenticationFunc = authFunc
}

router.implementations[route] = requestHandler{
router.implementations[*route] = requestHandler{
errMapper: router.errMapper,
handlerFunction: handleFunc,
options: options,
Expand Down
44 changes: 44 additions & 0 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,50 @@ func TestRouter_POSTInvalidData(t *testing.T) {
}
}

func TestRouter_POSTandGETSamePath(t *testing.T) {
//given
router, server := getRouterAndServer()
defer server.Close()
postCalled := false
getCalled := false
sendData := TestData{
Data: "test",
}
dataBytes, _ := json.Marshal(&sendData)
router.AddRequestHandler(http.MethodPost, "/test", func(_ *http.Request, _ map[string]string) (*Response, error) {
postCalled = true
return &Response{
StatusCode: http.StatusNoContent,
}, nil
})
router.AddRequestHandler(http.MethodGet, "/test", func(_ *http.Request, _ map[string]string) (*Response, error) {
getCalled = true
return &Response{
StatusCode: http.StatusOK,
Body: &TestData{
Data: "test",
},
}, nil
})

// when
postResponse, postErr := server.Client().Post(server.URL+"/test", "application/json", bytes.NewReader(dataBytes))
getResponse, getErr := server.Client().Get(server.URL + "/test")

// then
assert.Nil(t, postErr)
if assert.NotNil(t, postResponse) {
assert.True(t, postCalled)
assert.Equal(t, http.StatusNoContent, postResponse.StatusCode)
}

assert.Nil(t, getErr)
if assert.NotNil(t, getResponse) {
assert.True(t, getCalled)
assert.Equal(t, http.StatusOK, getResponse.StatusCode)
}
}

func TestRouter_InvalidPath(t *testing.T) {
// given
_, server := getRouterAndServer()
Expand Down

0 comments on commit 32e2b40

Please sign in to comment.