Skip to content

Fixed failing code generation in http for requests without body#122

Merged
umeshp7 merged 4 commits intodevelopfrom
feature/bugfix-http-no-body
Nov 12, 2019
Merged

Fixed failing code generation in http for requests without body#122
umeshp7 merged 4 commits intodevelopfrom
feature/bugfix-http-no-body

Conversation

@shreys7
Copy link
Member

@shreys7 shreys7 commented Nov 11, 2019

  • Create a GET request with no body. Save it to a collection and click view in web. It fails to generate snippets for HTTP.
  • It fails for all the requests where no body is present.
  • The following function tries to access request.body.mode without having a check for the body field to be present:
  let contentTypeIndex = _.findIndex(request.headers.members, { key: 'Content-Type' }),
    formDataHeader = `multipart/form-data; boundary=${FORM_DATA_BOUNDARY}`,
    headers = '';
 
  if (contentTypeIndex >= 0) {
    if (request.headers.members[contentTypeIndex].value === 'multipart/form-data' || request.body.mode === 'formdata') {
      request.headers.members[contentTypeIndex].value = formDataHeader;
    }
  }
 
  _.forEach(request.headers.members, (header) => {
    header.key = header.key.trim();
  });
  headers = convertPropertyListToString(request.headers, '\n', false);
  if (request.body.mode === 'formdata' && contentTypeIndex < 0) {
    headers += `Content-Type: ${formDataHeader}`;
  }
  return headers;
}
  • Added checks for request.body in the above function and corresponding tests.

@shreys7 shreys7 requested a review from umeshp7 November 11, 2019 07:12
if (contentTypeIndex >= 0) {
if (request.headers.members[contentTypeIndex].value === 'multipart/form-data' || request.body.mode === 'formdata') {
if (request.headers.members[contentTypeIndex].value === 'multipart/form-data' ||
(request.bidy && request.body.mode === 'formdata')) {
Copy link
Contributor

Choose a reason for hiding this comment

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

What is bidy?

Copy link
Contributor

Choose a reason for hiding this comment

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

How is the test passing with this?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry. Corrected the typo. and the test is for the snippet generation should not fail if the request does not have a body. This is happening, the snippet is getting generated.

@@ -168,7 +168,8 @@ function getHeaders (request) {
headers = '';

if (contentTypeIndex >= 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The fix is inside this particular block which checks if the 'Content-Type' header is present.
The unit test you have written does not pass this criterion of contentTypeIndex >= 0. Hence the fix is not being tested. That is why the initial typo wasn't detected.
@shreys7

Copy link
Member Author

Choose a reason for hiding this comment

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

yes. Shall I change the tests?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, you will have to add a 'Content-Type' header. Only then the unit test make sense.

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.

3 participants