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

ReplyHeaders type fixed #2729

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mlegenhausen
Copy link

@mlegenhausen mlegenhausen commented Mar 19, 2024

Following type fixes does this PR include:

  • string[] removed from ReplyHeaderValue and ReplyHeaderFunction. The resulting IncomingMessage.rawHeaders value would else be invalid. See example below.
  • ReplyHeaderFunction[] replaced with just string[]. This actually leads to an runtime error when running nock.

See following examples

import http from 'node:http';
import nock from 'nock';

nock('http://github.com')
  .get('/nock/nock')
  .reply(200, 'Hello from GitHub!', {
    'X-My-Headers': ['My Header value 1', 'My Header value 2'],
  });

const req = http.request('http://github.com/nock/nock', (res) => {
  console.log(res.rawHeaders);
});

req.end();

Output:

[ 'X-My-Headers', [ 'My Header value 1', 'My Header value 2' ] ]

Which is an invalid rawHeaders value. The node docs define a string[]: https://nodejs.org/docs/latest/api/http.html#messagerawheaders

import http from 'node:http';
import nock from 'nock';

nock('http://github.com')
  .get('/nock/nock')
  .reply(200, 'Hello from GitHub!', [() => 'Test1', () => 'Test2']);

const req = http.request('http://github.com/nock/nock', (res) => {
  console.log(res.rawHeaders);
});

req.end();

This results in an exception:

TypeError: name.toLowerCase is not a function
    at addHeaderLine (/node_modules/nock/lib/common.js:339:20)
    at /node_modules/nock/lib/common.js:280:5
    at forEachHeader (/node_modules/nock/lib/common.js:396:5)
    at Object.headersArrayToObject (/node_modules/nock/lib/common.js:279:3)
    at Interceptor.reply (/node_modules/nock/lib/interceptor.js:154:27)
    at file:///playground/dist/main.js:5:6
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
    at async loadESM (node:internal/process/esm_loader:28:7)
    at async handleMainPromise (node:internal/modules/run_main:113:12)

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.

None yet

1 participant