This repository has been archived by the owner on Apr 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reintroduce HTTP request logging for OAuth
- Loading branch information
Showing
5 changed files
with
191 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@shopify/shopify-api": patch | ||
--- | ||
|
||
Reintroduced logging of HTML requests for OAuth processes, which was incorrectly removed when the new clients were introduced. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
packages/shopify-api/lib/utils/__tests__/fetch-request.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import {testConfig} from '../../__tests__/test-config'; | ||
import {queueMockResponse} from '../../__tests__/test-helper'; | ||
import {LogSeverity} from '../../types'; | ||
import {fetchRequestFactory} from '../fetch-request'; | ||
|
||
describe('fetchRequest', () => { | ||
const domain = 'test.example.io'; | ||
const url = `https://${domain}`; | ||
const successResponse = {field1: 1, field2: 'value'}; | ||
|
||
it('skips logging if logger.httpRequests is false', async () => { | ||
// GIVEN | ||
const logFn = jest.fn(); | ||
const config = testConfig({ | ||
logger: {log: logFn, level: LogSeverity.Debug, httpRequests: false}, | ||
}); | ||
|
||
queueMockResponse(JSON.stringify(successResponse)); | ||
|
||
// WHEN | ||
const response = await fetchRequestFactory(config)(url); | ||
const responseBody = await response.json(); | ||
|
||
// THEN | ||
expect(logFn).not.toHaveBeenCalled(); | ||
expect(responseBody).toEqual(successResponse); | ||
expect({method: 'GET', domain, path: '/'}).toMatchMadeHttpRequest(); | ||
}); | ||
|
||
it('skips logging if logger.level is higher than Debug', async () => { | ||
// GIVEN | ||
const logFn = jest.fn(); | ||
const config = testConfig({ | ||
logger: {log: logFn, level: LogSeverity.Info, httpRequests: true}, | ||
}); | ||
|
||
queueMockResponse(JSON.stringify(successResponse)); | ||
|
||
// WHEN | ||
const response = await fetchRequestFactory(config)(url); | ||
const responseBody = await response.json(); | ||
|
||
// THEN | ||
expect(logFn).not.toHaveBeenCalled(); | ||
expect(responseBody).toEqual(successResponse); | ||
expect({method: 'GET', domain, path: '/'}).toMatchMadeHttpRequest(); | ||
}); | ||
|
||
it('logs GET requests when configured', async () => { | ||
// GIVEN | ||
const logFn = jest.fn(); | ||
const config = testConfig({ | ||
logger: {log: logFn, level: LogSeverity.Debug, httpRequests: true}, | ||
}); | ||
|
||
queueMockResponse(JSON.stringify(successResponse)); | ||
|
||
// WHEN | ||
const response = await fetchRequestFactory(config)(url); | ||
const responseBody = await response.json(); | ||
|
||
// THEN | ||
expect(logFn).toHaveBeenNthCalledWith( | ||
1, | ||
LogSeverity.Debug, | ||
`[shopify-api/DEBUG] Making HTTP request | {method: GET, url: ${url}}`, | ||
); | ||
expect(logFn).toHaveBeenNthCalledWith( | ||
2, | ||
LogSeverity.Debug, | ||
`[shopify-api/DEBUG] HTTP request completed | {method: GET, url: ${url}, status: 200}`, | ||
); | ||
expect(responseBody).toEqual(successResponse); | ||
expect({method: 'GET', domain, path: '/'}).toMatchMadeHttpRequest(); | ||
}); | ||
|
||
it('logs POST requests with the body when configured', async () => { | ||
// GIVEN | ||
const logFn = jest.fn(); | ||
const config = testConfig({ | ||
logger: {log: logFn, level: LogSeverity.Debug, httpRequests: true}, | ||
}); | ||
|
||
queueMockResponse(JSON.stringify(successResponse)); | ||
|
||
// WHEN | ||
const requestBody = { | ||
requestField1: 1, | ||
}; | ||
const response = await fetchRequestFactory(config)(url, { | ||
method: 'POST', | ||
body: JSON.stringify(requestBody), | ||
}); | ||
const responseBody = await response.json(); | ||
|
||
// THEN | ||
expect(logFn).toHaveBeenNthCalledWith( | ||
1, | ||
LogSeverity.Debug, | ||
`[shopify-api/DEBUG] Making HTTP request | {method: POST, url: ${url}, body: ${JSON.stringify(requestBody)}}`, | ||
); | ||
expect(logFn).toHaveBeenNthCalledWith( | ||
2, | ||
LogSeverity.Debug, | ||
`[shopify-api/DEBUG] HTTP request completed | {method: POST, url: ${url}, status: 200}`, | ||
); | ||
expect(responseBody).toEqual(successResponse); | ||
expect({ | ||
method: 'POST', | ||
domain, | ||
path: '/', | ||
data: requestBody, | ||
}).toMatchMadeHttpRequest(); | ||
}); | ||
|
||
it('logs non-200 response codes', async () => { | ||
// GIVEN | ||
const logFn = jest.fn(); | ||
const config = testConfig({ | ||
logger: {log: logFn, level: LogSeverity.Debug, httpRequests: true}, | ||
}); | ||
|
||
queueMockResponse(JSON.stringify(successResponse), {statusCode: 400}); | ||
|
||
// WHEN | ||
const response = await fetchRequestFactory(config)(url); | ||
const responseBody = await response.json(); | ||
|
||
// THEN | ||
expect(logFn).toHaveBeenNthCalledWith( | ||
1, | ||
LogSeverity.Debug, | ||
`[shopify-api/DEBUG] Making HTTP request | {method: GET, url: ${url}}`, | ||
); | ||
expect(logFn).toHaveBeenNthCalledWith( | ||
2, | ||
LogSeverity.Debug, | ||
`[shopify-api/DEBUG] HTTP request completed | {method: GET, url: ${url}, status: 400}`, | ||
); | ||
expect(responseBody).toEqual(successResponse); | ||
expect({ | ||
method: 'GET', | ||
domain, | ||
path: '/', | ||
}).toMatchMadeHttpRequest(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import {logger} from '../logger'; | ||
import {LogSeverity} from '../types'; | ||
import {abstractFetch} from '../../runtime'; | ||
import {ConfigInterface} from '../base-types'; | ||
|
||
export function fetchRequestFactory(config: ConfigInterface) { | ||
return async function fetchRequest( | ||
url: string, | ||
options?: RequestInit, | ||
): Promise<Response> { | ||
const log = logger(config); | ||
const doLog = | ||
config.logger.httpRequests && config.logger.level === LogSeverity.Debug; | ||
|
||
if (doLog) { | ||
log.debug('Making HTTP request', { | ||
method: options?.method || 'GET', | ||
url, | ||
...(options?.body && {body: options?.body}), | ||
}); | ||
} | ||
|
||
const response = await abstractFetch(url, options); | ||
|
||
if (doLog) { | ||
log.debug('HTTP request completed', { | ||
method: options?.method || 'GET', | ||
url, | ||
status: response.status, | ||
}); | ||
} | ||
|
||
return response; | ||
}; | ||
} |