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

Feature: Handle binary response body #53

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

rafaeljusto
Copy link
Contributor

When retrieving a binary response body, convert it to hexadecimal so we can safely add it to the snapshot file.

When retrieving a binary response body, convert it to hexadecimal so we can
safely add it to the snapshot file.
@sjkaliski
Copy link
Collaborator

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?

@rafaeljusto
Copy link
Contributor Author

Yeah, totally agree with you. It will be a pain to keep track of all content-types.

Maybe the method determines the content-type on its own?

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 ResponseType is empty. What do you think?

@sjkaliski
Copy link
Collaborator

@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.

@rafaeljusto
Copy link
Contributor Author

@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 😄

@sjkaliski
Copy link
Collaborator

Thanks! Will review tonight/tomorrow!

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