-
Notifications
You must be signed in to change notification settings - Fork 72
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
Conversation
jobId := s.nextJobId | ||
s.nextJobId++ | ||
|
||
jobSettings := jobs.JobSettings{} | ||
err := jsonConvert(request, &jobSettings) | ||
if err != nil { | ||
return internalError(err) | ||
return Response{ | ||
StatusCode: 400, |
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.
nit: Use http.StatusBadRequest
instead of an integer directly. Same for other status codes.
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.
I actually prefer 200/400/404/500 over anything else. Do you find those codes hard to read?
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.
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.
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! Looks good to me, just some minor comments.
libs/testserver/fake_workspace.go
Outdated
} else { | ||
return workspace.ObjectInfo{}, http.StatusNotFound | ||
return nil |
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.
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?
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.
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 |
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 do these logs go away?
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.
They don't quite go away -- they are formatted differently, seems like a line was split previously not not anymore.
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.
Seems like it started being logged again once we explicitly returned 404?
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.
This PR does not change how debug logs are formatted, so it's not really the focus there.
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.
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 |
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's happening here?
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.
Debug log capture response and it changed slightly.
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 HTTP/0.0 000
bit looks weird. Where is it coming from?
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.
From script - we have sed command there.
…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.
c2a13c4
to
c9beddc
Compare
@@ -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 |
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.
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.
Changes
Handlers now receive testserver.Request and return any which could be
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