From 60e431da4af03ac32faaca76ca6921fe6e91af7a Mon Sep 17 00:00:00 2001 From: dwieeb Date: Mon, 8 Jul 2019 17:45:02 -0500 Subject: [PATCH] Update HTTPS config options for superagent (#36627) * add tls configuration types superagent passes the ca, cert, and key parameters to `tls.createSecureContext`: https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options * add `response` event to superagent types --- types/superagent/index.d.ts | 9 +++--- types/superagent/superagent-tests.ts | 47 ++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/types/superagent/index.d.ts b/types/superagent/index.d.ts index 2b6c8036e3..d1b136af5f 100644 --- a/types/superagent/index.d.ts +++ b/types/superagent/index.d.ts @@ -129,21 +129,22 @@ declare namespace request { auth(user: string, pass: string, options?: { type: 'basic' | 'auto' }): this; auth(token: string, options: { type: 'bearer' }): this; buffer(val?: boolean): this; - ca(cert: Buffer): this; - cert(cert: Buffer | string): this; + ca(cert: string | string[] | Buffer | Buffer[]): this; + cert(cert: string | string[] | Buffer | Buffer[]): this; clearTimeout(): this; end(callback?: CallbackHandler): void; field(name: string, val: MultipartValue): this; field(fields: { [fieldName: string]: MultipartValue }): this; get(field: string): string; - key(cert: Buffer | string): this; + key(cert: string | string[] | Buffer | Buffer[]): this; ok(callback: (res: Response) => boolean): this; on(name: 'error', handler: (err: any) => void): this; on(name: 'progress', handler: (event: ProgressEvent) => void): this; + on(name: 'response', handler: (response: Response) => void): this; on(name: string, handler: (event: any) => void): this; parse(parser: Parser): this; part(): this; - pfx(cert: Buffer | string | { pfx: Buffer, passphrase: string }): this; + pfx(cert: string | string[] | Buffer | Buffer[] | { pfx: string | Buffer, passphrase: string }): this; pipe(stream: NodeJS.WritableStream, options?: object): stream.Writable; query(val: object | string): this; redirects(n: number): this; diff --git a/types/superagent/superagent-tests.ts b/types/superagent/superagent-tests.ts index 4ecc0ca2a0..25e10a62ad 100644 --- a/types/superagent/superagent-tests.ts +++ b/types/superagent/superagent-tests.ts @@ -418,6 +418,53 @@ request }) .end(callback); +// HTTPS request with string, Buffer, and arrays of strings and Buffers, from: https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options +request + .post('/secure') + .ca('ca') + .key('key') + .cert('cert') + .end(callback); + +request + .post('/secure') + .ca(['ca']) + .key(['key']) + .cert(['cert']) + .end(callback); + +request + .post('/secure') + .ca([ca]) + .key([key]) + .cert([cert]) + .end(callback); + +request + .post('/secure') + .pfx('cert.pfx') + .end(callback); + +request + .post('/secure') + .pfx(['cert.pfx']) + .end(callback); + +request + .post('/secure') + .pfx([pfx]) + .end(callback); + +// 'response' event, adapted from: https://visionmedia.github.io/superagent/docs/test.html +request + .get('/user/1') + .on('response', res => { + try { + assert.equal('bar', res.body.foo); + } catch (e) { /* ignore */ } + }) + .end(); + // ok, from: https://github.com/visionmedia/superagent/commit/34533bbc29833889090847c45a82b0ea81b2f06d request .get('/404')