Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to match mock raw query string before complex match #11

Merged
merged 1 commit into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions domain/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ var matchesQuery matcher = func(r *http.Request, mock Mock) bool {
return true
}

if mock.MatchRequest.Query == r.URL.RawQuery {
return true
}

receivedQuery := r.URL.Query()
mockQuery, err := url.ParseQuery(mock.MatchRequest.Query)
if err != nil {
Expand Down
35 changes: 34 additions & 1 deletion domain/mocks_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package domain

import (
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"strings"
"testing"

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

func TestMatcher_Matches(t *testing.T) {
Expand Down Expand Up @@ -39,6 +40,38 @@ func TestMatcher_Matches(t *testing.T) {
expectedResponse: Response{},
expectedOK: false,
},
"with query params - page 1": {
mock: Mock{
MatchRequest: MatchRequest{
Method: "GET",
Path: "/user",
Query: "page=1",
},
Response: Response{
Status: 200,
Body: `{"field": "page1"}`,
},
},
request: httptest.NewRequest(http.MethodGet, "/user?page=1", nil),
expectedResponse: Response{Body: `{"field": "page1"}`, Status: 200},
expectedOK: true,
},
"with query params - page 2": {
mock: Mock{
MatchRequest: MatchRequest{
Method: "GET",
Path: "/user",
Query: "page=2",
},
Response: Response{
Status: 200,
Body: `{"field": "page1"}`,
},
},
request: httptest.NewRequest(http.MethodGet, "/user?page=2", nil),
expectedResponse: Response{Body: `{"field": "page2"}`, Status: 200},
expectedOK: true,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@ github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/steinfletcher/apitest v1.3.13 h1:E0BAXde9dke8jEjK1hqTGvmI20vyyfC+xSdE9nmTc84=
github.com/steinfletcher/apitest v1.3.13/go.mod h1:pCHKMM2TcH1pezw/xbmilaCdK9/dGsoCZBafwaqJ2sY=
github.com/steinfletcher/apitest v1.4.4 h1:ZT/Wa3J615x7mBuarTf92o43AseYfHEFsLhnl1dBkl4=
github.com/steinfletcher/apitest v1.4.4/go.mod h1:yaYc9GDlj4fa0qUUDywqAmrELlNbDrBwd36Zgo5hMZo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/urfave/cli v1.22.1 h1:+mkCCcOFKPnCmVYVcURKps1Xe+3zP90gSYGNfRkjoIY=
Expand Down
66 changes: 63 additions & 3 deletions proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,72 @@ func TestProxy_MocksEnabled_ProxyBackend_Success(t *testing.T) {
}

func TestProxy_MocksEnabled_MockBackend_Success(t *testing.T) {
newApiTest(config(), "http://test-backend", true).
conf := config()
conf.Routes = []domain.Route{
{
Type: "mock",
Mock: &domain.Mock{
MatchRequest: domain.MatchRequest{
Method: "GET",
Path: "^/api/users/.*",
Query: "c=3",
},
Response: domain.Response{
Status: 200,
Body: `{"name": "bob"}`,
},
},
},
{
Type: "mock",
Mock: &domain.Mock{
MatchRequest: domain.MatchRequest{
Method: "GET",
Path: "^/api/users/.*",
Query: "a=1&b=2",
},
Response: domain.Response{
Status: 200,
Body: `{"name": "jon"}`,
},
},
},
}

newApiTest(conf, "http://test-backend", true).
Intercept(func(request *http.Request) {
request.URL.RawQuery = "a=1&b=2"
}).
Get("/api/users/info").
Expect(t).
Status(http.StatusOK).
Body(`{"name": "jon"}`).
End()

newApiTest(conf, "http://test-backend", true).
Intercept(func(request *http.Request) {
request.URL.RawQuery = "b=2&a=1"
}).
Get("/api/users/info").
Expect(t).
Status(http.StatusOK).
Body(`{"name": "jon"}`).
End()

newApiTest(conf, "http://test-backend", true).
Intercept(func(request *http.Request) {
request.URL.RawQuery = "c=3"
}).
Get("/api/users/info").
Query("include", "user_id").
Expect(t).
Status(http.StatusOK).
Body(`{"user_id": "123456"}`).
Body(`{"name": "bob"}`).
End()

newApiTest(conf, "http://test-backend", true).
Get("/api/users/info").
Expect(t).
Status(http.StatusBadGateway).
End()
}

Expand Down