Skip to content

Commit

Permalink
Merge branch 'master' into server-side-caching
Browse files Browse the repository at this point in the history
  • Loading branch information
drakos74 authored Apr 1, 2020
2 parents cf0bdef + 14cade1 commit 41986a3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
20 changes: 20 additions & 0 deletions component/http/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@ type Route struct {
middlewares []MiddlewareFunc
}

// Path returns route path value.
func (r Route) Path() string {
return r.path
}

// Method returns route method value (GET/POST/...).
func (r Route) Method() string {
return r.method
}

// Middlewares returns route middlewares.
func (r Route) Middlewares() []MiddlewareFunc {
return r.middlewares
}

// Handler returns route handler function.
func (r Route) Handler() http.HandlerFunc {
return r.handler
}

// RouteBuilder for building a route.
type RouteBuilder struct {
method string
Expand Down
41 changes: 41 additions & 0 deletions component/http/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package http

import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"

"github.com/beatlabs/patron/component/http/auth"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type MockAuthenticator struct {
Expand Down Expand Up @@ -299,3 +303,40 @@ func TestRoutesBuilder_Build(t *testing.T) {
})
}
}

func TestRoute_Getters(t *testing.T) {
type testResponse struct {
Value string
}

path := "/foo"
expectedResponse := testResponse{"foo"}
r, err := NewRouteBuilder(path, testingHandlerMock(expectedResponse)).WithTrace().MethodPost().Build()
require.NoError(t, err)

assert.Equal(t, path, r.Path())
assert.Equal(t, http.MethodPost, r.Method())
assert.Len(t, r.Middlewares(), 1)

// the only way to test do we get the same handler that we provided initially, is to run it explicitly,
// since all we have in Route itself is a wrapper function
req, err := http.NewRequest(http.MethodPost, path, nil)
require.NoError(t, err)
wr := httptest.NewRecorder()

r.Handler().ServeHTTP(wr, req)
br, err := ioutil.ReadAll(wr.Body)
require.NoError(t, err)

gotResponse := testResponse{}
err = json.Unmarshal(br, &gotResponse)
require.NoError(t, err)

assert.Equal(t, expectedResponse, gotResponse)
}

func testingHandlerMock(expected interface{}) ProcessorFunc {
return func(_ context.Context, _ *Request) (*Response, error) {
return NewResponse(expected), nil
}
}

0 comments on commit 41986a3

Please sign in to comment.