-
Notifications
You must be signed in to change notification settings - Fork 71
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
Add feature to mock server APIs in acceptance tests #2226
Conversation
acceptance/acceptance_test.go
Outdated
@@ -143,7 +144,7 @@ func testAccept(t *testing.T, InprocessMode bool, singleTest string) int { | |||
for _, dir := range testDirs { | |||
testName := strings.ReplaceAll(dir, "\\", "/") | |||
t.Run(testName, func(t *testing.T) { | |||
if !InprocessMode { | |||
if !InprocessMode && !hasCustomServer(t, dir) { |
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.
Why should having custom server disable parallel runs?
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.
Fixed. Originally I did that since I used t.Setenv
. I modified the approach to simply set override the command environment variable instead.
## Changes Just a move, no changes. As recommended here: #2226 (comment) ## Tests N/A
acceptance/acceptance_test.go
Outdated
require.NotEmpty(t, stub.Pattern) | ||
require.NotEmpty(t, stub.Response.Body) | ||
server.Handle(stub.Pattern, func(req *http.Request) (resp any, err error) { | ||
b := json.RawMessage(stub.Response.Body) |
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.
what does RawMessage do here? why not return stub.Response.Body?
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.
The server.Handle
function has a json.MarshalIndent(resp, "", " ")
call in it.
Returning the response body as a string
or []byte
directly serializes the body as a string / []byte primitive. The typecast here forces json.MarshalIndent
to interpret the string as the raw bytes of a JSON object.
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.
but I also see that we special case string response and in that case we skip marshalling. We have mkdirs endpoint using that. Are those broken?
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.
Ah my bad. I only tried []byte
and did not see we handled the string
case separately in the server handler.
Now we just return the string directly.
This reverts commit 76ca212.
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.
Thanks! Please push rebased branch before merging to ensure it still up to date.
## Changes With this PR, any acceptance tests that define custom server stubs in `test.toml` will automatically record all HTTP requests made and assert on them. Builds on top of #2226 ## Tests Modifying existing acceptance test.
Changes
This PR allows us to define custom server stubs in a
test.toml
file.Note: A followup PR will add functionality to do assertions on the API request itself.
Tests
New acceptance test.