Class HttpMockBuilder

Methods

  • Mocks responses for get methods You may pass a specific endpoint as parameter to mock only selected endpoints. This is very useful, when having methods that do several Http requests, so you can mock them one on one.

    The following code returns the same content on every get call

      HttpMockBuilder
    .create()
    .onGetReply(200, {response: 'foo}) // mocks all get requests
    .onPostReply(201, {response: 'foo}) // mocks all post requests
    .build()

    The next code returns the different content depending on the passed endpoint

      HttpMockBuilder
    .create()
    .onGetReply(200, {response: 'foo}, '/url/specific') // mocks get request for '/url/specific'
    .build()

    Parameters

    • status: number

      {number} The status to be returned

    • data: any

      The data to be returned

    • Optionalurl: string

      {string?} If given, the mock applies for that specific url, other for all method calls

    Returns HttpMockBuilder

    The builder instance (Fluent API)

  • Mocks response exceptions for get methods. It works like onGetReply(), but throws an HttpError instead

    Parameters

    • status: number

      {number} The status to be returned in exception object

    • errorMessage: string

      {string} The error message

    • data: any = null

      {any?} Eventual data carried with the error object

    • url: string = HttpMock.ForAll

      {string?} The specific url for which the exception should be thrown

    Returns HttpMockBuilder

    The builder instance (Fluent API)