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

Ability to programmatically trigger the finish of the request #53

Closed
luca-bernardi opened this issue Feb 10, 2014 · 4 comments
Closed

Comments

@luca-bernardi
Copy link

Right now the only thing that is possible to do to control when a response is returned is to specify the responseTime:.
This is not ideal especially if your writing you test because you need to rely of timing instead of actual method call.

@AliSoftware
Copy link
Owner

That's a good idea, but how would you imagine the API for that?

For example if you write:

id<OHHTTPStubsDescriptor> myStub =
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
    return YES;
} withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) {
    NSData* stubData = [@"Hello World!" dataUsingEncoding:NSUTF8StringEncoding];
    return [OHHTTPStubsResponse responseWithData:stubData statusCode:200 headers:nil];
}];

How would you then trigger the response of a specific/given request, even as this stubs every request (return YES in first block)? We can't imagine a method like [myStub triggerResponse] in that case…

Another solution would be to add this kind of API on the OHHTTPStubsResponse object, like a -(dispatch_block_t)responseTrigger method on OHHTTPStubsResponse so you can do something like this:

__block dispatch_block_t sendResponseBlock = nil;
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
    return YES;
} withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) {
    NSData* stubData = [@"Hello World!" dataUsingEncoding:NSUTF8StringEncoding];
    OHHTTPStubsResponse stubResponse = [OHHTTPStubsResponse responseWithData:stubData statusCode:200 headers:nil];
    sendResponseBlock = [stubResponse responseTrigger];
    return stubResponse:
}
// some code that send a network request, that will be catched by OHHTTPStubs
// then when you want to trigger the response:
if (sendResponseBlock) sendResponseBlock();

But still that won't solve any problem, as:

  • At the time the if (sendResponseBlock) sendResponseBlock(); line is hit, the RunLoop won't have excuted and the request won't really have been sent, as NSURLConnection handles networks request using events watching installed on NSRunLoop (that's why there is a scheduleInRunLoop:modes: method on NSURLConnection)
  • Even so, network requests are typically asynchronous, so you would have to block your Unit Test code anyway to wait for the request to be at least sent (which is done asychronously by NSURLConnection or NSURLSession anyway)

The problem being that Cocoa's URL Loading System has a dedicated thread that manage network requests to send them and manage the response, and is the one responsive for calling -canInitRequest: then startLoading (asynchronously, on this dedicated thread) on the NSURLProtocol classes like the one OHHTTPStubsProtocol uses.

So I'm afraid we don't have much control over this, as all this process is asynchronous by nature.

But if you have any suggestion on how to implement it and how would the OHHTTPStubs API could look to make this work, please share as this would be quite an interesting feature!

@luca-bernardi
Copy link
Author

I have to be honest I came up with a solutions similar to the second one you proposed and I stopped because of the problems you listed. I'm afraid that at the moment I do not have a good idea on how to implement that, an this is also the reason why I've been a little generic in my first comment: I was trying to start sort of brainstorming.
Anyway I'll keep a background thread running in my brain to see if I can came up with a proper solution.
I think that is worth to keep this issue open so that other peoples can eventually partecipate to the discussion.

@AliSoftware
Copy link
Owner

Agreed, i'll keep it open and label it accordingly.

@AliSoftware
Copy link
Owner

Closing this as it has been open for too long without any new propositions. Feel free to reopen if any idea to propose this comes to mind but I think this is conceptually impossible.

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

No branches or pull requests

2 participants