-
Notifications
You must be signed in to change notification settings - Fork 7
/
mocking-response-elements.ts
59 lines (49 loc) · 2.12 KB
/
mocking-response-elements.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import fetch from 'node-fetch';
import getStream from 'get-stream';
import intoStream from 'into-stream';
import {FakeServer} from '../src';
const port = 4444;
let fakeServer: FakeServer;
beforeEach(() => {
fakeServer = new FakeServer(port);
fakeServer.start();
});
afterEach(() => {
fakeServer.stop();
});
[{useNewApi: true}, {useNewApi: false}].forEach(({useNewApi}) => {
describe('Route Matching - willReturn Response Elements', () => {
test('GET route defined with mocked body and response headers', async () => {
const path = '/somePath';
let route;
if (useNewApi) {
route = fakeServer.get(path).willReturn({name: 'Cloud'}, 200, {imontop: 'Of The World'});
} else {
route = fakeServer.get().to(path).willReturn({name: 'Cloud'}, 200, {imontop: 'Of The World'});
}
const res = await fetch(`http://localhost:${port}${path}`, {method: 'GET'});
const body = await res.json();
expect(body.name).toEqual('Cloud');
expect(res.status).toEqual(200);
expect(res.headers.get('imontop')).toEqual('Of The World');
expect(fakeServer.didReceive(route.call)).toEqual(true);
});
test('GET route defined with response as stream', async () => {
const stringTobeStreamed = 'Rivers are huge Streams';
const bodyAsStream = intoStream(stringTobeStreamed);
const path = '/somePath';
let route;
if (useNewApi) {
route = fakeServer.get(path).willReturn(bodyAsStream);
} else {
route = fakeServer.get().to(path).willReturn(bodyAsStream);
}
const res = await fetch(`http://localhost:${port}${path}`, {method: 'GET'});
expect(res.headers.get('content-type')).toEqual('application/octet-stream');
expect(res.status).toEqual(200);
expect(fakeServer.didReceive(route.call)).toEqual(true);
const body = await getStream(res.body);
expect(body).toEqual(stringTobeStreamed);
});
});
});