From 68457b75a9059fc690b7dca141f661ab1447b02c Mon Sep 17 00:00:00 2001 From: Nick Penaranda Date: Wed, 20 May 2020 21:48:00 -0700 Subject: [PATCH 1/2] Allow request implementation to be injected --- .../src/main/connections/HttpConnection.ts | 8 ++++-- packages/thrift-client/src/main/types.ts | 1 + .../client/connections/HttpConnection.spec.ts | 28 +++++++++++++++++-- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/packages/thrift-client/src/main/connections/HttpConnection.ts b/packages/thrift-client/src/main/connections/HttpConnection.ts index 042fe29f..f7bfaf0d 100644 --- a/packages/thrift-client/src/main/connections/HttpConnection.ts +++ b/packages/thrift-client/src/main/connections/HttpConnection.ts @@ -1,7 +1,6 @@ import * as Core from '@creditkarma/thrift-server-core' -import request = require('request') - +import * as request from 'request' import { Request, RequestAPI, @@ -127,6 +126,7 @@ export class HttpConnection extends Core.ThriftConnection { private readonly requestOptions: RequestOptions private readonly serviceName: string | undefined private readonly withEndpointPerMethod: boolean + private readonly requestImpl: typeof request constructor({ hostName, @@ -139,6 +139,7 @@ export class HttpConnection extends Core.ThriftConnection { serviceName, withEndpointPerMethod = false, headerBlacklist = [], + requestImpl = request, }: IHttpConnectionOptions) { super(Core.getTransport(transport), Core.getProtocol(protocol)) this.requestOptions = Object.freeze( @@ -153,6 +154,7 @@ export class HttpConnection extends Core.ThriftConnection { this.withEndpointPerMethod = withEndpointPerMethod this.url = `${this.basePath}${this.path}` this.filters = [] + this.requestImpl = requestImpl } public register( @@ -234,7 +236,7 @@ export class HttpConnection extends Core.ThriftConnection { ) return new Promise((resolve, reject) => { - request( + this.requestImpl( requestOptions, (err: any, response: RequestResponse, body: Buffer) => { if ( diff --git a/packages/thrift-client/src/main/types.ts b/packages/thrift-client/src/main/types.ts index f2014f3c..d9ac0d98 100644 --- a/packages/thrift-client/src/main/types.ts +++ b/packages/thrift-client/src/main/types.ts @@ -62,6 +62,7 @@ export interface IHttpConnectionOptions { requestOptions?: RequestOptions withEndpointPerMethod?: boolean headerBlacklist?: Array + requestImpl?: typeof request } export interface ICreateHttpClientOptions extends IHttpConnectionOptions { diff --git a/packages/thrift-integration/src/client/connections/HttpConnection.spec.ts b/packages/thrift-integration/src/client/connections/HttpConnection.spec.ts index ace63df6..7939c8b9 100644 --- a/packages/thrift-integration/src/client/connections/HttpConnection.spec.ts +++ b/packages/thrift-integration/src/client/connections/HttpConnection.spec.ts @@ -1,6 +1,8 @@ import * as thrift from '@creditkarma/thrift-server-core' import * as Hapi from '@hapi/hapi' import * as http from 'http' +import * as request from 'request' +import { CoreOptions } from 'request' import { HttpConnection, @@ -15,8 +17,6 @@ import { TTwitter, } from '@creditkarma/thrift-client-ttwitter-filter' -import { CoreOptions } from 'request' - import { HAPI_CALC_SERVER_CONFIG } from '../../config' import { expect } from '@hapi/code' @@ -194,6 +194,30 @@ describe('HttpConnection', () => { }, ) }) + + it('should use request implementation if provided', async () => { + let ourRequestWasCalled = false + const mockRequest = request.defaults({ + auth: { + bearer: () => { + ourRequestWasCalled = true + return 'token' + }, + }, + }) + + const mockedConnection = new HttpConnection({ + serviceName: 'Calculator', + hostName: HAPI_CALC_SERVER_CONFIG.hostName, + port: HAPI_CALC_SERVER_CONFIG.port, + path: HAPI_CALC_SERVER_CONFIG.path, + requestImpl: mockRequest, + }) + const mockedClient = new Calculator.Client(mockedConnection) + return mockedClient + .add(5, 7) + .then(() => expect(ourRequestWasCalled).to.be.true()) + }) }) describe('With endpoint per method', () => { From e059277d82c002f4533fe292f08d0998ce1a56f5 Mon Sep 17 00:00:00 2001 From: Nick Penaranda Date: Wed, 20 May 2020 22:08:29 -0700 Subject: [PATCH 2/2] Update docs --- packages/thrift-client/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/thrift-client/README.md b/packages/thrift-client/README.md index 7f404e4c..906cde4a 100644 --- a/packages/thrift-client/README.md +++ b/packages/thrift-client/README.md @@ -97,6 +97,7 @@ The available options are: * protocol (optional): Name of the Thrift protocol type to use. Defaults to 'binary'. * requestOptions (optional): Options to pass to the underlying [Request library](https://github.com/request/request#requestoptions-callback). Defaults to {}. * register (optional): A list of filters to apply to your client. More on this later. +* requestImpl (optional): Provide a request implementation. Defaults to 'request' module. Currently `@creditkarma/thrift-server-core"` only supports buffered transport and binary or compact protocols.