Skip to content

Commit

Permalink
fix(shopify-api): 301 redirect follows with initial method
Browse files Browse the repository at this point in the history
  • Loading branch information
McTom234 committed Jan 22, 2025
1 parent f905b0f commit cb33f46
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ const tests: Record<string | number, Test> = {
}),
}),
},
301: {
expectedRequest: initTestRequest({method: 'post'}),
testResponse: initTestResponse({
statusCode: 301,
headers: {
location: '/url/path/301resolved',
},
}),
},
'301resolved': {
expectedRequest: initTestRequest({method: 'post'}),
testResponse: initTestResponse({
body: 'followed redirect',
statusCode: 204,
}),
},
400: {
expectedRequest: initTestRequest(),
testResponse: initTestResponse({
Expand All @@ -98,6 +114,13 @@ const tests: Record<string | number, Test> = {
body: JSON.stringify({}),
}),
},
405: {
expectedRequest: initTestRequest(),
testResponse: initTestResponse({
statusCode: 405,
statusText: 'Wrong method',
}),
},
417: {
expectedRequest: initTestRequest(),
testResponse: initTestResponse({
Expand Down Expand Up @@ -162,6 +185,12 @@ const server = createServer((req: IncomingMessage, res: ServerResponse) => {
const expectedRequest = test.expectedRequest;
let testResponse = test.testResponse;

if (code.startsWith('301')) {
if (expectedRequest?.method !== req.method?.toLowerCase()) {
testResponse = tests['405'].testResponse;
}
}

if (matchHeaders(receivedHeaders as Headers, expectedRequest.headers)) {
if (code === 'retries' && retryCount < 2) {
testResponse = tests['429'].testResponse;
Expand Down
12 changes: 12 additions & 0 deletions packages/apps/shopify-api/adapters/__e2etests__/test_suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,18 @@ export const testSuite = [
expectedResponse: initTestResponse(),
},
},
{
name: 'gracefully handles 301 redirect',
config: {
testRequest: initTestRequest({
url: '/url/path/301',
type: TestType.Graphql,
}),
expectedResponse: initTestResponse({
statusCode: 204,
}),
},
},
{
name: 'gracefully handles 403 error',
config: {
Expand Down
24 changes: 22 additions & 2 deletions packages/apps/shopify-api/adapters/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,28 @@ import {
nodeRuntimeString,
} from './adapter';

// For the purposes of this package, fetch correctly implements everything we need
setAbstractFetchFunc(fetch as any as AbstractFetchFunc);
// node-fetch redirects post requests as get requests on 301, 302 or 303 status codes
// this does not work for graphql requests, so we need to manually handle redirects
const fetchWithRedirect: AbstractFetchFunc = async (url, init) => {
const fetchOptions = {
...init,
redirect: 'manual',
} satisfies RequestInit;

const response = await (fetch as any as AbstractFetchFunc)(url, fetchOptions);

if (
(response.status === 301 || response.status === 302) &&
response.headers.has('location')
) {
const location = response.headers.get('location')!;
return fetchWithRedirect(location, init);
}

return response;
};

setAbstractFetchFunc(fetchWithRedirect);
setAbstractConvertRequestFunc(nodeConvertRequest);
setAbstractConvertIncomingResponseFunc(nodeConvertIncomingResponse);
setAbstractConvertResponseFunc(nodeConvertAndSendResponse);
Expand Down

0 comments on commit cb33f46

Please sign in to comment.