-
Notifications
You must be signed in to change notification settings - Fork 26
Added support for static mocking (mocks defined in JSON files) #145
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
Changes from all commits
ab4eb95
02276ba
419a44d
629ba7e
3e3d4bc
eaca7a8
9980009
f2b2404
3f39305
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,3 +9,9 @@ ui/node_modules | |
| # Jetbrains files | ||
| .idea/ | ||
|
|
||
| # VSCode | ||
| .vscode/ | ||
|
|
||
| # Local run output files | ||
| wiretap-report.json | ||
| __debug* | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,10 @@ | |
| package cmd | ||
|
|
||
| import ( | ||
| "os" | ||
| "reflect" | ||
| "strconv" | ||
|
|
||
| "github.com/pb33f/libopenapi" | ||
| "github.com/pb33f/ranch/bus" | ||
| "github.com/pb33f/ranch/plank/pkg/server" | ||
|
|
@@ -14,9 +18,7 @@ import ( | |
| "github.com/pb33f/wiretap/report" | ||
| "github.com/pb33f/wiretap/shared" | ||
| "github.com/pb33f/wiretap/specs" | ||
| "os" | ||
| "reflect" | ||
| "strconv" | ||
| staticMock "github.com/pb33f/wiretap/static-mock" | ||
| ) | ||
|
|
||
| func runWiretapService(wiretapConfig *shared.WiretapConfiguration, doc libopenapi.Document) (server.PlatformServer, error) { | ||
|
|
@@ -70,6 +72,13 @@ func runWiretapService(wiretapConfig *shared.WiretapConfiguration, doc libopenap | |
| panic(err) | ||
| } | ||
|
|
||
| staticMockService := staticMock.NewStaticMockService(wtService, wiretapConfig.Logger) | ||
| // register Static-Mock Service | ||
| if err = platformServer.RegisterService( | ||
| staticMockService, staticMock.StaticMockServiceChan); err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
|
Comment on lines
+75
to
+81
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the ranch makes it so easy to add new features 🙏
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BTW, this is all code for async APIs, under the covers ranch is wiring up this service against a message bus (that is what the channel is for). You can then talk to this service using pub/sub over a websocket. |
||
| // register spec service | ||
| if err = platformServer.RegisterService( | ||
| specs.NewSpecService(doc), specs.SpecServiceChan); err != nil { | ||
|
|
@@ -107,7 +116,12 @@ func runWiretapService(wiretapConfig *shared.WiretapConfiguration, doc libopenap | |
| bootedMessage(wiretapConfig) | ||
|
|
||
| // boot the http handler | ||
| handleHttpTraffic(wiretapConfig, wtService) | ||
| hht := HandleHttpTraffic{ | ||
| WiretapConfig: wiretapConfig, | ||
| WiretapService: wtService, | ||
| StaticMockService: staticMockService, | ||
| } | ||
| handleHttpTraffic(&hht) | ||
|
|
||
| // boot the monitor | ||
| serveMonitor(wiretapConfig) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // Copyright 2023-2024 Princess Beef Heavy Industries, LLC / Dave Shanley | ||
| // https://pb33f.io | ||
| // SPDX-License-Identifier: AGPL | ||
|
|
||
| package daemon | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
|
|
||
| "github.com/pb33f/ranch/model" | ||
| ) | ||
|
|
||
| func (ws *WiretapService) handleStaticMockResponse(request *model.Request, response *http.Response) { | ||
| // validate response async | ||
| go ws.broadcastResponse(request, response) | ||
|
|
||
| for k, v := range response.Header { | ||
| for _, j := range v { | ||
| request.HttpResponseWriter.Header().Set(k, fmt.Sprint(j)) | ||
| } | ||
| } | ||
|
|
||
| responseCodeToReturn := 200 | ||
| if response.StatusCode != 0 { | ||
| responseCodeToReturn = response.StatusCode | ||
| } | ||
| request.HttpResponseWriter.WriteHeader(responseCodeToReturn) | ||
|
|
||
| if response.Body == nil { | ||
| return | ||
| } | ||
|
|
||
| byteArrayBody, err := io.ReadAll(response.Body) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| _, errs := request.HttpResponseWriter.Write(byteArrayBody) | ||
| if errs != nil { | ||
| panic(errs) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package shared | ||
|
|
||
| func SetCORSHeaders(headers map[string][]string) { | ||
| headers["Access-Control-Allow-Headers"] = []string{"*"} | ||
| headers["Access-Control-Allow-Origin"] = []string{"*"} | ||
| headers["Access-Control-Allow-Methods"] = []string{"OPTIONS,POST,GET,DELETE,PATCH,PUT"} | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should make static mocking path available for path globs. This way, one singular wiretap server can serve static mocks, openapi generated mocks, and also be a proxy ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is still a single server. This doesn't create a new server. We only add a new service that follows a different codepath when static mocking is enabled.
This is already happening. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Im having trouble understanding. According to the code, if staticMockDir is set, then no matter what, every single http request will go to the staticMockHandler. Right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but if there are no matches, it will be forwarded "back" to
wiretapService.HandleHttpRequest. Look at line 195 instaticMock/handle_static_mock_request.go.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or refer to the architecture diagram in this doc - https://docs.google.com/document/d/17pnA8Y5a6nh_0QrEc78FZSX_gJOaoEilB3GudVb-xwk/edit?usp=sharing