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: improve http-client coverage #33633

Merged
merged 6 commits into from
Dec 19, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
http: do not override user-provided options object
KuthorX authored and aduh95 committed Sep 19, 2023
commit 915b724e6576f99479242c740858aabf4ae2e68b
14 changes: 14 additions & 0 deletions test/parallel/test-http-client-input-function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const ClientRequest = require('http').ClientRequest;

{
const req = new ClientRequest(() => {});
Copy link
Member

Choose a reason for hiding this comment

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

The test should definitely hit the mentioned code line but it would be great to write a test that actually verifies that the callback is also called and ideally, this test case would not cause any errors, since it's a legit code path.

Copy link
Contributor Author

@KuthorX KuthorX May 30, 2020

Choose a reason for hiding this comment

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

@BridgeAR Here is a rewrote version, which can make cb be called, but it's a little longer than previous version, so I am not sure whether it's ok, could you help me to check?

'use strict';

const common = require('../common');
const http = require('http');
const assert = require('assert');
const ClientRequest = require('http').ClientRequest;

{
  const server = http.createServer(common.mustCall((req, res) => {
    res.writeHead(200);
    res.end('hello world');
  })).listen(80, '127.0.0.1');
  
  const req = new ClientRequest(common.mustCall((response) => {
    let body = '';
    response.setEncoding('utf8');
    response.on('data', (chunk) => {
      body += chunk;
    });

    response.on('end', common.mustCall(() => {
      assert.strictEqual(body, 'hello world');
      server.close();
    }));
  }));

  req.end();
}

req.on('error', common.mustCall((err) => {
assert.strictEqual(err.code, 'ECONNREFUSED');
}))
assert.strictEqual(req.path, '/');
assert.strictEqual(req.method, 'GET');
}
13 changes: 13 additions & 0 deletions test/parallel/test-http-client-insecure-http-parser-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const ClientRequest = require('http').ClientRequest;

{
assert.throws(() => {
new ClientRequest({insecureHTTPParser: 'wrongValue'});
}, {
code: /ERR_INVALID_ARG_TYPE/
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
code: /ERR_INVALID_ARG_TYPE/
code: 'ERR_INVALID_ARG_TYPE',
message: /insecureHTTPParser/

}, 'http request should throw when passing not-boolean value as insecureHTTPParser');
}