Skip to content

Commit

Permalink
Mark RequestInterceptor closure callback arguments escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
cysp committed Mar 17, 2019
1 parent f57428f commit 107f72c
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Source/RequestInterceptor.swift
Expand Up @@ -101,8 +101,8 @@ extension RequestInterceptor {
}
}

public typealias AdaptHandler = (URLRequest, Session, _ completion: (Result<URLRequest>) -> Void) -> Void
public typealias RetryHandler = (Request, Session, Error, _ completion: (RetryResult) -> Void) -> Void
public typealias AdaptHandler = (URLRequest, Session, _ completion: @escaping (Result<URLRequest>) -> Void) -> Void
public typealias RetryHandler = (Request, Session, Error, _ completion: @escaping (RetryResult) -> Void) -> Void

// MARK: -

Expand Down
119 changes: 119 additions & 0 deletions Tests/RequestInterceptorTests.swift
Expand Up @@ -117,6 +117,36 @@ final class AdapterTestCase: BaseTestCase {
// Then
XCTAssertEqual(result, .doNotRetry)
}

func testThatAdapterCanBeImplementedAsynchronously() {
// Given
let urlRequest = URLRequest(url: URL(string: "https://httpbin.org/get")!)
let session = Session()
var adapted = false

let adapter = Adapter { request, _, completion in
adapted = true
DispatchQueue.main.async {
completion(.success(request))
}
}

var result: Result<URLRequest>!

let completesExpectation = expectation(description: "adapter completes")

// When
adapter.adapt(urlRequest, for: session) {
result = $0
completesExpectation.fulfill()
}

waitForExpectations(timeout: timeout)

// Then
XCTAssertTrue(adapted)
XCTAssertTrue(result.isSuccess)
}
}

// MARK: -
Expand Down Expand Up @@ -161,6 +191,37 @@ final class RetrierTestCase: BaseTestCase {
// Then
XCTAssertTrue(result.isSuccess)
}

func testThatRetrierCanBeImplementedAsynchronously() {
// Given
let url = URL(string: "https://httpbin.org/get")!
let session = Session(startRequestsImmediately: false)
let request = session.request(url)
var retried = false

let retrier = Retrier { request, session, error, completion in
retried = true
DispatchQueue.main.async {
completion(.retry)
}
}

var result: RetryResult!

let completesExpectation = expectation(description: "retrier completes")

// When
retrier.retry(request, for: session, dueTo: MockError()) {
result = $0
completesExpectation.fulfill()
}

waitForExpectations(timeout: timeout)

// Then
XCTAssertTrue(retried)
XCTAssertEqual(result, .retry)
}
}

// MARK: -
Expand Down Expand Up @@ -258,6 +319,35 @@ final class InterceptorTestCase: BaseTestCase {
XCTAssertTrue(result.error is MockError)
}

func testThatInterceptorCanAdaptRequestAsynchronously() {
// Given
let urlRequest = URLRequest(url: URL(string: "https://httpbin.org/get")!)
let session = Session()

let adapter = Adapter { urlRequest, _, completion in
DispatchQueue.main.async {
completion(.failure(MockError()))
}
}
let interceptor = Interceptor(adapters: [adapter])

var result: Result<URLRequest>!

let completesExpectation = expectation(description: "interceptor completes")

// When
interceptor.adapt(urlRequest, for: session) {
result = $0
completesExpectation.fulfill()
}

waitForExpectations(timeout: timeout)

// Then
XCTAssertTrue(result.isFailure)
XCTAssertTrue(result.error is MockError)
}

func testThatInterceptorCanRetryRequestWithNoRetriers() {
// Given
let url = URL(string: "https://httpbin.org/get")!
Expand Down Expand Up @@ -312,6 +402,35 @@ final class InterceptorTestCase: BaseTestCase {
XCTAssertEqual(result, .retry)
}

func testThatInterceptorCanRetryRequestAsynchronously() {
// Given
let url = URL(string: "https://httpbin.org/get")!
let session = Session(startRequestsImmediately: false)
let request = session.request(url)

let retrier = Retrier { _, _, _, completion in
DispatchQueue.main.async {
completion(.retry)
}
}
let interceptor = Interceptor(retriers: [retrier])

var result: RetryResult!

let completesExpectation = expectation(description: "interceptor completes")

// When
interceptor.retry(request, for: session, dueTo: MockError()) {
result = $0
completesExpectation.fulfill()
}

waitForExpectations(timeout: timeout)

// Then
XCTAssertEqual(result, .retry)
}

func testThatInterceptorStopsIteratingThroughPendingRetriersWithRetryResult() {
// Given
let url = URL(string: "https://httpbin.org/get")!
Expand Down

0 comments on commit 107f72c

Please sign in to comment.