Skip to content

Commit

Permalink
Updated handler
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed Oct 15, 2022
1 parent 75572e6 commit cc8d44d
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 7 deletions.
45 changes: 41 additions & 4 deletions internal/mock/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
9 changes: 9 additions & 0 deletions internal/mock/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package mock

type HandlerOption = func(*Handler)

func WithMock(mock Mock) HandlerOption {
return func(handler *Handler) {
handler.mock = mock
}
}
3 changes: 2 additions & 1 deletion internal/mock/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func MakeMockedRoutes(router *mux.Router, mocks []Mock) {
}
}

route.Handler(&Handler{mock: mock})
handler := NewMockHandler(WithMock(mock))
route.Handler(handler)
}
}
57 changes: 57 additions & 0 deletions internal/mock/routes_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
14 changes: 12 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
},
},
})
Expand Down

0 comments on commit cc8d44d

Please sign in to comment.