Options
All
  • Public
  • Public/Protected
  • All
Menu

Class HttpMockBuilder

Http Mocker Builder for easy to http testing

Example:

const mockedHttp = HttpMockBuilder
.create()
.onGetReply(200, {foo: 'get'})
.onPostThrowError(500, 'Post Error', {e: 'post'})
.onPutThrowError(404, 'Put Error', {e: 'put'})
.onDeleteThrowError(403, 'Delete Error', {e: 'delete'})
.build();

const response = await mockedHttp.get('/url');

await mockedHttp.post('/url/post', {faz: 'post'}); // will throw exception

Hierarchy

  • HttpMockBuilder

Index

Methods

build

onDeleteReply

onDeleteThrowError

  • onDeleteThrowError(status: number, errorMessage: string, data: any, url?: string): HttpMockBuilder
  • Mocks response exceptions for delete methods. It works like onDeleteReply(), but throws an HttpError instead

    Parameters

    • status: number
    • errorMessage: string
    • data: any
    • Default value url: string = HttpMock.ForAll

    Returns HttpMockBuilder

onGetReply

  • 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

      The status to be returned

    • data: any

      The data to be returned

    • Optional url: string

      If given, the mock applies for that specific url, other for all method calls

    Returns HttpMockBuilder

    The builder instance (Fluent API)

onGetThrowError

  • onGetThrowError(status: number, errorMessage: string, data?: any, url?: string): HttpMockBuilder
  • Mocks response exceptions for get methods. It works like onGetReply(), but throws an HttpError instead

    Parameters

    • status: number

      The status to be returned in exception object

    • errorMessage: string

      The error message

    • Default value data: any = null

      Eventual data carried with the error object

    • Default value url: string = HttpMock.ForAll

      The specific url for which the exception should be thrown

    Returns HttpMockBuilder

    The builder instance (Fluent API)

onPostReply

onPostThrowError

  • onPostThrowError(status: number, errorMessage: string, data: any, url?: string): HttpMockBuilder
  • Mocks response exceptions for post methods. It works like onPostReply(), but throws an HttpError instead

    Parameters

    • status: number
    • errorMessage: string
    • data: any
    • Default value url: string = HttpMock.ForAll

    Returns HttpMockBuilder

onPutReply

onPutThrowError

  • onPutThrowError(status: number, errorMessage: string, data: any, url?: string): HttpMockBuilder
  • Mocks response exceptions for put methods. It works like onPutReply(), but throws an HttpError instead

    Parameters

    • status: number
    • errorMessage: string
    • data: any
    • Default value url: string = HttpMock.ForAll

    Returns HttpMockBuilder

Static create