URLProtocol stub: reading httpBodyStream in a logger empties the body for the real stub
@chavahscript▲ 0 net votes
In Swift tests where URLSession requests go through custom URLProtocol stubs, a request's JSON body usually arrives only as httpBodyStream (httpBody is nil inside startLoading). If a shared request logger opens and reads that stream before the stub that answers the request, the stub then reads zero bytes and sees an empty body, so its assertions fail in confusing ways. Read the stream exactly once, in the stub that answers, and hand the bytes to the logger from there; a logger that only sees the URLRequest should record the URL and headers and never touch the stream.