Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,8 @@
"jest-json-schema": "^6.1.0",
"jest-watch-typeahead": "^2.2.2",
"mini-css-extract-plugin": "^2.9.4",
"mock-express-request": "^0.2.0",
"mock-express-response": "^0.3.0",
"node-fetch": "^3.3.2",
"node-mocks-http": "^1.17.2",
"nodemon": "^3.1.10",
"pino-devtools": "^2.8.0",
"pino-pretty": "^13.1.1",
Expand Down
7 changes: 3 additions & 4 deletions tests/unit/amo/middleware/test_HstsMiddleware.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import MockExpressRequest from 'mock-express-request';
import MockExpressResponse from 'mock-express-response';
import httpMocks from 'node-mocks-http';

import { hsts } from 'amo/middleware';

describe(__filename, () => {
it('provides the expected HSTS headers', () => {
const middleware = hsts();
const nextSpy = sinon.stub();
const req = new MockExpressRequest();
const res = new MockExpressResponse();
const req = httpMocks.createRequest();
const res = httpMocks.createResponse();

middleware(req, res, nextSpy);

Expand Down
15 changes: 7 additions & 8 deletions tests/unit/amo/middleware/test_cspMiddleware.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import MockExpressRequest from 'mock-express-request';
import MockExpressResponse from 'mock-express-response';
import httpMocks from 'node-mocks-http';
import parse from 'content-security-policy-parser';

import { csp } from 'amo/middleware';
Expand Down Expand Up @@ -122,8 +121,8 @@ describe(__filename, () => {
const config = require('config');
const middleware = csp({ _config: config });
const nextSpy = sinon.stub();
const req = new MockExpressRequest();
const res = new MockExpressResponse();
const req = httpMocks.createRequest();
const res = httpMocks.createResponse();
middleware(req, res, nextSpy);
const cspHeader = res.get('content-security-policy');
const policy = parse(cspHeader);
Expand All @@ -140,8 +139,8 @@ describe(__filename, () => {
const middleware = csp({ _config: getFakeConfig({ CSP: false }), _log });

const nextSpy = sinon.stub();
const req = new MockExpressRequest();
const res = new MockExpressResponse();
const req = httpMocks.createRequest();
const res = httpMocks.createResponse();

middleware(req, res, nextSpy);

Expand All @@ -157,8 +156,8 @@ describe(__filename, () => {
const middleware = csp({ _config: getFakeConfig({ CSP: false }), _log });

const nextSpy = sinon.stub();
const req = new MockExpressRequest();
const res = new MockExpressResponse();
const req = httpMocks.createRequest();
const res = httpMocks.createResponse();

middleware(req, res, nextSpy);

Expand Down
7 changes: 3 additions & 4 deletions tests/unit/amo/middleware/test_frameguardMiddleware.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import MockExpressRequest from 'mock-express-request';
import MockExpressResponse from 'mock-express-response';
import httpMocks from 'node-mocks-http';

import { frameguard } from 'amo/middleware';

Expand All @@ -17,8 +16,8 @@ describe(__filename, () => {
const conf = require('config');
const middleware = frameguard({ _config: conf });
const nextSpy = sinon.stub();
const req = new MockExpressRequest();
const res = new MockExpressResponse();
const req = httpMocks.createRequest();
const res = httpMocks.createResponse();
middleware(req, res, nextSpy);
expect(res.get('x-frame-options')).toEqual('DENY');
sinon.assert.calledOnce(nextSpy);
Expand Down
15 changes: 7 additions & 8 deletions tests/unit/amo/middleware/test_requestId.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
/**
* @jest-environment node
*/
import MockExpressRequest from 'mock-express-request';
import MockExpressResponse from 'mock-express-response';
import httpMocks from 'node-mocks-http';

import { AMO_REQUEST_ID_HEADER } from 'amo/constants';
import requestId from 'amo/middleware/requestId';

describe(__filename, () => {
function _requestId({
req = new MockExpressRequest(),
res = new MockExpressResponse(),
req = httpMocks.createRequest(),
res = httpMocks.createResponse(),
next = sinon.stub(),
_httpContext = {
set: sinon.stub(),
Expand All @@ -20,7 +19,7 @@ describe(__filename, () => {
}

it('adds a generated request ID to the HTTP context and response', () => {
const res = new MockExpressResponse();
const res = httpMocks.createResponse();
const next = sinon.stub();
const _httpContext = {
set: sinon.stub(),
Expand All @@ -38,8 +37,8 @@ describe(__filename, () => {
});

it('uses the request ID from the request when available', () => {
const req = new MockExpressRequest();
const res = new MockExpressResponse();
const req = httpMocks.createRequest();
const res = httpMocks.createResponse();
const _httpContext = {
set: sinon.stub(),
};
Expand All @@ -54,7 +53,7 @@ describe(__filename, () => {
});

it('ensures the request has AMO_REQUEST_ID_HEADER', () => {
const req = new MockExpressRequest();
const req = httpMocks.createRequest();
expect(req.get(AMO_REQUEST_ID_HEADER)).not.toBeDefined();

_requestId({ req });
Expand Down
35 changes: 20 additions & 15 deletions tests/unit/amo/utils/test_server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import path from 'path';
import EventEmitter from 'events';

import fs from 'fs-extra';
import MockExpressResponse from 'mock-express-response';
import httpMocks from 'node-mocks-http';

import {
viewFrontendVersionHandler,
Expand All @@ -19,16 +20,16 @@ describe(__filename, () => {
const _config = getFakeConfig({ basePath });
const handler = viewFrontendVersionHandler({ _config });

const res = new MockExpressResponse();
const res = httpMocks.createResponse({
eventEmitter: EventEmitter,
});
handler(null, res);

res.on('finish', () => {
res.on('end', () => {
expect(res.statusCode).toEqual(200);
expect(res.get('content-type')).toEqual(
'application/json; charset=utf-8',
);
expect(res.get('content-type')).toEqual('application/json');
expect(res.get('access-control-allow-origin')).toEqual('*');
expect(res._getJSON()).toMatchObject(versionJson);
expect(res._getJSONData()).toMatchObject(versionJson);

done();
});
Expand All @@ -50,11 +51,13 @@ describe(__filename, () => {
);
const handler = viewFrontendVersionHandler({ _config });

const res = new MockExpressResponse();
const res = httpMocks.createResponse({
eventEmitter: EventEmitter,
});
handler(null, res);

res.on('finish', () => {
expect(res._getJSON()).toMatchObject({
res.on('end', () => {
expect(res._getJSONData()).toMatchObject({
...versionJson,
experiments,
feature_flags: {
Expand All @@ -73,10 +76,12 @@ describe(__filename, () => {

const handler = viewFrontendVersionHandler({ _config, _log });

const res = new MockExpressResponse();
const res = httpMocks.createResponse({
eventEmitter: EventEmitter,
});
handler(null, res);

res.on('finish', () => {
res.on('end', () => {
expect(res.statusCode).toEqual(415);
sinon.assert.calledOnce(_log.error);

Expand All @@ -94,7 +99,7 @@ describe(__filename, () => {
const _fetch = jest.fn().mockResolvedValue({ status: 200 });
const handler = viewHeartbeatHandler({ _config, _fetch });

const res = new MockExpressResponse();
const res = httpMocks.createResponse();
await handler(null, res);

expect(_fetch).toHaveBeenCalledWith(
Expand All @@ -107,7 +112,7 @@ describe(__filename, () => {
const _fetch = jest.fn().mockResolvedValue({ status: 400 });
const handler = viewHeartbeatHandler({ _fetch });

const res = new MockExpressResponse();
const res = httpMocks.createResponse();
await handler(null, res);

expect(res.statusCode).toEqual(500);
Expand All @@ -117,7 +122,7 @@ describe(__filename, () => {
const _fetch = jest.fn().mockRejectedValue();
const handler = viewHeartbeatHandler({ _fetch });

const res = new MockExpressResponse();
const res = httpMocks.createResponse();
await handler(null, res);

expect(res.statusCode).toEqual(500);
Expand Down
Loading