Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Verify whether Dredd handles binary data #1094

Merged
merged 15 commits into from
Aug 2, 2018
Merged

Conversation

honzajavorek
Copy link
Contributor

@honzajavorek honzajavorek commented Jul 20, 2018

🚀 Why this change?

This is to close the Epic: empty responses and binary files epic. Since the empty responses have been fixed, it is (probably!) already possible to test binary data with Dredd or to easily skip the testing. However, this isn't verified in Dredd's test suite and there are no examples in the docs.

🚧 To Do

📝 Related issues and Pull Requests

✅ What didn't I forget?

  • To write docs
  • To write tests
  • To put Conventional Changelog prefixes in front of all my commits and run npm run lint

@honzajavorek honzajavorek changed the title Verify whether Dredd handles binary responses Verify whether Dredd handles binary data Jul 20, 2018
@honzajavorek honzajavorek force-pushed the honzajavorek/binary branch 2 times, most recently from 7e6109b to 7e5bf31 Compare July 25, 2018 15:45
Uses Base64 encoding as the serialization, which allows also non-JS
hooks to set request/response bodies to a binary content.

Close #617
Close #87
Close #836
Normalize 'multipart/form-data' bodies for both API Blueprint and Swagger,
avoid ignoring falsy values at multiple places (URI parameters, x-example)

try {
httpOptions.body = getBodyAsBuffer(transactionReq.body, transactionReq.bodyEncoding);
httpOptions.headers = setContentLength(transactionReq.headers, httpOptions.body);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that setContentLength is a little bit misleading here, normalizeHeaders or something like that would be more fitting in this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It doesn't interfere with any other headers. It detects the name of the content-length header if it exists, and sets it to the correct value. In case it doesn't exists, it adds the header. IMHO the name is right.

Copy link
Contributor

@michalholasek michalholasek Aug 1, 2018

Choose a reason for hiding this comment

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

normalizeContentLengthHeader then Q___(-_-)Q

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

return 'base64';
}
throw new Error(`Unsupported encoding: '${encoding}' (only UTF-8 and `
+ 'Base64 are supported)');
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: I would personally use switch instead if(s) wherever possible, something like:

switch (lcEncoding) {
  case 'utf-8':
  case 'utf8':
    return 'utf-8'
  case 'base64';
    return 'base64';
  default:
    throw new Error(`Unsupported encoding: '${encoding}' (only UTF-8 and `
      + 'Base64 are supported)');
}

But it is not an issue, just an idea.

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'll trust you this is more javascriptonic 😄 Python doesn't have switch so I never think of using it, even in situations where it probably makes sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

performRequest.getBodyAsBuffer = getBodyAsBuffer;
performRequest.setContentLength = setContentLength;
performRequest.createTransactionRes = createTransactionRes;
performRequest.detectBodyEncoding = detectBodyEncoding;
Copy link
Contributor

Choose a reason for hiding this comment

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

When we are exposing private/internal APIs for testing purposes, I think it would be good idea to prefix the names with leading underscores __ - see this for inspiration 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

lol (inspiration). I did some research and dropped some thoughts on this to apiaryio/dredd-transactions#179 (comment). Let's continue there.

Copy link
Contributor

@michalholasek michalholasek left a comment

Choose a reason for hiding this comment

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

Great work 👍. Just a few comments, I'll leave it up to you whether you decide to make changes or not.

@honzajavorek
Copy link
Contributor Author

@michalholasek Addressed your comments. Would you mind looking at the additional commits and saying 👍 or 👎 ?

Copy link
Contributor

@michalholasek michalholasek left a comment

Choose a reason for hiding this comment

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

👍

@honzajavorek honzajavorek merged commit 36b4f3b into master Aug 2, 2018
@honzajavorek honzajavorek deleted the honzajavorek/binary branch August 2, 2018 14:53
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Find a way how to work with binary files in hooks Dredd testing of APIary calls returning an image / PNG
2 participants