@testable import Envoy import Foundation import XCTest private let kMessage1 = Data([1, 2, 3, 4, 5]) private let kMessage2 = Data([6, 7, 8, 9, 0, 1]) final class GRPCStreamTests: XCTestCase { // MARK: - Request tests func testDataSizeIsFiveBytesGreaterThanMessageSize() { var sentData = Data() let streamClient = MockStreamClient { stream in stream.onRequestData = { data, _ in sentData.append(data) } } _ = GRPCClient(streamClient: streamClient) .newGRPCStreamPrototype() .start() .sendMessage(kMessage1) XCTAssertEqual(5 + kMessage1.count, sentData.count) } func testPrefixesSentDataWithZeroCompressionFlag() { var sentData = Data() let streamClient = MockStreamClient { stream in stream.onRequestData = { data, _ in sentData.append(data) } } _ = GRPCClient(streamClient: streamClient) .newGRPCStreamPrototype() .start() .sendMessage(kMessage1) XCTAssertEqual(UInt8(0), sentData.integer(atIndex: 0)) } func testPrefixesSentDataWithBigEndianLengthOfMessage() { var sentData = Data() let streamClient = MockStreamClient { stream in stream.onRequestData = { data, _ in sentData.append(data) } } _ = GRPCClient(streamClient: streamClient) .newGRPCStreamPrototype() .start() .sendMessage(kMessage1) let expectedMessageLength = UInt32(kMessage1.count).bigEndian let messageLength: UInt32? = sentData.integer(atIndex: 1) XCTAssertEqual(expectedMessageLength, messageLength) } func testAppendsMessageDataAtTheEndOfSentData() { var sentData = Data() let streamClient = MockStreamClient { stream in stream.onRequestData = { data, _ in sentData.append(data) } } _ = GRPCClient(streamClient: streamClient) .newGRPCStreamPrototype() .start() .sendMessage(kMessage1) XCTAssertEqual(kMessage1, sentData.subdata(in: 5..