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

test: refactor test-httpparser.response.js #14290

Closed
wants to merge 1 commit into from
Closed

Conversation

erdun
Copy link
Contributor

@erdun erdun commented Jul 16, 2017

Replace string concatenation in test/async-hooks/test-httpparser.response.js with template literals

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines
Affected core subsystem(s)

@nodejs-github-bot nodejs-github-bot added async_hooks Issues and PRs related to the async hooks subsystem. test Issues and PRs related to the tests. labels Jul 16, 2017
@targos targos added the code-and-learn Issues related to the Code-and-Learn events and PRs submitted during the events. label Jul 16, 2017
Copy link
Member

@Trott Trott left a comment

Choose a reason for hiding this comment

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

LGTM if Ci is green

Copy link
Member

@benjamingr benjamingr left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution and welcome to the project :)

CRLF +
'pong'
`HTTP/1.1 200 OK${CRLF}Content-types: text/plain${CRLF}` +
`Content-Length: 4${CRLF}${CRLF}pong`
Copy link
Member

Choose a reason for hiding this comment

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

IMO this does not represent the HTTP header structure as well as the old code did, where one code line was equivalent to one line of the request.

Copy link
Member

Choose a reason for hiding this comment

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

I tend to agree - I'd rather see the CRLF on a new line.

Copy link
Member

Choose a reason for hiding this comment

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

I think the thing to do is to go back to the old code layout, but replace lines 22, 23, and 24 with template literals:

const request = Buffer.from(
  `HTTP/1.1 200 OK${CRLF}` +
  `Content-types: text/plain${CRLF}` +
  `Content-Length: 4${CRLF}` +
  CRLF +
  'pong'
);

@erdun Does that seem good to you? Can you make that change?

Copy link
Member

Choose a reason for hiding this comment

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

Another possibility might be to use Array.prototype.join() but I'm not sure it's more readable:

const request = Buffer.from(
  ['HTTP 1.1 200 OK',
   'Content-types: text/plain',
   'Content-Length: 4',
   '',
   'pong'
  ].join(CRLF)
)

Copy link
Member

Choose a reason for hiding this comment

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

Yet another possibility for readable-and-an-improvement-over-what's-there-now is to leave it as strings but remove the inline concatenation by removing the CRLF variable:

  'HTTP/1.1 200 OK\r\n' +
  'Content-types: text/plain\r\n' +
  'Content-Length: 4\r\n' +
  '\r\n' +
  'pong'

Aside: Is Content-types is a typographical error and it is supposed to be Content-type? If so, maybe that should be fixed too.

Copy link
Member

Choose a reason for hiding this comment

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

Oh okay, I assumed the capitalization was as much by design as the typo in the header name.

cc @thlorenz @nodejs/async_hooks

Copy link
Member

Choose a reason for hiding this comment

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

I thought the capitalization was by design since this is a test file.

IDK, looks like the test just checks that you get a response. You think it's checking that a different capitalisation is still valid?

Copy link
Member

Choose a reason for hiding this comment

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

@gibfahn I thought it was checking the misnamed header (It's Content-types here and not Content-Type) I assumed it was checking compatibility with nonconforming clients.

Copy link
Member

Choose a reason for hiding this comment

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

I don't see any reason why Content-types vs Content-type would affect the test. I think it is just a meaningless error. The test just checks that creating an HTTPParser object correctly invokes async_hooks.

Copy link
Contributor

Choose a reason for hiding this comment

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

BTW That header is not mandatory. IMHO it should be removed.
Actually the only header that "MUST" be included is the Status-Line and two CRLFs
Ref: https://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6

@vsemozhetbyt
Copy link
Contributor

Copy link
Member

@tniessen tniessen left a comment

Choose a reason for hiding this comment

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

  1. The code is less readable than before. (Why do we even use CRLF instead of \r\n?)
  2. The HTTP protocol is strictly line-oriented and the old code depicted that nicely by using one line of code per HTTP request line. These changes shadow the structure of the request (see test: refactor test-httpparser.response.js #14290 (comment)).

'Content-Length: 4' + CRLF +
CRLF +
'pong'
`HTTP/1.1 200 OK${CRLF}Content-types: text/plain${CRLF}` +
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps just replace the ${CRLF} with a literal \r\n ?

Copy link
Member

Choose a reason for hiding this comment

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

+1 (see #14290 (review))

Copy link
Contributor

@refack refack left a comment

Choose a reason for hiding this comment

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

Message should be minimized (optional headers removed)

Copy link
Contributor

@trevnorris trevnorris left a comment

Choose a reason for hiding this comment

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

git commit title is too long. please limit to 50 characters.

* replace CRLF constant with '\r\n' literal
* fix typo in HTTP header name
@Trott
Copy link
Member

Trott commented Jul 26, 2017

Fixed up commit message and nits. PTAL @trevnorris @refack @tniessen

@Trott
Copy link
Member

Trott commented Jul 26, 2017

@tniessen tniessen changed the title test: replace string concatenation with template literals test: refactor test-httpparser.response.js Jul 26, 2017
@trevnorris trevnorris dismissed their stale review July 27, 2017 14:08

Thanks for fixing the git commit title

Trott pushed a commit to Trott/io.js that referenced this pull request Jul 27, 2017
* replace CRLF constant with '\r\n' literal
* fix typo in HTTP header name

PR-URL: nodejs#14290
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Gibson Fahnestock <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
@Trott
Copy link
Member

Trott commented Jul 27, 2017

Landed in 672e020.

Thanks for the contribution! 🎉

@Trott Trott closed this Jul 27, 2017
addaleax pushed a commit that referenced this pull request Jul 27, 2017
* replace CRLF constant with '\r\n' literal
* fix typo in HTTP header name

PR-URL: #14290
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Gibson Fahnestock <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
@addaleax addaleax mentioned this pull request Aug 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
async_hooks Issues and PRs related to the async hooks subsystem. code-and-learn Issues related to the Code-and-Learn events and PRs submitted during the events. test Issues and PRs related to the tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.