diff --git a/internal/mock/handler.go b/internal/mock/handler.go index 2d3598c2..b6c0077a 100644 --- a/internal/mock/handler.go +++ b/internal/mock/handler.go @@ -3,13 +3,50 @@ package mock import ( "fmt" "net/http" + + "github.com/evg4b/uncors/internal/responseprinter" + "github.com/pterm/pterm" ) type Handler struct { - mock Mock + mock Mock + mockWriter pterm.PrefixPrinter +} + +func NewMockHandler(options ...HandlerOption) *Handler { + handler := &Handler{ + mockWriter: pterm.PrefixPrinter{ + MessageStyle: &pterm.ThemeDefault.InfoMessageStyle, + Prefix: pterm.Prefix{ + Style: &pterm.Style{pterm.FgBlack, pterm.BgLightMagenta}, + Text: " MOCK ", + }, + }, + } + + for _, option := range options { + option(handler) + } + + return handler +} + +func (handler *Handler) ServeHTTP(writer http.ResponseWriter, request *http.Request) { + updateRequest(request) + writer.WriteHeader(handler.mock.Response.Code) + fmt.Fprint(writer, handler.mock.Response.RawContent) + handler.mockWriter.Println(responseprinter.PrintResponse(&http.Response{ + Request: request, + StatusCode: handler.mock.Response.Code, + })) } -func (h *Handler) ServeHTTP(writer http.ResponseWriter, request *http.Request) { - writer.WriteHeader(h.mock.Response.Code) - fmt.Fprint(writer, h.mock.Response.RawContent) +func updateRequest(request *http.Request) { + request.URL.Host = request.Host + + if request.TLS != nil { + request.URL.Scheme = "https" + } else { + request.URL.Scheme = "http" + } } diff --git a/internal/mock/options.go b/internal/mock/options.go new file mode 100644 index 00000000..12aadd9b --- /dev/null +++ b/internal/mock/options.go @@ -0,0 +1,9 @@ +package mock + +type HandlerOption = func(*Handler) + +func WithMock(mock Mock) HandlerOption { + return func(handler *Handler) { + handler.mock = mock + } +} diff --git a/internal/mock/routes.go b/internal/mock/routes.go index 3749e2c1..d7bdef28 100644 --- a/internal/mock/routes.go +++ b/internal/mock/routes.go @@ -21,6 +21,7 @@ func MakeMockedRoutes(router *mux.Router, mocks []Mock) { } } - route.Handler(&Handler{mock: mock}) + handler := NewMockHandler(WithMock(mock)) + route.Handler(handler) } } diff --git a/internal/mock/routes_test.go b/internal/mock/routes_test.go new file mode 100644 index 00000000..c5044d38 --- /dev/null +++ b/internal/mock/routes_test.go @@ -0,0 +1,57 @@ +package mock_test + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/evg4b/uncors/internal/mock" + "github.com/gorilla/mux" + "github.com/stretchr/testify/assert" +) + +func TestMakeMockedRoutes(t *testing.T) { + router := mux.NewRouter() + mock.MakeMockedRoutes(router, []mock.Mock{ + { + Path: "/api/user", + Response: mock.Response{ + Code: http.StatusOK, + RawContent: `{"name": "Jon Smite"}`, + }, + }, + }) + + tests := []struct { + name string + method string + url string + headers map[string]string + expected string + statusCode int + }{ + { + name: "", + method: http.MethodGet, + url: "/api/user", + expected: `{"name": "Jon Smite"}`, + statusCode: http.StatusOK, + }, + } + for _, testCase := range tests { + t.Run(testCase.name, func(t *testing.T) { + request := httptest.NewRequest(testCase.method, testCase.url, nil) + for key, value := range testCase.headers { + request.Header.Add(key, value) + } + recorder := httptest.NewRecorder() + + router.ServeHTTP(recorder, request) + + resp := recorder.Result() + defer resp.Body.Close() + + assert.Equal(t, testCase.statusCode, resp.StatusCode) + }) + } +} diff --git a/main.go b/main.go index e2bea684..6a8c6be2 100644 --- a/main.go +++ b/main.go @@ -5,12 +5,12 @@ import ( "errors" "flag" "fmt" - "github.com/evg4b/uncors/internal/mock" "net/http" "os" "strings" "github.com/evg4b/uncors/internal/infrastructure" + "github.com/evg4b/uncors/internal/mock" "github.com/evg4b/uncors/internal/proxy" "github.com/evg4b/uncors/internal/urlreplacer" "github.com/gorilla/mux" @@ -47,11 +47,21 @@ func main() { router := mux.NewRouter() mock.MakeMockedRoutes(router, []mock.Mock{ + { + Path: "/enterprise", + Queries: map[string]string{ + "demo": "true", + }, + Response: mock.Response{ + RawContent: `{"demo": "lololo"}`, + Code: http.StatusOK, + }, + }, { Path: "/enterprise", Response: mock.Response{ RawContent: `{"demo": "test"}`, - Code: 200, + Code: http.StatusOK, }, }, })