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

acc: Simplify writing handlers; support headers in responses #2338

Merged
merged 4 commits into from
Feb 12, 2025

Conversation

denik
Copy link
Contributor

@denik denik commented Feb 12, 2025

Changes

Handlers now receive testserver.Request and return any which could be

  • string or []byte (returns it as is but sets content-type to json or plain text depending on content)
  • struct (encodes it as json and sets content-type to json)
  • testserver.Response (full control over status and headers)

Note if testserver.Response is returned from the handler, it's Body attribute can still be an object. In that case, it'll be serialized and appropriate content-type header will be added.

The config is now using the same testserver.Response struct, the same logic applies both configured responses and responses returned from handlers.

As a result, one can set headers both in Golang handlers and in test.toml.

This also fixes a bug with RecordRequest not seeing the body if it was already consumed by the handler.

Tests

  • Existing rests.
  • acceptance/selftest/server is extended to set response header.

@denik denik temporarily deployed to test-trigger-is February 12, 2025 09:42 — with GitHub Actions Inactive
@denik denik marked this pull request as ready for review February 12, 2025 09:45
@denik denik temporarily deployed to test-trigger-is February 12, 2025 10:14 — with GitHub Actions Inactive
jobId := s.nextJobId
s.nextJobId++

jobSettings := jobs.JobSettings{}
err := jsonConvert(request, &jobSettings)
if err != nil {
return internalError(err)
return Response{
StatusCode: 400,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Use http.StatusBadRequest instead of an integer directly. Same for other status codes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually prefer 200/400/404/500 over anything else. Do you find those codes hard to read?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not if I know what the code means. But I had to look up that 400 was BadRequest since I did not remember that off the top of my head.

We can go with the approach you prefer though.

Copy link
Contributor

@shreyas-goenka shreyas-goenka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Looks good to me, just some minor comments.

} else {
return workspace.ObjectInfo{}, http.StatusNotFound
return nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit too implicit that nil corresponds to 404. Normally, in Go, returning nil does not correspond to an error.

Should we explicitly return a 404 here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. I thought it would be generally applicable, but this is the only case that relies on it. Updated to return explicit response here.

@@ -78,12 +78,10 @@
10:07:59 Debug: No script defined for postinit, skipping pid=12345 mutator=initialize mutator=seq mutator=scripts.postinit
10:07:59 Debug: Apply pid=12345 mutator=validate
10:07:59 Debug: GET /api/2.0/workspace/get-status?path=/Workspace/Users/[USERNAME]/.bundle/debug/default/files
< HTTP/1.1 404 Not Found
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do these logs go away?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They don't quite go away -- they are formatted differently, seems like a line was split previously not not anymore.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like it started being logged again once we explicitly returned 404?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR does not change how debug logs are formatted, so it's not really the focus there.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, fair enough. It's interesting though, because a faithful server would allow us to catch a regression whether we stop logging the status code in the future. It's not clear why this is happening when both approaches seem to be the same.

< {} pid=12345 mutator=validate mutator (read-only)=parallel mutator (read-only)=validate:files_to_sync sdk=true
< {} pid=12345 mutator=validate mutator (read-only)=parallel mutator (read-only)=validate:files_to_sync sdk=true
10:07:59 Debug: non-retriable error: Not Found pid=12345 mutator=validate mutator (read-only)=parallel mutator (read-only)=validate:files_to_sync sdk=true
< HTTP/0.0 000 Not Found (Error: Not Found) pid=12345 mutator=validate mutator (read-only)=parallel mutator (read-only)=validate:files_to_sync sdk=true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's happening here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug log capture response and it changed slightly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The HTTP/0.0 000 bit looks weird. Where is it coming from?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From script - we have sed command there.

@denik denik temporarily deployed to test-trigger-is February 12, 2025 11:29 — with GitHub Actions Inactive
@denik denik temporarily deployed to test-trigger-is February 12, 2025 11:50 — with GitHub Actions Inactive
…ody bug

Handlers now receive testserver.Request and return any which could be
- nil (returns 404)
- string / []byte (returns it as is but sets content-type to json or test depending on content)
- object (encodes it as json and sets content-type to json)
- testserver.Response (full control over status, headers)

The config is now using the same testserver.Response struct as handlers, so the same logic applies there.

It is now possible to specify headers in test.toml.

This also fixes a bug with RecordRequest reading the body, not leaving it for the actual handler.
@denik denik force-pushed the denik/acc-request-response branch from c2a13c4 to c9beddc Compare February 12, 2025 11:55
@denik denik temporarily deployed to test-trigger-is February 12, 2025 11:55 — with GitHub Actions Inactive
@denik denik requested a review from shreyas-goenka February 12, 2025 11:56
@@ -78,12 +78,10 @@
10:07:59 Debug: No script defined for postinit, skipping pid=12345 mutator=initialize mutator=seq mutator=scripts.postinit
10:07:59 Debug: Apply pid=12345 mutator=validate
10:07:59 Debug: GET /api/2.0/workspace/get-status?path=/Workspace/Users/[USERNAME]/.bundle/debug/default/files
< HTTP/1.1 404 Not Found
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, fair enough. It's interesting though, because a faithful server would allow us to catch a regression whether we stop logging the status code in the future. It's not clear why this is happening when both approaches seem to be the same.

@denik denik added this pull request to the merge queue Feb 12, 2025
Merged via the queue into main with commit 4034766 Feb 12, 2025
9 checks passed
@denik denik deleted the denik/acc-request-response branch February 12, 2025 13:07
denik added a commit that referenced this pull request Feb 12, 2025
This is needed for b.WorkspaceClient().CurrentWorkspaceID(ctx) which
is used by initialize_urls.go mutator ("bundle summary") (#2316).

It also also needed for to call serverless endpoint (#2348).

Builds on top of #2338
github-merge-queue bot pushed a commit that referenced this pull request Feb 13, 2025
This is needed for b.WorkspaceClient().CurrentWorkspaceID(ctx) which is
used by initialize_urls.go mutator ("bundle summary") #2316

It also also needed for to call serverless detection endpoint #2348

Builds on top of #2338
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.

2 participants