All files / src fakeServer.ts

96.3% Statements 26/27
90% Branches 9/10
100% Functions 2/2
96.3% Lines 26/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 521x             1x 1x   1x 62x   62x     111x 111x 2x     111x 111x 3x   108x       108x   108x 106x     108x       108x 7x 7x 7x 7x 7x 7x 7x   101x      
import {
    RequestInit,
    Response,
    ResponseInit,
} from "node-fetch";
import { IRequestContext } from "./httpClient";
import RequestResponseLogEntry from "./requestResponseLogEntry";
import Logger from "./logger";
const log: Logger = new Logger();
 
export default class FakeServer {
    public fakeResponses: RequestResponseLogEntry[] = [];
    public constructor(fakeResponses: RequestResponseLogEntry[]) {
        this.fakeResponses = fakeResponses;
    }
    public async getFakeHttpResponse(url: string, requestInit: RequestInit, expectedHttpStatusCode: number[], context: IRequestContext): Promise<Response> {
        log.debug("getFakeHttpResponse");
        if (!requestInit.method) {
            requestInit.method = "UNDEFINED";
        }
 
        const rrEntry: RequestResponseLogEntry | undefined = this.fakeResponses.shift();
        if (!rrEntry) {
            throw new Error(`error providing fake http response. No fake response available`);
        }
        const responseInit: ResponseInit = {
            status: rrEntry.response.status,
        };
 
        const response: Response = new Response(rrEntry.response.body, responseInit);
 
        if (rrEntry.response.contentType) {
            response.headers.append("Content-Type", rrEntry.response.contentType);
        }
 
        Iif (rrEntry.response.contentLocation) {
            response.headers.append("Content-Location", rrEntry.response.contentLocation);
        }
 
        if (expectedHttpStatusCode.indexOf(response.status) === -1) {
            log.debug("getHttpResponse unexpected status response ", response.status + " " + response.statusText);
            log.debug("getHttpResponse description ", context.description);
            log.debug("getHttpResponse expected ", expectedHttpStatusCode.join(","));
            log.debug("getHttpResponse headers ", JSON.stringify(response.headers, null, 4));
            log.debug("getHttpResponse request body ", requestInit.body);
            log.debug("getHttpResponse text ", await response.text());
            throw new Error(`HTTP response status ${response.status} not expected. Expected status: ${expectedHttpStatusCode.join(",")} - status text: ${response.statusText}`);
        }
        return response;
    }
}