A Swift library for encoding, decoding, recording and testing using the HTTP Archive format.
The main HAR
module includes the functionality to read and write .har
files from disk.
import HAR
let archive = try HAR(contentsOf: URL(fileURLWithPath: "/path/to/example.har"))
archive.log.entries[0].request.url
try archive.write(to: URL(fileURLWithPath: "/other/example.har"))
The HARNetworking
has additional functionality around the URLSession
, URLRequest
and HTTPURLResponse
types. (Note that on Linux, this depends FoundationNetworking
)
A new HTTP archive can be created by recording the HTTP request and response from an actual URLSession
request.
import HARNetworking
let url = URL(string: "http://example.com/")!
let request = URLRequest(url: url)
HAR.record(request: request) { (result: Result<HAR, Error>) in
let archive = try! result.get()
print(archive.log.entries[0].response.status)
}
Furthermore, HARTesting
provides networking and testing helpers for use only in a test target. (It depends on XCTest
)
import HARTesting
import XCTest
final class FooTests: XCTestCase {
func testRequest() throws {
let archiveFileURL = URL(fileURLWithPath: "/path/to/example.har")
let url = URL(string: "http://example.com")!
let urlRequest = URLRequest(url: url)
// If example.har exists, load it offline. Otherwise make an online request and save it as an archive.
let result = waitForHTTPURLRequest(urlRequest, mockedWith: archiveFileURL)
let (data, response) = try result.get()
XCTAssertEqual(response.statusCode, 200)
XCTAssertEqual(data.count, 1256)
}
}