Skip to content
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

Merged
merged 35 commits into from
Jan 30, 2025

Conversation

shreyas-goenka
Copy link
Contributor

@shreyas-goenka shreyas-goenka commented Jan 24, 2025

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.

@shreyas-goenka shreyas-goenka marked this pull request as ready for review January 24, 2025 13:31
@@ -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) {
Copy link
Contributor

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?

Copy link
Contributor Author

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.

acceptance/workspace/jobs/create/server.json Outdated Show resolved Hide resolved
libs/testserver/server.go Outdated Show resolved Hide resolved
@shreyas-goenka shreyas-goenka changed the base branch from main to move-to-testserver January 28, 2025 21:35
acceptance/workspace/jobs/create/server.json Outdated Show resolved Hide resolved
internal/testutil/file.go Outdated Show resolved Hide resolved
github-merge-queue bot pushed a commit that referenced this pull request Jan 29, 2025
## Changes
Just a move, no changes. As recommended here:
#2226 (comment)

## Tests
N/A
Base automatically changed from move-to-testserver to main January 29, 2025 10:47
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)
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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?

Copy link
Contributor Author

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.
Copy link
Contributor

@denik denik left a 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.

@shreyas-goenka shreyas-goenka added this pull request to the merge queue Jan 30, 2025
Merged via the queue into main with commit 3c6eacb Jan 30, 2025
9 checks passed
@shreyas-goenka shreyas-goenka deleted the validate-response-body branch January 30, 2025 10:47
github-merge-queue bot pushed a commit that referenced this pull request Jan 31, 2025
## 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants