-
Notifications
You must be signed in to change notification settings - Fork 9
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
Feature: Handle binary response body #53
base: master
Are you sure you want to change the base?
Conversation
When retrieving a binary response body, convert it to hexadecimal so we can safely add it to the snapshot file.
I'm wondering if we should rethink the method signature so it can accommodate all response types. Otherwise it'll start to become overloaded as we support other response types. Maybe the method determines the content-type on its own? |
Yeah, totally agree with you. It will be a pain to keep track of all content-types.
You mean, the method will receive an enum? Like: // JSON
abide.AssertHTTPResponse(t, t.Name(), w.Result(), abide.ResponseTypeJSON)
// text
abide.AssertHTTPResponse(t, t.Name(), w.Result(), abide.ResponseTypeText)
// binary
abide.AssertHTTPResponse(t, t.Name(), w.Result(), abide.ResponseTypeBinary) We could use functional options to keep backward compatibility. // ResponseType expected response body type.
type ResponseType string
// ResponseType possible values.
const (
ResponseTypeJSON ResponseType = "json"
ResponseTypeText ResponseType = "text"
ResponseTypeBinary ResponseType = "binary"
)
// SetResponseType define the response type in the options.
func SetResponseType(typ ResponseType) func(*AssertOptions) {
return func(options *AssertOptions) {
options.ResponseType = typ
}
}
// AssertOptions possible options for the assert.
type AssertOptions struct {
ResponseType ResponseType
}
// AssertHTTPResponse asserts the value of an http.Response.
func AssertHTTPResponse(t *testing.T, id string, w *http.Response, opts ...func(*AssertOptions)) {
options := &AssertOptions{}
for _, opt := range opts {
opt(options)
}
// ...
}
abide.AssertHTTPResponse(t, t.Name(), w.Result(), abide.SetResponseType(abide.ResponseTypeBinary)) We could have better names for sure 😄 (I'm terrible at giving names to things). But we would still need to check for the JSON content-type to keep backward compatibility when the |
@rafaeljusto this sounds great and let's go down this route! We should also figure out the module/versioning plan here long term. I'll file a ticket. |
@sjkaliski Applied the changes there. Do you think we should just hex encode binary content or use some hash function? Because the content body can be big and not really comparable 😄 |
Thanks! Will review tonight/tomorrow! |
When retrieving a binary response body, convert it to hexadecimal so we can safely add it to the snapshot file.