/
MockResponse.ts
82 lines (75 loc) · 2.11 KB
/
MockResponse.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import {MockHeaders} from './MockHeaders';
import EventTarget from './MockEventTarget';
import MockProgressEvent from './MockProgressEvent';
export default class MockResponse {
private _status: number = 200;
private _reason: string = 'OK';
private _headers: MockHeaders = {};
private _body: any = null;
status(): number;
status(status: number): MockResponse;
status(status?: number): number | MockResponse {
if (typeof status !== 'undefined') {
this._status = status;
return this;
} else {
return this._status;
}
}
reason(): string;
reason(reason: string): MockResponse;
reason(reason?: string): string | MockResponse {
if (typeof reason !== 'undefined') {
this._reason = reason;
return this;
} else {
return this._reason;
}
}
statusText(): null | string;
statusText(reason: string): MockResponse;
statusText(reason?: string): null | string | MockResponse {
console.warn(
'xhr-mock: MockResponse.statusText() has been deprecated. Use MockResponse.reason() instead.'
);
if (typeof reason !== 'undefined') {
return this.reason(reason);
} else {
return this.reason();
}
}
header(name: string): null | string;
header(name: string, value: string): MockResponse;
header(name: string, value?: string): null | string | MockResponse {
if (typeof value !== 'undefined') {
this._headers[name.toLowerCase()] = value;
return this;
} else {
return this._headers[name.toLowerCase()] || null;
}
}
headers(): MockHeaders;
headers(headers: MockHeaders): MockResponse;
headers(headers?: MockHeaders): MockHeaders | MockResponse {
if (typeof headers === 'object') {
for (let name in headers) {
if (headers.hasOwnProperty(name)) {
this.header(name, headers[name]);
}
}
return this;
} else {
return this._headers;
}
}
body(): any;
body(body: any): MockResponse;
body(body?: any): any | MockResponse {
if (typeof body !== 'undefined') {
this._body = body;
return this;
} else {
return this._body;
}
}
}