-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added mock draft * Updated handler * Added methods tests * Added tests for mock routes generator * Added mocks parameter * Cleanup packages * Added documentation
- Loading branch information
Evgeny Abramovich
committed
Oct 16, 2022
1 parent
bfe40b0
commit 9c2bfa1
Showing
9 changed files
with
615 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package mock | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/evg4b/uncors/internal/responseprinter" | ||
"github.com/pterm/pterm" | ||
) | ||
|
||
type Handler struct { | ||
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 updateRequest(request *http.Request) { | ||
request.URL.Host = request.Host | ||
|
||
if request.TLS != nil { | ||
request.URL.Scheme = "https" | ||
} else { | ||
request.URL.Scheme = "http" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package mock | ||
|
||
type Response struct { | ||
Code int `yaml:"code"` | ||
Headers map[string]string `yaml:"headers"` | ||
RawContent string `yaml:"raw-content"` //nolint:tagliatelle | ||
} | ||
|
||
type Mock struct { | ||
Path string `yaml:"path"` | ||
Method string `yaml:"method"` | ||
Queries map[string]string `yaml:"queries"` | ||
Headers map[string]string `yaml:"headers"` | ||
Response Response `yaml:"response"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package mock | ||
|
||
import "github.com/gorilla/mux" | ||
|
||
func MakeMockedRoutes(router *mux.Router, mocks []Mock) { | ||
var defaultMocks []Mock | ||
|
||
for _, mock := range mocks { | ||
if len(mock.Queries) > 0 || len(mock.Headers) > 0 || len(mock.Method) > 0 { | ||
route := router.NewRoute() | ||
|
||
setPath(route, mock.Path) | ||
setMethod(route, mock.Method) | ||
setQueries(route, mock.Queries) | ||
setHeaders(route, mock.Headers) | ||
|
||
handler := NewMockHandler(WithMock(mock)) | ||
route.Handler(handler) | ||
} else { | ||
defaultMocks = append(defaultMocks, mock) | ||
} | ||
} | ||
|
||
for _, mock := range defaultMocks { | ||
route := router.NewRoute() | ||
|
||
setPath(route, mock.Path) | ||
|
||
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) | ||
} | ||
} | ||
} |
Oops, something went wrong.