-
Notifications
You must be signed in to change notification settings - Fork 178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
request.HTTPBody is always nil in custom matcher #32
Comments
Hi @paulyoung, just to confirm. you're setting the property and then making a request with NSURLConnection/NSURLSession? Would you be able to check if passing your request directly to MockingjayProtocol's MockingjayProtocol.canInitWithRequest(request) |
@kylef I can confirm that inside of However, I just made this change and now everything works as expected: the-grid/Disc@02b31d9 In any case, this issue can be closed. Thanks for looking into it! |
So it appears that this issue wasn't actually resolved. I think what was happening was the actual request was being made, and now that request is no longer valid so a test started failing. Where
Here's the same thing inside my custom matcher:
|
Here are the relevant test case: and matcher: |
Thats for the additional information. Had a look into this and I can confirm this is how it's behaving, after doing some digging it does seem like either an bug in NSURLSession or how NSURLSession is designed. Mockingjay registers as an NSURLProtocol and the first method that NSURLSession will call on the protocol is I've also found this note from a README from OHHTTPStubs (the popular Objective-C alternative) which mentions the same problem. I've found a related discussion inside OHHTTPStubs:
I'm not really sure how we can go about adding support for this. |
Additionally, found a related rdar://15993891 which includes a comment stating that this is not how the documentation says it should work:
|
Interesting workaround: https://github.com/AliSoftware/OHHTTPStubs/wiki/Testing-for-the-request-body-in-your-stubs |
Thanks again for looking into this! I employed the "transparent" workaround. Feel free to close this issue or leave open as you see fit. |
Workaround for MockingJay issue: kylef/Mockingjay#32.
@kylef , can you add a property that uses that method as an extension, or at least add this issue to README? I've spent an hour thinking the problem is in my sending code :) |
I'm using the following workaround (Swift 2.3) The usage is the following:
I can make PR if the solution is good enough. @kylef |
Seems not to work on my machine. It swizzles alright, but the body is nil in the request again. Perhaps it is copied to a new instance that leaves the body empty. |
Works perfectly using import Foundation
extension InputStream {
func readfully() -> Data {
var result = Data()
var buffer = [UInt8](repeating: 0, count: 4096)
open()
var amount = 0
repeat {
amount = read(&buffer, maxLength: buffer.count)
if amount > 0 {
result.append(buffer, count: amount)
}
} while amount > 0
close()
return result
}
} Usage: let bodyData = request.httpBodyStream?.readfully() |
Doesn't work anymore. |
Works for me (Swift 5.1). |
Works as intended (Swift 5.4) |
I'm setting the
HTTPBody
property of anNSMutableURLRequest
and trying to do some comparison on it inside of a custommatcher
.However, the property's value is always
nil
by the time the request reaches thematcher
despite it being set prior to that.The text was updated successfully, but these errors were encountered: