Skip to content

Commit 045dc6d

Browse files
author
Nicklas Utgaard
committed
feat(response-utils): Added helper-function to add headers to response
1 parent a63fb6b commit 045dc6d

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/response-utils.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export default class ResponseUtils {
5353
}
5454

5555
static jsonPromise(json: MockHandler): Promise<ResponseData> {
56-
const response: ResponseData = { body: JSON.stringify(json) } as ResponseData;
56+
const response: ResponseData = { body: JSON.stringify(json) };
5757
return Promise.resolve(response);
5858
}
5959

@@ -67,14 +67,21 @@ export default class ResponseUtils {
6767

6868
static statusCode(status: number): MockHandler {
6969
return (args: HandlerArgument) => {
70-
const response: ResponseData = { status } as ResponseData;
70+
const response: ResponseData = { status };
7171
return Promise.resolve(response);
7272
};
7373
}
7474

7575
static statusText(statusText: string): MockHandler {
7676
return (args: HandlerArgument) => {
77-
const response: ResponseData = { statusText } as ResponseData;
77+
const response: ResponseData = { statusText };
78+
return Promise.resolve(response);
79+
};
80+
}
81+
82+
static headers(headers: { [key: string]: string }): MockHandler {
83+
return (args: HandlerArgument) => {
84+
const response: ResponseData = { headers };
7885
return Promise.resolve(response);
7986
};
8087
}

test/yet-another-fetch-mock.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,21 @@ describe('FetchMock', () => {
219219
Promise.all([first, second]).then(() => done());
220220
});
221221

222+
it('should be able to set headers', done => {
223+
mock.get(
224+
'/withheaders',
225+
ResponseUtils.combine(
226+
ResponseUtils.json({ key: 'value' }),
227+
ResponseUtils.headers({ 'Content-Type': 'custom/type' })
228+
)
229+
);
230+
231+
fetch('/withheaders').then(resp => {
232+
expect(resp.headers.get('Content-Type')).toBe('custom/type');
233+
done();
234+
});
235+
});
236+
222237
it('should support lowercase httpverb', done => {
223238
mock.post('/lowercase', { key: 'BIG-CASE' });
224239

0 commit comments

Comments
 (0)