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

Added option for custom headers #887

Merged
merged 3 commits into from
Oct 7, 2024
Merged

Conversation

dxdxdt
Copy link
Contributor

@dxdxdt dxdxdt commented Aug 15, 2024

Difference/improvements over #885:

  • Semantically identical to curl -H and curl --header 1
  • Empty headers are allowed (RFC 7230)
  • Includes tests

Sorry :)

Relevant issues

Resolves #884

Contributor checklist
  • Provide tests for the changes (unless documentation-only)
  • Documented any new features, CLI switches, etc. (if applicable)
    • Server --help output
    • README.md
    • doc/http-server.1 (use the same format as other entries)
  • The pull request is being made against the master branch
Maintainer checklist
  • Assign a version triage tag
  • Approve tests if applicable

Footnotes

  1. https://curl.se/docs/manpage.html#-H

- Change --help option output
- Accept empty header to conform to RFC 7230 (-H "empty-header" or
  -H "empty-header:")
};

function setHeader(str) {
const m = /^(.+?)\s*(:\s*(.*))$/.exec(str);
Copy link
Contributor

Choose a reason for hiding this comment

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

note: not re-used, so should be safe from RegExp state caveat

if (argv.cors) {
options.cors = true;
if (typeof argv.cors === 'string') {
options.corsHeaders = argv.cors;
}
}

if (argv.header) {
if (Array.isArray(argv.header)) {
argv.header.forEach(h => setHeader(h));
Copy link
Contributor

Choose a reason for hiding this comment

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

note: I remember reading somewhere that forEach actually has a performance impact over for, but I'm considering that an unnecessary optimization and not requiring any change here.

@KernelDeimos
Copy link
Contributor

Looks good, merging this - thanks! I've opened #899 for continued discussion before this goes into a release.

@KernelDeimos KernelDeimos merged commit 7f7d93a into http-party:master Oct 7, 2024
@KernelDeimos
Copy link
Contributor

Tests are failing. Also mentioned this in the aforementioned discussion; please respond in the discussion rather than here.

 FAIL  test/cli.test.js 5 failed of 16 1.377s
✖ single --header option is applied > test unfinished
  test/cli.test.js                                                                                        
  132   });                                                                                               
  133 }                                                                                                   
  134                                                                                                     
  135 test('single --header option is applied', (t) => {                                                  
  ━━━━┛                                                                                                   
  136   t.plan(4);                                                                                        
  137                                                                                                     
  138   doHeaderOptionTest(t,                                                                             
  139     ['--header=X-http-server-test-A: hello'],                                                       
  Object.<anonymous> (test/cli.test.js:135:1)

✖ single --header option is applied > test count(0) != plan(4)
  test/cli.test.js                                                                                        
  133 }                                                                                                   
  134                                                                                                     
  135 test('single --header option is applied', (t) => {                                                  
  136   t.plan(4);                                                                                        
  ━━━━━━━━┛                                                                                               
  137                                                                                                     
  138   doHeaderOptionTest(t,                                                                             
  139     ['--header=X-http-server-test-A: hello'],                                                       
  140     { 'x-http-server-test-a': 'hello' }                                                             
  --- expected   
  +++ actual     
  @@ -1,1 +1,1 @@
  -4             
  +0             

✖ child test left in queue: single -H option is applied
✖ child test left in queue: mix of multiple --header and -H options are applied
✖ child test left in queue: empty header value is allowed (RFC 7230)

Asserts:  11 pass  5 fail  16 of 16 complete
Suites:    0 pass  1 fail    1 of 1 complete

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.

User-provided custom headers
2 participants