Skip to content

Commit

Permalink
Added methods tests
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed Oct 15, 2022
1 parent cc8d44d commit 5175729
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 20 deletions.
8 changes: 5 additions & 3 deletions internal/mock/model.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package mock

type Response struct {
Code int
Headers map[string]string
RawContent string
Code int
Headers map[string]string
RawContent string
ContentFile string
}

type Mock struct {
Path string
Method string
Queries map[string]string
Headers map[string]string
Response Response
Expand Down
46 changes: 32 additions & 14 deletions internal/mock/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,41 @@ import "github.com/gorilla/mux"
func MakeMockedRoutes(router *mux.Router, mocks []Mock) {
for _, mock := range mocks {
route := router.NewRoute()
if len(mock.Path) > 0 {
route.Path(mock.Path)
}

if len(mock.Queries) > 0 {
for key, value := range mock.Queries {
route.Queries(key, value)
}
}

if len(mock.Headers) > 0 {
for key, value := range mock.Headers {
route.Headers(key, value)
}
}
setPath(route, mock.Path)
setMethod(route, mock.Method)
setQueries(route, mock.Queries)
setHeaders(route, mock.Headers)

handler := NewMockHandler(WithMock(mock))
route.Handler(handler)
}
}

func setPath(route *mux.Route, path string) {
if len(path) > 0 {
route.Path(path)
}
}

func setMethod(route *mux.Route, methods string) {
if len(methods) > 0 {
route.Methods(methods)
}
}

func setQueries(route *mux.Route, queries map[string]string) {
if len(queries) > 0 {
for key, value := range queries {
route.Queries(key, value)
}
}
}

func setHeaders(route *mux.Route, headers map[string]string) {
if len(headers) > 0 {
for key, value := range headers {
route.Headers(key, value)
}
}
}
79 changes: 76 additions & 3 deletions internal/mock/routes_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package mock_test

import (
"github.com/evg4b/uncors/testing/testutils"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand All @@ -11,6 +13,52 @@ import (
)

func TestMakeMockedRoutes(t *testing.T) {
t.Run("Request method handling", func(t *testing.T) {
t.Run("where mock method is not set allow method", func(t *testing.T) {
router := mux.NewRouter()
expectedBody := `{"name": "Jon Smite"}`
mock.MakeMockedRoutes(router, []mock.Mock{{
Path: "/api",
Response: mock.Response{
Code: http.StatusOK,
RawContent: expectedBody,
},
}})

methods := []string{
http.MethodGet,
http.MethodHead,
http.MethodPost,
http.MethodPut,
http.MethodPatch,
http.MethodDelete,
http.MethodOptions,
http.MethodTrace,
}

for _, method := range methods {
t.Run(method, func(t *testing.T) {
request := httptest.NewRequest(method, "http://localhost/api", nil)
recorder := httptest.NewRecorder()

router.ServeHTTP(recorder, request)

response := recorder.Result()
defer testutils.CheckNoError(t, response.Body.Close())

body, err := io.ReadAll(response.Body)
testutils.CheckNoError(t, err)

assert.Equal(t, http.StatusOK, response.StatusCode)
assert.Equal(t, expectedBody, string(body))
})
}
})

t.Run("where method is set", func(t *testing.T) {

})
})
router := mux.NewRouter()
mock.MakeMockedRoutes(router, []mock.Mock{
{
Expand All @@ -20,6 +68,13 @@ func TestMakeMockedRoutes(t *testing.T) {
RawContent: `{"name": "Jon Smite"}`,
},
},
{
Path: "/api/bad_user",
Response: mock.Response{
Code: http.StatusBadRequest,
RawContent: `{"error": "incorrect data"}`,
},
},
})

tests := []struct {
Expand All @@ -31,9 +86,23 @@ func TestMakeMockedRoutes(t *testing.T) {
statusCode int
}{
{
name: "",
name: "http GET with status code 200",
method: http.MethodGet,
url: "http://localhost/api/user",
expected: `{"name": "Jon Smite"}`,
statusCode: http.StatusOK,
},
{
name: "http GET with status code 400",
method: http.MethodGet,
url: "/api/user",
url: "http://localhost/api/bad_user",
expected: `{"error": "incorrect data"}`,
statusCode: http.StatusBadRequest,
},
{
name: "http POST with status code 200",
method: http.MethodPost,
url: "http://localhost/api/user",
expected: `{"name": "Jon Smite"}`,
statusCode: http.StatusOK,
},
Expand All @@ -49,9 +118,13 @@ func TestMakeMockedRoutes(t *testing.T) {
router.ServeHTTP(recorder, request)

resp := recorder.Result()
defer resp.Body.Close()
defer testutils.CheckNoError(t, resp.Body.Close())

body, err := io.ReadAll(resp.Body)
testutils.CheckNoError(t, err)

assert.Equal(t, testCase.statusCode, resp.StatusCode)
assert.Equal(t, testCase.expected, string(body))
})
}
}

0 comments on commit 5175729

Please sign in to comment.