Hearken Documentation Beta

Class Hearken​Test​Case

open class HearkenTestCase: XCTestCase

An XCTestCase subclass that instantiates a Server for each test scenario.

Create your own subclass of HearkenTestCase in your tests:

import HearkenTestKit

class MyTestCase: HearkenTestCase {
    ...
}

Use the assert method to check that the tested application makes the correct request:

func testSomething() {
    assert(request: .init(method: .POST,
                          uri: "/api/test",
                          body: "Body",
           response: .init(status: .ok))
    ...
}

Conforms To

XCTestCase

Properties

server

let server

The Server to be used in each test scenario.

The Server is started in setUp() and stopped in tearDown(), failing the test if something goes wrong.

Methods

set​Up()

override open func setUp()

Provides an opportunity to reset state before each test method in a test case is called.

The setUp() instance method is called once before each test begins. Override this method to reset state for each test method. The Server is started in this method.

tear​Down()

override open func tearDown()

Provides an opportunity to perform cleanup after each test method in a test case ends.

The tearDown() instance method is called once after each test completes. Override this method to perform any per-test cleanup. The Server is stopped in this method.

assert(request:​response:​headers​Must​Match:​)

public func assert(request: HTTPRequest, response: HTTPResponse, headersMustMatch: Bool = false)

Asserts that the request provided and the actually received request match.

If the request has the Content-Type: application/json header, the body is parsed into a JSON and then matched. Similarly if it has Content-Type: application/x-www-form-urlencoded, the body is parsed into a Dictionary and then matched. Otherwise the raw Data is matched.

Networking libraries usually add some headers to the requests even if the application did not so headersMustMatch defaults to false.

Parameters

request HTTPRequest
  • request: The HTTPRequest to match with the received request.
response HTTPResponse
  • response: The HTTPResponse to be returned.
headers​Must​Match Bool
  • headersMustMatch: Enforce the matching of the HTTPHeaders. Defaults to false.

decode(json:​)

func decode(json body: Data) -> [String: AnyHashable]

decode(url​Encoded​Form:​)

private func decode(urlEncodedForm body: Data) -> [String: String]