Skip to content

Commit

Permalink
Improve documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
AvdLee committed Feb 7, 2020
1 parent 2464a66 commit ba110e5
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions Sources/MockingURLProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,26 +106,23 @@ 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 }

bodyStream.open()

// Will read 16 chars per iteration. Can use bigger buffer if needed
let bufferSize: Int = 16

let buffer = UnsafeMutablePointer<UInt8>.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
Expand Down

0 comments on commit ba110e5

Please sign in to comment.