Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
feat(server): ExpressJS to Fastify migration (#938)
Browse files Browse the repository at this point in the history
  • Loading branch information
giulianok committed Mar 6, 2023
1 parent 636f24c commit 9b0c4b3
Show file tree
Hide file tree
Showing 117 changed files with 11,715 additions and 27,587 deletions.
31 changes: 31 additions & 0 deletions __mocks__/node-fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2022 American Express Travel Related Services Company, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express
* or implied. See the License for the specific language governing permissions and limitations
* under the License.
*/

const fetch = jest.createMockFromModule('node-fetch');
fetch.mockReturnJsonOnce = (obj) => {
if (obj instanceof Error) {
return fetch.mockImplementationOnce(() => Promise.reject(obj));
}

return fetch.mockImplementationOnce(() => Promise.resolve({ body: JSON.stringify(obj) }));
};

fetch.mockReturnFileOnce = (body) => {
if (body instanceof Error) {
return fetch.mockImplementationOnce(() => Promise.reject(body));
}

return fetch.mockImplementationOnce(() => Promise.resolve({ body, statusCode: 200 }));
};
module.exports = fetch;
9 changes: 4 additions & 5 deletions __tests__/client/initClient.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@

import { fromJS } from 'immutable';

jest.mock('@americanexpress/one-app-router', () => {
const reactRouter = jest.requireActual('@americanexpress/one-app-router');
jest.spyOn(reactRouter, 'matchPromise');
return reactRouter;
});
jest.mock('@americanexpress/one-app-router', () => ({
...jest.requireActual('@americanexpress/one-app-router'),
matchPromise: jest.fn(),
}));

jest.mock('../../src/client/prerender', () => {
const prerender = jest.requireActual('../../src/client/prerender');
Expand Down
2 changes: 2 additions & 0 deletions __tests__/integration/__snapshots__/one-app.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ exports[`Tests that require Docker setup one-app successfully started metrics ha
"nodejs_active_handles_total",
"nodejs_active_requests",
"nodejs_active_requests_total",
"nodejs_active_resources",
"nodejs_active_resources_total",
"nodejs_eventloop_lag_max_seconds",
"nodejs_eventloop_lag_mean_seconds",
"nodejs_eventloop_lag_min_seconds",
Expand Down
74 changes: 40 additions & 34 deletions __tests__/integration/one-app.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ describe('Tests that require Docker setup', () => {
headers: {
origin: 'test.example.com',
},
body: JSON.stringify({}),
});
const rawHeaders = response.headers.raw();
expect(response.status).toBe(200);
expect(rawHeaders).not.toHaveProperty('access-control-allow-origin');
expect(rawHeaders).not.toHaveProperty('access-control-expose-headers');
expect(rawHeaders).not.toHaveProperty('access-control-allow-credentials');
});
Expand Down Expand Up @@ -210,9 +210,9 @@ describe('Tests that require Docker setup', () => {
headers: {
origin: 'test.example.com',
},
body: {
body: JSON.stringify({
message: 'Hello!',
},
}),
}
);
const rawHeaders = response.headers.raw();
Expand Down Expand Up @@ -1335,9 +1335,6 @@ describe('Tests that require Docker setup', () => {
date: [
expect.any(String),
],
etag: [
expect.any(String),
],
'one-app-version': [
expect.any(String),
],
Expand All @@ -1348,14 +1345,23 @@ describe('Tests that require Docker setup', () => {
'max-age=15552000; includeSubDomains',
],
vary: [
'Accept-Encoding',
'Accept-Encoding, accept-encoding',
],
'x-content-type-options': [
'nosniff',
],
'x-dns-prefetch-control': [
'off',
],
'x-download-options': [
'noopen',
],
'x-frame-options': [
'DENY',
],
'x-permitted-cross-domain-policies': [
'none',
],
'x-xss-protection': [
'1; mode=block',
],
Expand All @@ -1369,9 +1375,11 @@ describe('Tests that require Docker setup', () => {
headers: {
origin: 'test.example.com',
},
body: {},
});

expect(response.headers.raw()).toEqual({
vary: ['Accept-Encoding'],
connection: [
'close',
],
Expand All @@ -1385,7 +1393,7 @@ describe('Tests that require Docker setup', () => {
expect.any(String),
],
'referrer-policy': [
'no-referrer',
'same-origin',
],
'strict-transport-security': [
'max-age=15552000; includeSubDomains',
Expand All @@ -1400,13 +1408,13 @@ describe('Tests that require Docker setup', () => {
'noopen',
],
'x-frame-options': [
'SAMEORIGIN',
'DENY',
],
'x-permitted-cross-domain-policies': [
'none',
],
'x-xss-protection': [
'0',
'1; mode=block',
],
});
expect(response.status).toBe(204);
Expand All @@ -1421,6 +1429,7 @@ describe('Tests that require Docker setup', () => {
headers: {
origin: 'test.example.com',
},
body: {},
});

expect(response.headers.raw()).toEqual({
Expand All @@ -1439,20 +1448,17 @@ describe('Tests that require Docker setup', () => {
date: [
expect.any(String),
],
etag: [
expect.any(String),
],
'one-app-version': [
expect.any(String),
],
'referrer-policy': [
'no-referrer',
'same-origin',
],
'strict-transport-security': [
'max-age=15552000; includeSubDomains',
],
vary: [
'Accept-Encoding',
'Accept-Encoding, accept-encoding',
],
'x-content-type-options': [
'nosniff',
Expand All @@ -1464,13 +1470,13 @@ describe('Tests that require Docker setup', () => {
'noopen',
],
'x-frame-options': [
'SAMEORIGIN',
'DENY',
],
'x-permitted-cross-domain-policies': [
'none',
],
'x-xss-protection': [
'0',
'1; mode=block',
],
});
expect(response.status).toBe(415);
Expand All @@ -1485,8 +1491,11 @@ describe('Tests that require Docker setup', () => {
origin: 'test.example.com',
'content-type': 'application/json',
},
body: JSON.stringify({}),
});

// expect(response.status).toBe(204);
expect(await response.text()).toBe('');
expect(response.headers.raw()).toEqual({
connection: [
'close',
Expand All @@ -1497,18 +1506,16 @@ describe('Tests that require Docker setup', () => {
date: [
expect.any(String),
],
etag: [
expect.any(String),
],
'one-app-version': [
expect.any(String),
],
'referrer-policy': [
'no-referrer',
'same-origin',
],
'strict-transport-security': [
'max-age=15552000; includeSubDomains',
],
vary: ['Accept-Encoding'],
'x-content-type-options': [
'nosniff',
],
Expand All @@ -1519,17 +1526,15 @@ describe('Tests that require Docker setup', () => {
'noopen',
],
'x-frame-options': [
'SAMEORIGIN',
'DENY',
],
'x-permitted-cross-domain-policies': [
'none',
],
'x-xss-protection': [
'0',
'1; mode=block',
],
});
expect(response.status).toBe(204);
expect(await response.text()).toBe('');
});

test('Request: /foo/invalid.json', async () => {
Expand All @@ -1542,6 +1547,7 @@ describe('Tests that require Docker setup', () => {
});

expect(response.status).toBe(404);
expect(await response.text()).toBe('Not found');
expect(response.headers.raw()).toEqual({
'cache-control': [
'no-store',
Expand All @@ -1561,8 +1567,8 @@ describe('Tests that require Docker setup', () => {
date: [
expect.any(String),
],
etag: [
expect.any(String),
'expect-ct': [
'max-age=0',
],
'one-app-version': [
expect.any(String),
Expand All @@ -1577,7 +1583,7 @@ describe('Tests that require Docker setup', () => {
'max-age=15552000; includeSubDomains',
],
vary: [
'Accept-Encoding',
'Accept-Encoding, accept-encoding',
],
'x-content-type-options': [
'nosniff',
Expand Down Expand Up @@ -1655,9 +1661,9 @@ describe('Tests that can run against either local Docker setup or remote One App
headers: {
origin: 'test.example.com',
},
body: {
body: JSON.stringify({
message: 'Hello!',
},
}),
}
);
expect(response.status).toBe(200);
Expand All @@ -1669,11 +1675,10 @@ describe('Tests that can run against either local Docker setup or remote One App
const response = await fetch(`${appInstanceUrls.fetchUrl}/success`, {
...defaultFetchOpts,
method: 'POST',
body: {},
});
const pageHtml = await response.text();
expect(pageHtml.includes('Hello! One App is successfully rendering its Modules!')).toBe(
true
);
expect(pageHtml).toContain('Hello! One App is successfully rendering its Modules!');
});

test('app passes vitruvius data to modules', async () => {
Expand Down Expand Up @@ -1702,7 +1707,7 @@ describe('Tests that can run against either local Docker setup or remote One App
method: 'GET',
originalUrl: '/vitruvius',
params: {
0: '/vitruvius',
'*': 'vitruvius',
},
protocol: expect.stringMatching(/^https?$/),
query: {},
Expand Down Expand Up @@ -1732,6 +1737,7 @@ describe('Tests that can run against either local Docker setup or remote One App
sendingData: 'in POSTs',
});
});

test('app passes urlencoded POST data to modules via vitruvius', async () => {
const response = await fetch(`${appInstanceUrls.fetchUrl}/vitruvius`, {
...defaultFetchOpts,
Expand Down
2 changes: 1 addition & 1 deletion __tests__/server/config/env/runTime.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ describe('runTime', () => {
const referrerPolicyOverride = getEnvVarConfig('ONE_REFERRER_POLICY_OVERRIDE');

it('default value', () => {
expect(referrerPolicyOverride.defaultValue()).toEqual('same-origin');
expect(referrerPolicyOverride.defaultValue()).toEqual('');
});

it('validates approved policy', () => {
Expand Down
Loading

0 comments on commit 9b0c4b3

Please sign in to comment.