diff --git a/Sources/MockingURLProtocol.swift b/Sources/MockingURLProtocol.swift index 1c4f05f..51997ba 100644 --- a/Sources/MockingURLProtocol.swift +++ b/Sources/MockingURLProtocol.swift @@ -106,6 +106,7 @@ private extension URLRequest { return try? JSONSerialization.jsonObject(with: httpBody, options: .fragmentsAllowed) as? [String: Any] } + /// We need to use the http body stream data as the URLRequest once launched converts the `httpBody` to this stream of data. private func httpBodyStreamData() -> Data? { guard let bodyStream = self.httpBodyStream else { return nil } @@ -113,19 +114,15 @@ private extension URLRequest { // Will read 16 chars per iteration. Can use bigger buffer if needed let bufferSize: Int = 16 - let buffer = UnsafeMutablePointer.allocate(capacity: bufferSize) - var data = Data() while bodyStream.hasBytesAvailable { - - let readDat = bodyStream.read(buffer, maxLength: bufferSize) - data.append(buffer, count: readDat) + let readData = bodyStream.read(buffer, maxLength: bufferSize) + data.append(buffer, count: readData) } buffer.deallocate() - bodyStream.close() return data