Skip to content

Commit 93de326

Browse files
authored
MockRequestExecutor runRequest (#517)
### Motivation Our actual HTTPRequestExecutors send out an HTTPRequest as soon as they see it and they call `willExecuteRequest` and `requestHeadSent` in very fast succession on the `HTTPExecutableRequest`. We should have the same behavior in tests. ### Changes - Call `requestHeadSent` from `MockRequestExecutor.runRequest` - Tests that use `MockRequestExecutor` call `runRequest` on it first ### Result Cleaner, more life like tests
1 parent 474b23d commit 93de326

File tree

2 files changed

+9
-15
lines changed

2 files changed

+9
-15
lines changed

Tests/AsyncHTTPClientTests/Mocks/MockRequestExecutor.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ final class MockRequestExecutor {
8888
precondition(self.request == nil)
8989
self.request = request
9090
request.willExecuteRequest(self)
91+
request.requestHeadSent()
9192
}
9293

9394
func receiveRequestBody(deadline: NIODeadline = .now() + .seconds(60), _ verify: (ByteBuffer) throws -> Void) throws {
@@ -166,7 +167,7 @@ final class MockRequestExecutor {
166167
}
167168
}
168169

169-
func resetResponseStreamDemandSignal0() {
170+
private func resetResponseStreamDemandSignal0() {
170171
self._signaledDemandForResponseBody = false
171172
}
172173
}

Tests/AsyncHTTPClientTests/RequestBagTests.swift

+7-14
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,10 @@ final class RequestBagTests: XCTestCase {
7676
eventLoop: embeddedEventLoop
7777
)
7878

79-
executor.runRequest(bag)
80-
8179
XCTAssertEqual(delegate.hitDidSendRequestHead, 0)
82-
bag.requestHeadSent()
80+
executor.runRequest(bag)
8381
XCTAssertEqual(delegate.hitDidSendRequestHead, 1)
82+
8483
streamIsAllowedToWrite = true
8584
bag.resumeRequestBodyStream()
8685
streamIsAllowedToWrite = false
@@ -180,10 +179,8 @@ final class RequestBagTests: XCTestCase {
180179

181180
let executor = MockRequestExecutor(eventLoop: embeddedEventLoop)
182181

183-
bag.willExecuteRequest(executor)
184-
185182
XCTAssertEqual(delegate.hitDidSendRequestHead, 0)
186-
bag.requestHeadSent()
183+
executor.runRequest(bag)
187184
XCTAssertEqual(delegate.hitDidSendRequestHead, 1)
188185
XCTAssertEqual(delegate.hitDidSendRequestPart, 0)
189186
bag.resumeRequestBodyStream()
@@ -256,12 +253,11 @@ final class RequestBagTests: XCTestCase {
256253

257254
let executor = MockRequestExecutor(eventLoop: embeddedEventLoop)
258255

259-
bag.willExecuteRequest(executor)
260256
XCTAssertFalse(executor.isCancelled)
261257

262258
XCTAssertEqual(delegate.hitDidSendRequestHead, 0)
263259
XCTAssertEqual(delegate.hitDidSendRequest, 0)
264-
bag.requestHeadSent()
260+
executor.runRequest(bag)
265261
XCTAssertEqual(delegate.hitDidSendRequestHead, 1)
266262
XCTAssertEqual(delegate.hitDidSendRequest, 1)
267263

@@ -330,8 +326,7 @@ final class RequestBagTests: XCTestCase {
330326
guard let bag = maybeRequestBag else { return XCTFail("Expected to be able to create a request bag.") }
331327

332328
let executor = MockRequestExecutor(eventLoop: embeddedEventLoop)
333-
bag.willExecuteRequest(executor)
334-
bag.requestHeadSent()
329+
executor.runRequest(bag)
335330
bag.receiveResponseHead(.init(version: .http1_1, status: .ok))
336331
XCTAssertEqual(executor.isCancelled, false)
337332
bag.fail(HTTPClientError.readTimeout)
@@ -387,11 +382,10 @@ final class RequestBagTests: XCTestCase {
387382
guard let bag = maybeRequestBag else { return XCTFail("Expected to be able to create a request bag.") }
388383

389384
let executor = MockRequestExecutor(eventLoop: embeddedEventLoop)
390-
bag.willExecuteRequest(executor)
391385

392386
XCTAssertEqual(delegate.hitDidSendRequestHead, 0)
393387
XCTAssertEqual(delegate.hitDidSendRequest, 0)
394-
bag.requestHeadSent()
388+
executor.runRequest(bag)
395389
XCTAssertEqual(delegate.hitDidSendRequestHead, 1)
396390
XCTAssertEqual(delegate.hitDidSendRequest, 0)
397391

@@ -432,8 +426,7 @@ final class RequestBagTests: XCTestCase {
432426
guard let bag = maybeRequestBag else { return XCTFail("Expected to be able to create a request bag.") }
433427

434428
let executor = MockRequestExecutor(eventLoop: embeddedEventLoop)
435-
bag.willExecuteRequest(executor)
436-
bag.requestHeadSent()
429+
executor.runRequest(bag)
437430
bag.receiveResponseHead(.init(version: .http1_1, status: .ok))
438431
XCTAssertFalse(executor.signalledDemandForResponseBody)
439432
XCTAssertNoThrow(try XCTUnwrap(delegate.backpressurePromise).succeed(()))

0 commit comments

Comments
 (0)