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

feat: Injectable request #131

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions packages/thrift-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
8 changes: 5 additions & 3 deletions packages/thrift-client/src/main/connections/HttpConnection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as Core from '@creditkarma/thrift-server-core'

import request = require('request')

import * as request from 'request'
import {
Request,
RequestAPI,
Expand Down Expand Up @@ -127,6 +126,7 @@ export class HttpConnection extends Core.ThriftConnection<RequestOptions> {
private readonly requestOptions: RequestOptions
private readonly serviceName: string | undefined
private readonly withEndpointPerMethod: boolean
private readonly requestImpl: typeof request

constructor({
hostName,
Expand All @@ -139,6 +139,7 @@ export class HttpConnection extends Core.ThriftConnection<RequestOptions> {
serviceName,
withEndpointPerMethod = false,
headerBlacklist = [],
requestImpl = request,
}: IHttpConnectionOptions) {
super(Core.getTransport(transport), Core.getProtocol(protocol))
this.requestOptions = Object.freeze(
Expand All @@ -153,6 +154,7 @@ export class HttpConnection extends Core.ThriftConnection<RequestOptions> {
this.withEndpointPerMethod = withEndpointPerMethod
this.url = `${this.basePath}${this.path}`
this.filters = []
this.requestImpl = requestImpl
}

public register(
Expand Down Expand Up @@ -234,7 +236,7 @@ export class HttpConnection extends Core.ThriftConnection<RequestOptions> {
)

return new Promise((resolve, reject) => {
request(
this.requestImpl(
requestOptions,
(err: any, response: RequestResponse, body: Buffer) => {
if (
Expand Down
1 change: 1 addition & 0 deletions packages/thrift-client/src/main/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export interface IHttpConnectionOptions {
requestOptions?: RequestOptions
withEndpointPerMethod?: boolean
headerBlacklist?: Array<string>
requestImpl?: typeof request
}

export interface ICreateHttpClientOptions extends IHttpConnectionOptions {
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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'
Expand Down Expand Up @@ -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', () => {
Expand Down