@ -7,7 +7,7 @@
|
||||
*/
|
||||
|
||||
export class MockBody implements Body {
|
||||
readonly body !: ReadableStream;
|
||||
readonly body!: ReadableStream;
|
||||
bodyUsed: boolean = false;
|
||||
|
||||
constructor(public _body: string|null) {}
|
||||
@ -24,13 +24,21 @@ export class MockBody implements Body {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
async blob(): Promise<Blob> { throw 'Not implemented'; }
|
||||
async blob(): Promise<Blob> {
|
||||
throw 'Not implemented';
|
||||
}
|
||||
|
||||
async json(): Promise<any> { return JSON.parse(this.getBody()); }
|
||||
async json(): Promise<any> {
|
||||
return JSON.parse(this.getBody());
|
||||
}
|
||||
|
||||
async text(): Promise<string> { return this.getBody(); }
|
||||
async text(): Promise<string> {
|
||||
return this.getBody();
|
||||
}
|
||||
|
||||
async formData(): Promise<FormData> { throw 'Not implemented'; }
|
||||
async formData(): Promise<FormData> {
|
||||
throw 'Not implemented';
|
||||
}
|
||||
|
||||
private getBody(): string {
|
||||
if (this.bodyUsed === true) {
|
||||
@ -47,31 +55,51 @@ export class MockBody implements Body {
|
||||
export class MockHeaders implements Headers {
|
||||
map = new Map<string, string>();
|
||||
|
||||
[Symbol.iterator]() { return this.map[Symbol.iterator](); }
|
||||
[Symbol.iterator]() {
|
||||
return this.map[Symbol.iterator]();
|
||||
}
|
||||
|
||||
append(name: string, value: string): void { this.map.set(name.toLowerCase(), value); }
|
||||
append(name: string, value: string): void {
|
||||
this.map.set(name.toLowerCase(), value);
|
||||
}
|
||||
|
||||
delete (name: string): void { this.map.delete(name.toLowerCase()); }
|
||||
delete(name: string): void {
|
||||
this.map.delete(name.toLowerCase());
|
||||
}
|
||||
|
||||
entries() { return this.map.entries(); }
|
||||
entries() {
|
||||
return this.map.entries();
|
||||
}
|
||||
|
||||
forEach(callback: Function): void { this.map.forEach(callback as any); }
|
||||
forEach(callback: Function): void {
|
||||
this.map.forEach(callback as any);
|
||||
}
|
||||
|
||||
get(name: string): string|null { return this.map.get(name.toLowerCase()) || null; }
|
||||
get(name: string): string|null {
|
||||
return this.map.get(name.toLowerCase()) || null;
|
||||
}
|
||||
|
||||
has(name: string): boolean { return this.map.has(name.toLowerCase()); }
|
||||
has(name: string): boolean {
|
||||
return this.map.has(name.toLowerCase());
|
||||
}
|
||||
|
||||
keys() { return this.map.keys(); }
|
||||
keys() {
|
||||
return this.map.keys();
|
||||
}
|
||||
|
||||
set(name: string, value: string): void { this.map.set(name.toLowerCase(), value); }
|
||||
set(name: string, value: string): void {
|
||||
this.map.set(name.toLowerCase(), value);
|
||||
}
|
||||
|
||||
values() { return this.map.values(); }
|
||||
values() {
|
||||
return this.map.values();
|
||||
}
|
||||
}
|
||||
|
||||
export class MockRequest extends MockBody implements Request {
|
||||
readonly isHistoryNavigation: boolean = false;
|
||||
readonly isReloadNavigation: boolean = false;
|
||||
readonly body !: ReadableStream;
|
||||
readonly body!: ReadableStream;
|
||||
readonly cache: RequestCache = 'default';
|
||||
readonly credentials: RequestCredentials = 'omit';
|
||||
readonly destination: RequestDestination = 'document';
|
||||
@ -88,17 +116,19 @@ export class MockRequest extends MockBody implements Request {
|
||||
url: string;
|
||||
|
||||
constructor(input: string|Request, init: RequestInit = {}) {
|
||||
super(init !== undefined ? (init.body as(string | null)) || null : null);
|
||||
super(init !== undefined ? (init.body as (string | null)) || null : null);
|
||||
if (typeof input !== 'string') {
|
||||
throw 'Not implemented';
|
||||
}
|
||||
this.url = input;
|
||||
const headers = init.headers as{[key: string]: string};
|
||||
const headers = init.headers as {[key: string]: string};
|
||||
if (headers !== undefined) {
|
||||
if (headers instanceof MockHeaders) {
|
||||
this.headers = headers;
|
||||
} else {
|
||||
Object.keys(headers).forEach(header => { this.headers.set(header, headers[header]); });
|
||||
Object.keys(headers).forEach(header => {
|
||||
this.headers.set(header, headers[header]);
|
||||
});
|
||||
}
|
||||
}
|
||||
if (init.cache !== undefined) {
|
||||
@ -128,7 +158,9 @@ export class MockRequest extends MockBody implements Request {
|
||||
export class MockResponse extends MockBody implements Response {
|
||||
readonly trailer: Promise<Headers> = Promise.resolve(new MockHeaders());
|
||||
readonly headers: Headers = new MockHeaders();
|
||||
get ok(): boolean { return this.status >= 200 && this.status < 300; }
|
||||
get ok(): boolean {
|
||||
return this.status >= 200 && this.status < 300;
|
||||
}
|
||||
readonly status: number;
|
||||
readonly statusText: string;
|
||||
readonly type: ResponseType = 'basic';
|
||||
@ -141,12 +173,14 @@ export class MockResponse extends MockBody implements Response {
|
||||
super(typeof body === 'string' ? body : null);
|
||||
this.status = (init.status !== undefined) ? init.status : 200;
|
||||
this.statusText = init.statusText || 'OK';
|
||||
const headers = init.headers as{[key: string]: string};
|
||||
const headers = init.headers as {[key: string]: string};
|
||||
if (headers !== undefined) {
|
||||
if (headers instanceof MockHeaders) {
|
||||
this.headers = headers;
|
||||
} else {
|
||||
Object.keys(headers).forEach(header => { this.headers.set(header, headers[header]); });
|
||||
Object.keys(headers).forEach(header => {
|
||||
this.headers.set(header, headers[header]);
|
||||
});
|
||||
}
|
||||
}
|
||||
if (init.type !== undefined) {
|
||||
|
Reference in New Issue
Block a user