Repository: beatlabs/patron
Pull request #195 (2020-03-31 15:21:18)

Add Route struct getters
<!--
Thanks for taking precious time for making a PR.

Before creating a pull request, please make sure:
- Your PR solves one problem for which a issue exist and a solution has been discussed
- You have read the guide for contributing
  - See https://github.com/beatlabs/patron/blob/master/CONTRIBUTING.md
- You signed all your commits (otherwise we won't be able to merge the PR)
  - See https://github.com/beatlabs/patron/blob/master/CONTRIBUTING.md#sign-your-work
- You added unit tests for the new functionality
- You mention in the PR description which issue it is addressing, e.g. "Resolves #123"
-->

## Which problem is this PR solving?

Closes https://github.com/beatlabs/patron/issues/194

## Short description of the changes

Adds getters to all internals of Route struct


codecov[bot]: # [Codecov](https://codecov.io/gh/beatlabs/patron/pull/195?src=pr&el=h1) Report
> Merging [#195](https://codecov.io/gh/beatlabs/patron/pull/195?src=pr&el=desc) into [master](https://codecov.io/gh/beatlabs/patron/commit/7c27b37aba9dc190efbf0c6a4c56660eea826980&el=desc) will **increase** coverage by `0.02%`.
> The diff coverage is `88.46%`.

[![Impacted file tree graph](https://codecov.io/gh/beatlabs/patron/pull/195/graphs/tree.svg?width=650&height=150&src=pr&token=sxY15rXW1X)](https://codecov.io/gh/beatlabs/patron/pull/195?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #195      +/-   ##
==========================================
+ Coverage   84.40%   84.43%   +0.02%     
==========================================
  Files          51       52       +1     
  Lines        3046     3096      +50     
==========================================
+ Hits         2571     2614      +43     
- Misses        397      401       +4     
- Partials       78       81       +3     
```


| [Impacted Files](https://codecov.io/gh/beatlabs/patron/pull/195?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [client/amqp/amqp.go](https://codecov.io/gh/beatlabs/patron/pull/195/diff?src=pr&el=tree#diff-Y2xpZW50L2FtcXAvYW1xcC5nbw==) | `40.98% <ø> (ø)` | |
| [client/amqp/option.go](https://codecov.io/gh/beatlabs/patron/pull/195/diff?src=pr&el=tree#diff-Y2xpZW50L2FtcXAvb3B0aW9uLmdv) | `66.66% <ø> (ø)` | |
| [client/amqp/user.pb.go](https://codecov.io/gh/beatlabs/patron/pull/195/diff?src=pr&el=tree#diff-Y2xpZW50L2FtcXAvdXNlci5wYi5nbw==) | `26.92% <ø> (ø)` | |
| [client/http/option.go](https://codecov.io/gh/beatlabs/patron/pull/195/diff?src=pr&el=tree#diff-Y2xpZW50L2h0dHAvb3B0aW9uLmdv) | `100.00% <ø> (ø)` | |
| [client/redis/redis.go](https://codecov.io/gh/beatlabs/patron/pull/195/diff?src=pr&el=tree#diff-Y2xpZW50L3JlZGlzL3JlZGlzLmdv) | `44.44% <ø> (ø)` | |
| [client/sns/message.go](https://codecov.io/gh/beatlabs/patron/pull/195/diff?src=pr&el=tree#diff-Y2xpZW50L3Nucy9tZXNzYWdlLmdv) | `97.53% <ø> (ø)` | |
| [client/sns/publisher.go](https://codecov.io/gh/beatlabs/patron/pull/195/diff?src=pr&el=tree#diff-Y2xpZW50L3Nucy9wdWJsaXNoZXIuZ28=) | `93.33% <ø> (ø)` | |
| [client/sqs/message.go](https://codecov.io/gh/beatlabs/patron/pull/195/diff?src=pr&el=tree#diff-Y2xpZW50L3Nxcy9tZXNzYWdlLmdv) | `92.50% <ø> (ø)` | |
| [client/sqs/publisher.go](https://codecov.io/gh/beatlabs/patron/pull/195/diff?src=pr&el=tree#diff-Y2xpZW50L3Nxcy9wdWJsaXNoZXIuZ28=) | `93.33% <ø> (ø)` | |
| [component/async/amqp/amqp.go](https://codecov.io/gh/beatlabs/patron/pull/195/diff?src=pr&el=tree#diff-Y29tcG9uZW50L2FzeW5jL2FtcXAvYW1xcC5nbw==) | `45.58% <ø> (ø)` | |
| ... and [30 more](https://codecov.io/gh/beatlabs/patron/pull/195/diff?src=pr&el=tree-more) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/beatlabs/patron/pull/195?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/beatlabs/patron/pull/195?src=pr&el=footer). Last update [aa707fa...b756350](https://codecov.io/gh/beatlabs/patron/pull/195?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/component/http/route.go b/component/http/route.go
index d84473179..3844ef33c 100644
--- a/component/http/route.go
+++ b/component/http/route.go
@@ -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


Test patch:
diff --git a/component/http/route_test.go b/component/http/route_test.go
index 35712e3f4..c925dcfbf 100644
--- a/component/http/route_test.go
+++ b/component/http/route_test.go
@@ -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 {
@@ -289,3 +293,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
+	}
+}