-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmock.js
56 lines (51 loc) · 1.22 KB
/
mock.js
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
import { Headers, Response } from 'isomorphic-fetch'
import stream from 'stream'
export default class Mock {
constructor(application, configuration) {
this.application = application;
this.body = configuration.body;
this.headers = configuration.headers;
this.size = configuration.size;
this.status = configuration.status;
this.timeout = configuration.timeout;
}
call(environment) {
return Promise.resolve(
new Response(
this._buildReadableStream(),
{
headers: this._getHeaders(),
size: this.size,
status: this._getStatus(),
timeout: this.timeout
}
)
);
}
/**
* @return {stream.Readable}
*/
_buildReadableStream() {
let readableStream = new stream.Readable();
readableStream._read = () => {};
readableStream.push(this.body);
readableStream.push(null);
return readableStream;
}
/**
* @return {fetch.Headers}
*/
_getHeaders() {
let headers = new Headers(this.headers || {});
if (headers.has('Content-Length')) {
headers.set('Content-Length', this.body.length);
}
return headers;
}
/**
* @return {Integer}
*/
_getStatus() {
return this.status || 200;
}
}