-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ios: update unary/streaming & add test (#354)
Per the discussion [here](envoyproxy/envoy-mobile#312 (comment)), updating the unary and streaming interfaces. - Moved the non-streaming convenience function into an extension on the protocol type so that it's available to all consumers - Added a test to validate the default behavior of this extension function - Added a `CancelableStream` protocol which includes a subset of functionality from the `StreamEmitter`, allowing consumers of the unary function to cancel requests without having the ability to send additional data into the stream Signed-off-by: Michael Rebello <[email protected]> Signed-off-by: JP Simard <[email protected]>
- Loading branch information
Showing
6 changed files
with
107 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import Envoy | ||
import Foundation | ||
import XCTest | ||
|
||
private final class MockStreamEmitter: StreamEmitter { | ||
var onData: ((Data?) -> Void)? | ||
var onTrailers: (([String: [String]]) -> Void)? | ||
|
||
func sendData(_ data: Data) -> StreamEmitter { | ||
self.onData?(data) | ||
return self | ||
} | ||
|
||
func sendMetadata(_ metadata: [String: [String]]) -> StreamEmitter { | ||
return self | ||
} | ||
|
||
func close(trailers: [String: [String]]) { | ||
self.onTrailers?(trailers) | ||
} | ||
|
||
func cancel() {} | ||
} | ||
|
||
private final class MockClient: Client { | ||
var onRequest: ((Request) -> Void)? | ||
var onData: ((Data?) -> Void)? | ||
var onTrailers: (([String: [String]]) -> Void)? | ||
|
||
func send(_ request: Request, handler: ResponseHandler) -> StreamEmitter { | ||
self.onRequest?(request) | ||
let emitter = MockStreamEmitter() | ||
emitter.onData = self.onData | ||
emitter.onTrailers = self.onTrailers | ||
return emitter | ||
} | ||
} | ||
|
||
final class ClientTests: XCTestCase { | ||
func testNonStreamingExtensionSendsRequestDetailsThroughStream() { | ||
let requestExpectation = self.expectation(description: "Sends request") | ||
let dataExpectation = self.expectation(description: "Sends data") | ||
let closeExpectation = self.expectation(description: "Calls close") | ||
|
||
let expectedRequest = RequestBuilder( | ||
method: .get, scheme: "https", authority: "www.envoyproxy.io", path: "/docs") | ||
.build() | ||
let expectedData = Data([1, 2, 3]) | ||
let expectedTrailers = ["foo": ["bar", "baz"]] | ||
|
||
let mockClient = MockClient() | ||
mockClient.onRequest = { request in | ||
XCTAssertEqual(expectedRequest, request) | ||
requestExpectation.fulfill() | ||
} | ||
|
||
mockClient.onData = { data in | ||
XCTAssertEqual(expectedData, data) | ||
dataExpectation.fulfill() | ||
} | ||
|
||
mockClient.onTrailers = { trailers in | ||
XCTAssertEqual(expectedTrailers, trailers) | ||
closeExpectation.fulfill() | ||
} | ||
|
||
mockClient.send(expectedRequest, data: expectedData, trailers: expectedTrailers, | ||
handler: ResponseHandler()) | ||
self.wait(for: [requestExpectation, dataExpectation, closeExpectation], | ||
timeout: 0.1, enforceOrder: true) | ||
} | ||
} |