Skip to content
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

HTTPBodyStream not opening. #109

Closed
brennick opened this issue Aug 11, 2015 · 7 comments
Closed

HTTPBodyStream not opening. #109

brennick opened this issue Aug 11, 2015 · 7 comments

Comments

@brennick
Copy link

I followed the instructions here: https://github.com/AliSoftware/OHHTTPStubs/wiki/Testing-for-the-request-body-in-your-stubs#a-transparent-workaround

And when I call the following code:

request.HTTPBodyStream!.open()
let jsonObject = NSJSONSerialization.JSONObjectWithStream(request.HTTPBodyStream!, options: NSJSONReadingOptions.allZeros, error: &error) as! [String : AnyObject?]

I keep getting the error saying Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSJSONSerialization JSONObjectWithStream:options:error:]: stream is not open for reading'

It should be working since the stream is not nil and I have opened it, but I can't seem to get it to actual open for reading.

@AliSoftware
Copy link
Owner

Never tried that before so not sure, but it seems like you are trying to
read from request.HTTPBodyStream, and since it's the request, so the
data being sent, I bet it's an NSOutputStream, only intended for writing,
not an NSInputStream we can read from.
So it seems normal to me that you get that kind of error telling that the
steam is not open for reading (but only for writing).

(The issue is probably not related to OHHTTPStubs btw)

@newlix
Copy link

newlix commented Oct 31, 2015

If the workaround in wiki is not working, please remove it .
Thank you.

https://github.com/AliSoftware/OHHTTPStubs/wiki/Testing-for-the-request-body-in-your-stubs#a-transparent-workaround

A Transparent Workaround
Open and then drain the `HTTPBodyStream`. You've just recovered the `HTTPBody` as a big bag o' bytes!

If you're pulling in JSON, this gets even easier: `open` the stream, then call `+[NSJSONSerialization JSONObjectWithStream:options:error:]`. The deserializer will take care of draining the stream and spitting out JSON for you.

@AliSoftware
Copy link
Owner

😲 I'm not the one who wrote that in my wiki! Will check this out.

@AliSoftware
Copy link
Owner

@jeremy-w As you're the one who added that in my Wiki, could you explain how you made it work (or if you tested it at all), given that it doesn't seem to work for the people commenting in this issue?

@jeremy-w
Copy link

jeremy-w commented Nov 3, 2015

@AliSoftware I wrote it up only after I got it working in some project, so it's not an untested workaround. It's been a while though, so I'd have to do some digging to surface the code in question.

What immediately leaps to mind on reading the original post is that NSStream is an async API, so after asking it to open, you might need to listen for the NSStreamEventOpenCompleted event before you can read from it in order to play by the rules of its state machine. (That said, CFStreamRead is documented as blocking till the open has finished.)

It's also possible that framework changes since I used the workaround have broken it.

@AliSoftware
Copy link
Owner

Thx for the feedback @jeremy-w 👍
Keep us posted (if you could test it again and e.g. confirm that it was working before on one of your project but broke since, etc) 😉

@markst
Copy link

markst commented Oct 20, 2016

I seem to be able to check the body by serialzing the httpBodyStream

OHHTTPStubs.stubRequests(passingTest: { (request) -> Bool in
    guard let httpBodyStream = request.httpBodyStream else { return false }
    do {
        httpBodyStream.open()
        let requestBody = try JSONSerialization.jsonObject(with: httpBodyStream, options: [])
        print("requestBody",requestBody)
    } catch let error as NSError {
        print("error: \(error.localizedDescription)")
    }

#52

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants