All files / src/api request-expectation.ts

0% Statements 0/15
0% Branches 0/2
0% Functions 0/10
0% Lines 0/15

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 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102                                                                                                                                                                                                           
import deepEqual from "deep-equal";
import { ValidationError } from "../api";
import { RequestExt } from "../server";
import {
  validateRawBodyEquals,
  validateBodyEquals,
  validateBodyEmpty,
  validateBodyNotEmpty,
  validateCoercedDateBodyEquals,
  validateXMLBodyEquals,
  validateHeader,
  validateQueryParam,
} from "./request-validations";
 
/**
 * Class containing all the expectations that can be run on the request.
 */
export class RequestExpectation {
  public constructor(private originalRequest: RequestExt) {}
  /**
   * Expect the raw body of the request to match the given string.
   * @param rawBody Raw request body.
   * @throws {ValidationError} if there is an error.
   */
  public rawBodyEquals(expectedRawBody: string | undefined): void {
    validateRawBodyEquals(this.originalRequest, expectedRawBody);
  }
 
  /**
   * Expect the body of the request to match the given object.
   * @param rawBody Raw request body.
   * @throws {ValidationError} if there is an error.
   */
  public bodyEquals(expectedRawBody: unknown | undefined): void {
    validateBodyEquals(this.originalRequest, expectedRawBody);
  }
 
  /**
   * Expect the coerced body of the request to match the given object.
   * @param rawBody Raw request body.
   * @throws {ValidationError} if there is an error.
   */
  public coercedBodyEquals(expectedRawBody: unknown | undefined): void {
    validateCoercedDateBodyEquals(this.originalRequest, expectedRawBody);
  }
 
  /**
   * Expect the body of the request to be empty.
   * @throws {ValidationError} if there is an error.
   */
  public bodyEmpty(): void {
    validateBodyEmpty(this.originalRequest);
  }
 
  /**
   * Expect the body of the request to be not empty.
   * @throws {ValidationError} if there is an error.
   */
  public bodyNotEmpty(): void {
    validateBodyNotEmpty(this.originalRequest);
  }
 
  /**
   * Expect the body of the request matches the XML body you expect
   * @param expectedRawBody Raw request XML body expressed in a string.
   * @throws {ValidationError} if there is an error.
   */
  public async xmlBodyEquals(expectedRawBody: string): Promise<void> {
    await validateXMLBodyEquals(this.originalRequest, expectedRawBody);
  }
 
  /**
   * Expect the header of the request contains the expected key/value pair
   * @param headerName Key expected in header
   * @param expectedValue Values expected in header
   * @throws {ValidationError} if there is an error.
   */
  public containsHeader(headerName: string, expectedValue: string): void {
    validateHeader(this.originalRequest, headerName, expectedValue);
  }
 
  /**
   * Expect the query string of the request contains the expected name/value pair.
   * @param paramName Name of the query parameter.
   * @param expectedValue Value expected of the query parameter.
   */
  public containsQueryParam(paramName: string, expectedValue: string): void {
    validateQueryParam(this.originalRequest, paramName, expectedValue);
  }
 
  /**
   * Check if two requests are equal
   * @param actual Actual value
   * @param expected Expected value
   */
  public deepEqual(actual: unknown, expected: unknown, message = "Values not deep equal"): void {
    Iif (!deepEqual(actual, expected, { strict: true })) {
      throw new ValidationError(message, actual, expected);
    }
  }
}