Skip to content

Commit

Permalink
Merge pull request #14 from suzuki-shunsuke/feat/change-default-behav…
Browse files Browse the repository at this point in the history
…ior-when-no-route-maches

feat: change default behavior when no route matches
  • Loading branch information
suzuki-shunsuke authored Jul 7, 2019
2 parents 30b99ae + 42f79ee commit 73c50e3
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 62 deletions.
78 changes: 70 additions & 8 deletions fagott/transport.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package fagott

import (
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// RoundTrip implements http.RoundTripper.
Expand All @@ -14,10 +22,11 @@ func (transport *Transport) RoundTrip(req *http.Request) (*http.Response, error)
for _, route := range service.Routes {
b, err := isMatch(req, route.Matcher)
if err != nil {
return &http.Response{
Request: req,
StatusCode: 500,
}, err
if transport.T != nil {
transport.T.Logf("failed to check whether the route matches the request: %v", err)
} else {
fmt.Fprintf(os.Stderr, "failed to check whether the route matches the request: %v\n", err)
}
}
if !b {
continue
Expand All @@ -30,12 +39,65 @@ func (transport *Transport) RoundTrip(req *http.Request) (*http.Response, error)
return createHTTPResponse(req, route.Response)
}
}
// there is no match response
// no route matches the request
if transport.Transport != nil {
return transport.Transport.RoundTrip(req)
}
if http.DefaultClient.Transport != nil && http.DefaultClient.Transport != transport {
return http.DefaultClient.Transport.RoundTrip(req)
return noMatchedRouteRoundTrip(transport.T, req)
}

func noMatchedRouteRoundTrip(t *testing.T, req *http.Request) (*http.Response, error) {
if t == nil {
return &http.Response{
Request: req,
StatusCode: 404,
Body: ioutil.NopCloser(strings.NewReader(`{"message": "no route matches the request"}`)),
}, nil
}
query := req.URL.Query()
qArr := make([]string, len(query))
i := 0
for k, v := range query {
qArr[i] = " " + k + ": " + strings.Join(v, ", ")
i++
}

hArr := make([]string, len(req.Header))
j := 0
for k, v := range req.Header {
hArr[j] = " " + k + ": " + strings.Join(v, ", ")
j++
}
return http.DefaultTransport.RoundTrip(req)

body := ""
if req.Body != nil {
b, err := ioutil.ReadAll(req.Body)
if err != nil {
assert.Nil(t, err, "failed to reqd the request body")
} else {
body = string(b)
}
}

require.Fail(
t, fmt.Sprintf(`no route matches the request.
url: %s
method: %s
query:
%s
header:
%s
body:
%s`,
req.URL.String(),
req.Method,
strings.Join(qArr, "\n"),
strings.Join(hArr, "\n"),
body,
))
return &http.Response{
Request: req,
StatusCode: 404,
Body: ioutil.NopCloser(strings.NewReader(`{"message": "no route matches the request"}`)),
}, nil
}
62 changes: 8 additions & 54 deletions fagott/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ import (
func TestTransport_RoundTrip(t *testing.T) {
token := "XXXXX"
data := []struct {
title string
req *http.Request
transport *Transport
isErr bool
defaultClient *http.Client
defaultTransport http.RoundTripper
exp *http.Response
title string
req *http.Request
transport *Transport
isErr bool
exp *http.Response
}{
{
title: "normal",
Expand Down Expand Up @@ -114,13 +112,6 @@ func TestTransport_RoundTrip(t *testing.T) {
},
},
},
},
isErr: true,
},
{
title: "transport.Transport is called",
req: &http.Request{},
transport: &Transport{
Transport: NewMockRoundTripper(t, gomic.DoNothing).
SetReturnRoundTrip(&http.Response{
StatusCode: 401,
Expand All @@ -131,15 +122,9 @@ func TestTransport_RoundTrip(t *testing.T) {
},
},
{
title: "http.DefaultClient is used",
req: &http.Request{
URL: &url.URL{
Scheme: "http",
Host: "example.com",
},
},
transport: &Transport{},
defaultClient: &http.Client{
title: "transport.Transport is called",
req: &http.Request{},
transport: &Transport{
Transport: NewMockRoundTripper(t, gomic.DoNothing).
SetReturnRoundTrip(&http.Response{
StatusCode: 401,
Expand All @@ -149,41 +134,10 @@ func TestTransport_RoundTrip(t *testing.T) {
StatusCode: 401,
},
},
{
title: "http.DefaultTransport is used",
req: &http.Request{
URL: &url.URL{
Scheme: "http",
Host: "example.com",
},
},
transport: &Transport{},
defaultTransport: NewMockRoundTripper(t, gomic.DoNothing).
SetReturnRoundTrip(&http.Response{
StatusCode: 401,
}, nil),
exp: &http.Response{
StatusCode: 401,
},
},
}

for _, d := range data {
t.Run(d.title, func(t *testing.T) {
if d.defaultClient != nil {
defer func(h *http.Client) {
http.DefaultClient = h
}(http.DefaultClient)
http.DefaultClient = d.defaultClient
}
if d.defaultTransport != nil {
defer func(c, r http.RoundTripper) {
http.DefaultClient.Transport = c
http.DefaultTransport = r
}(http.DefaultClient.Transport, http.DefaultTransport)
http.DefaultClient.Transport = d.transport
http.DefaultTransport = d.defaultTransport
}
resp, err := d.transport.RoundTrip(d.req)
if d.isErr {
require.NotNil(t, err)
Expand Down

0 comments on commit 73c50e3

Please sign in to comment.