acc: Simplify writing handlers; support headers in responses#2338
acc: Simplify writing handlers; support headers in responses#2338
Conversation
| if err != nil { | ||
| return internalError(err) | ||
| return Response{ | ||
| StatusCode: 400, |
There was a problem hiding this comment.
nit: Use http.StatusBadRequest instead of an integer directly. Same for other status codes.
There was a problem hiding this comment.
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.
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.
shreyas-goenka
left a comment
There was a problem hiding this comment.
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.
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.
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.
| 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.
Why do these logs go away?
There was a problem hiding this comment.
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.
Seems like it started being logged again once we explicitly returned 404?
There was a problem hiding this comment.
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.
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.
What's happening here?
There was a problem hiding this comment.
Debug log capture response and it changed slightly.
There was a problem hiding this comment.
The HTTP/0.0 000 bit looks weird. Where is it coming from?
There was a problem hiding this comment.
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
| 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.
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