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

663/mk/standardize op client requests #971

Merged
merged 10 commits into from
Jan 13, 2023
141 changes: 140 additions & 1 deletion packages/open-payments/src/client/requests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
import { createAxiosInstance, deleteRequest, get, post } from './requests'
import { generateKeyPairSync } from 'crypto'
import nock from 'nock'
import { mockOpenApiResponseValidators, silentLogger } from '../test/helpers'
import {
mockOpenApiResponseValidators,
silentLogger,
withEnvVariableOverride
} from '../test/helpers'

describe('requests', (): void => {
const logger = silentLogger
Expand Down Expand Up @@ -194,6 +198,51 @@ describe('requests', (): void => {
)
).rejects.toThrow(/Failed to validate OpenApi response/)
})

test(
'keeps https protocol for request if non-development environment',
withEnvVariableOverride(
{ NODE_ENV: 'production' },
async (): Promise<void> => {
const httpsUrl = 'https://localhost:1000/'
const scope = nock(httpsUrl).get('/').reply(200)

await get(
{ axiosInstance, logger },
{ url: httpsUrl },
responseValidators.successfulValidator
)

expect(axiosInstance.get).toHaveBeenCalledWith(httpsUrl, {
headers: {}
})
scope.done()
}
)
)

test(
'uses http protocol for request if development environment',
withEnvVariableOverride(
{ NODE_ENV: 'development' },
async (): Promise<void> => {
const httpsUrl = 'https://localhost:1000/'
const httpUrl = httpsUrl.replace('https', 'http')
const scope = nock(httpUrl).get('/').reply(200)

await get(
{ axiosInstance, logger },
{ url: httpsUrl },
responseValidators.successfulValidator
)

expect(axiosInstance.get).toHaveBeenCalledWith(httpUrl, {
headers: {}
})
scope.done()
}
)
)
})

describe('post', (): void => {
Expand Down Expand Up @@ -364,6 +413,51 @@ describe('requests', (): void => {
)
).rejects.toThrow(/Failed to validate OpenApi response/)
})

test(
'keeps https protocol for request if non-development environment',
withEnvVariableOverride(
{ NODE_ENV: 'production' },
async (): Promise<void> => {
const httpsUrl = 'https://localhost:1000/'
const scope = nock(httpsUrl).post('/').reply(200)

await post(
{ axiosInstance, logger },
{ url: httpsUrl },
responseValidators.successfulValidator
)

expect(axiosInstance.post).toHaveBeenCalledWith(httpsUrl, undefined, {
headers: {}
})
scope.done()
}
)
)

test(
'uses http protocol for request if development environment',
withEnvVariableOverride(
{ NODE_ENV: 'development' },
async (): Promise<void> => {
const httpsUrl = 'https://localhost:1000/'
const httpUrl = httpsUrl.replace('https', 'http')
const scope = nock(httpUrl).post('/').reply(200)

await post(
{ axiosInstance, logger },
{ url: httpsUrl },
responseValidators.successfulValidator
)

expect(axiosInstance.post).toHaveBeenCalledWith(httpUrl, undefined, {
headers: {}
})
scope.done()
}
)
)
})

describe('delete', (): void => {
Expand Down Expand Up @@ -501,5 +595,50 @@ describe('requests', (): void => {
)
).rejects.toThrow(/Failed to validate OpenApi response/)
})

test(
'keeps https protocol for request if non-development environment',
withEnvVariableOverride(
{ NODE_ENV: 'production' },
async (): Promise<void> => {
const httpsUrl = 'https://localhost:1000/'
const scope = nock(httpsUrl).delete('/').reply(200)

await deleteRequest(
{ axiosInstance, logger },
{ url: httpsUrl },
responseValidators.successfulValidator
)

expect(axiosInstance.delete).toHaveBeenCalledWith(httpsUrl, {
headers: {}
})
scope.done()
}
)
)

test(
'uses http protocol for request if development environment',
withEnvVariableOverride(
{ NODE_ENV: 'development' },
async (): Promise<void> => {
const httpsUrl = 'https://localhost:1000/'
const httpUrl = httpsUrl.replace('https', 'http')
const scope = nock(httpUrl).delete('/').reply(200)

await deleteRequest(
{ axiosInstance, logger },
{ url: httpsUrl },
responseValidators.successfulValidator
)

expect(axiosInstance.delete).toHaveBeenCalledWith(httpUrl, {
headers: {}
})
scope.done()
}
)
)
})
})
17 changes: 17 additions & 0 deletions packages/open-payments/src/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ export const defaultAxiosInstance = createAxiosInstance({
privateKey: generateKeyPairSync('ed25519').privateKey
})

export const withEnvVariableOverride = (
override: Record<string, string>,
testCallback: () => Promise<void>
): (() => Promise<void>) => {
return async () => {
const savedEnvVars = Object.assign({}, process.env)

for (const key in override) {
process.env[key] = override[key]
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
for (const key in override) {
process.env[key] = override[key]
}
Object.assign(process.env, override)


await testCallback()

process.env = savedEnvVars
mkurapov marked this conversation as resolved.
Show resolved Hide resolved
}
}

export const mockOpenApiResponseValidators = () => ({
successfulValidator: ((data: unknown): data is unknown =>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down