Skip to content

Commit

Permalink
Merge pull request #1419 from btomaj/fix-test-helpers
Browse files Browse the repository at this point in the history
Fix setUpValidRequest to preserve headers
  • Loading branch information
paulomarg authored Aug 27, 2024
2 parents 393702e + 8e61a39 commit 4d3364f
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 13 deletions.
6 changes: 6 additions & 0 deletions .changeset/short-mayflies-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@shopify/shopify-app-remix': patch
'@shopify/shopify-api': patch
---

setUpValidRequest in shopify-api now preserves headers. Documentation for test helpers now more accurately describes use cases.
2 changes: 1 addition & 1 deletion packages/apps/shopify-api/docs/guides/test-helpers.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Testing your app

This package exports two helper methods through `@shopify/shopify-api/test-helpers` to simplify end-to-end testing: `setUpValidSession()` and `setUpValidRequest()`. These methods can be used together to fake authorization during integration testing or end-to-end testing. `setUpValidSession()` creats a fake session, and `setUpValidRequest()` modifies Requests so that this package authorizes them against the fake session.
This package exports two helper methods through `@shopify/shopify-api/test-helpers` to simplify integration testing and local end-to-end testing: `setUpValidSession()` and `setUpValidRequest()`. These methods can be used together to fake authorization. `setUpValidSession()` creats a fake session, and `setUpValidRequest()` modifies Requests so that this package authorizes them against the fake session.

## setUpValidSession()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ describe('setUpValidRequest', () => {
expect(token?.length).toBeGreaterThan(0);
});

it('bearer requests preserve headers', async () => {
options = {
type: RequestType.Bearer,
store: TEST_SHOP_NAME,
apiKey: API_KEY,
apiSecretKey: API_SECRET_KEY,
};

request.headers.set('preserved-header', 'preserved header value');

const authorizedRequest = await setUpValidRequest(options, request);
const preservedHeader = authorizedRequest.headers.get('preserved-header');

expect(preservedHeader).toBe('preserved header value');
});

it('can create an extension request', async () => {
options = {
type: RequestType.Extension,
Expand All @@ -82,6 +98,22 @@ describe('setUpValidRequest', () => {
expect(requestBody).toEqual('test');
});

it('extension requests preserve headers', async () => {
options = {
type: RequestType.Extension,
store: TEST_SHOP_NAME,
apiSecretKey: API_SECRET_KEY,
body: 'test',
};

request.headers.set('preserved-header', 'preserved header value');

const authorizedRequest = await setUpValidRequest(options, request);
const preservedHeader = authorizedRequest.headers.get('preserved-header');

expect(preservedHeader).toBe('preserved header value');
});

it('can create a public request', async () => {
options = {
type: RequestType.Public,
Expand Down
31 changes: 20 additions & 11 deletions packages/apps/shopify-api/test-helpers/setup-valid-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,10 @@ function bearerRequest(
) {
const {token} = getJwt(store, apiKey, apiSecretKey);

return new Request(request, {
headers: {
authorization: `Bearer ${token}`,
},
});
const authenticatedRequest = new Request(request);
authenticatedRequest.headers.set('authorization', `Bearer ${token}`);

return authenticatedRequest;
}

function extensionRequest(
Expand All @@ -128,15 +127,25 @@ function extensionRequest(
) {
const bodyString = JSON.stringify(body);

return new Request(request, {
const authenticatedRequest = new Request(request, {
method: 'POST',
body: bodyString,
headers: {
'X-Shopify-Hmac-Sha256': getHmac(bodyString, apiSecretKey),
'X-Shopify-Shop-Domain': getShopValue(store),
...headers,
},
});
authenticatedRequest.headers.set(
'X-Shopify-Hmac-Sha256',
getHmac(bodyString, apiSecretKey),
);
authenticatedRequest.headers.set(
'X-Shopify-Shop-Domain',
getShopValue(store),
);
if (headers) {
for (const [key, value] of Object.entries(headers)) {
authenticatedRequest.headers.set(key, value);
}
}

return authenticatedRequest;
}

async function publicRequest(
Expand Down
2 changes: 1 addition & 1 deletion packages/apps/shopify-app-remix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ const shopify = shopifyApp(config);
...
```

`testConfig()` accepts a config object as an optional parameter. The config values provided override the default config values returned by `testConfig()`. This is especially useful for end-to-end testing to ensure `shopifyApp()` reads the sessions from the development database.
`testConfig()` accepts a config object as an optional parameter. The config values provided override the default config values returned by `testConfig()`. This is especially useful for integration testing and end-to-end testing to ensure `shopifyApp()` reads the sessions from the development database.

```ts
// my-app/app/shopify.server.ts
Expand Down

0 comments on commit 4d3364f

Please sign in to comment.