Skip to content
Open
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
15 changes: 12 additions & 3 deletions packages/mock-addon/src/utils/faker.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,25 @@ export class Faker {
return global.realFetch(input, options);
}

const { response, status, delay = 0 } = matched;
const { response, status, delay = 0, responseHeaders = {} } = matched;

let mockResponseSent = false;

return new Promise((resolve, reject) => {
const timeoutId = setTimeout(() => {
if (typeof response === 'function') {
resolve(CustomResponse(url, status, response(request)));
resolve(
CustomResponse(
url,
status,
response(request),
responseHeaders
)
);
} else {
resolve(CustomResponse(url, status, response));
resolve(
CustomResponse(url, status, response, responseHeaders)
);
}

mockResponseSent = true;
Expand Down
3 changes: 2 additions & 1 deletion packages/mock-addon/src/utils/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'whatwg-fetch';
import statusTextMap from './statusMap';
import { defaultResponseHeaders } from './headers';

export function CustomResponse(url, status, responseText) {
export function CustomResponse(url, status, responseText, headers) {
const text =
typeof responseText === 'string'
? responseText
Expand All @@ -14,6 +14,7 @@ export function CustomResponse(url, status, responseText) {
statusText: statusTextMap[status.toString()],
headers: new Headers({
...defaultResponseHeaders,
...headers,
}),
url,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/mock-addon/src/utils/validator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import statusTextMap from '../utils/statusMap';

const methods = ['GET', 'PUT', 'POST', 'DELETE', 'PATCH', 'OPTIONS'];
const methods = ['GET', 'PUT', 'POST', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD'];
const statusCodes = Object.keys(statusTextMap);

const isObject = (value) =>
Expand Down