Skip to content
This repository has been archived by the owner on May 5, 2023. It is now read-only.

Commit

Permalink
Add https.globalAgent overriding test case (#40)
Browse files Browse the repository at this point in the history
* Add `https.globalAgent` overriding test case

* Only run `https.globalAgent` test on Node >= 10
  • Loading branch information
TooTallNate authored Feb 4, 2020
1 parent 636dab9 commit d3b4e63
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@types/debug": "4",
"@types/mocha": "^5.2.7",
"@types/node": "^12.12.17",
"@types/semver": "^7.1.0",
"@types/ws": "^6.0.3",
"@typescript-eslint/eslint-plugin": "1.6.0",
"@typescript-eslint/parser": "1.1.0",
Expand All @@ -52,6 +53,7 @@
"eslint-plugin-react": "7.12.4",
"mocha": "^6.2.0",
"rimraf": "^3.0.0",
"semver": "^7.1.2",
"typescript": "^3.5.3",
"ws": "^3.0.0"
},
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ namespace createAgent {
public requests: {
[key: string]: http.IncomingMessage[];
};
public options: https.AgentOptions;
private promisifiedCallback?: createAgent.AgentCallbackPromise;
private explicitDefaultPort?: number;
private explicitProtocol?: string;
Expand Down Expand Up @@ -127,6 +128,7 @@ namespace createAgent {
this.maxSockets = 1;
this.sockets = {};
this.requests = {};
this.options = {};
}

get defaultPort(): number {
Expand Down
50 changes: 49 additions & 1 deletion test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import http from 'http';
import https from 'https';
import assert from 'assert';
import listen from 'async-listen';
import { satisfies } from 'semver';
import { Agent, RequestOptions } from '../src';

// In Node 12+ you can just override `http.globalAgent`, but for older Node
Expand Down Expand Up @@ -186,7 +187,7 @@ describe('Agent (TypeScript)', () => {
assert(gotCallback);
} finally {
server.close();
http.globalAgent = originalAgent;
httpAgent.globalAgent = agent;
}
});

Expand Down Expand Up @@ -429,5 +430,52 @@ describe('Agent (TypeScript)', () => {
server.close();
}
});

if (satisfies(process.version, '>= 10')) {
it('should work when overriding `https.globalAgent`', async () => {
let gotReq = false;
let gotCallback = false;

const agent = new Agent(
(req: http.ClientRequest, opts: RequestOptions): net.Socket => {
gotCallback = true;
assert.equal(opts.secureEndpoint, true);
assert.equal(opts.protocol, 'https:');
return tls.connect(opts);
}
);

const server = https.createServer(sslOptions, (req, res) => {
gotReq = true;
res.setHeader('X-Foo', 'bar');
res.setHeader('X-Url', req.url || '/');
res.end();
});
await listen(server);

const addr = server.address();
if (!addr || typeof addr === 'string') {
throw new Error('Server did not bind to a port');
}
const { port } = addr;

// Override the default `https.globalAgent`
const originalAgent = https.globalAgent;
https.globalAgent = agent;

try {
const info: https.RequestOptions = url.parse(`https://127.0.0.1:${port}/foo`);
info.rejectUnauthorized = false;
const res = await req(info);
assert.equal('bar', res.headers['x-foo']);
assert.equal('/foo', res.headers['x-url']);
assert(gotReq);
assert(gotCallback);
} finally {
server.close();
https.globalAgent = originalAgent;
}
});
}
});
});

0 comments on commit d3b4e63

Please sign in to comment.