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

Delayed requests #125

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

pcantrell
Copy link

A possible approach to #123.

This PR adds delay() and go() methods to the response DSL. This allows clients to do:

let reqStub = stubRequest("GET", "http://foo")
    .andReturn(200)
    .delay()

// trigger request
// make assertions here about state of things while request is in progress

reqStub.go()

// wait for response

This is an incomplete implementation:

  • it’s only implemented for LSHTTPStubURLProtocol, and
  • there are no specs.

@luisobo, let me know if you have thoughts about this approach. If so, I’ll update the PR with the full implementation.

@cianclarke
Copy link

+1 to this - nice work @pcantrell , I've also been wishing for this.
Could the same be achieved by passing an exception via andFailWithError? Either way, I think this would be nice to have

@pcantrell
Copy link
Author

andFailWithError affects the content of the response, but not when it happens. This addition is all about timing, and is orthogonal to all the other aspects of the response.

@pcantrell
Copy link
Author

Rebased for easy peasy merging fun.

FWIW, this code has been tested in the wild — in use on Siesta for the past 2 months, and working great. Here’s an example of how it plays out in practice:

https://github.com/bustoutsolutions/siesta/blob/master/Tests/ResourceRequestsSpec.swift#L109-L116

@lucaslt89
Copy link

+1 to this too! I've tested it and it works smoothly!

@aarsland
Copy link

+1 Would be excellent if this gets accepted and merged @luisobo .
@pcantrell I have found a problem with the implementation. It will also prevent subsequent requests from responding.

- (void)testThatDelayDoesNotInterruptAllRequests
{
    NSData *bodyData = [@"Body content" dataUsingEncoding:NSUTF8StringEncoding];

    LSStubResponseDSL *stub = stubRequest(@"GET", @"http://a-url").andReturn(200).delay();

    stubRequest(@"GET", @"http://b-url").andReturn(200).withBody(bodyData);

    __block NSData *dataFromResponse = nil;

    XCTestExpectation *expectation = [self expectationWithDescription:@"Wait for network"];

    // This line will prevent all request to finish, not only the intended request.
    [[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:@"http://a-url"]
                                 completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                 }] resume];

    [[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:@"http://b-url"]
            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                [stub go];
                dataFromResponse = data;
                [expectation fulfill];
            }] resume];

    [self waitForExpectationsWithTimeout:2 handler:nil];
    XCTAssert([dataFromResponse isEqualToData:bodyData]);
}

@andrewkboyd
Copy link
Collaborator

This seems reasonable, however in the meantime OS X and tvOS targets have been added to the master. If you rebase we should be able to make the merge easy and I can pull it in.

Seems simple and makes sense.

@pcantrell
Copy link
Author

pcantrell commented Sep 3, 2016

Rebased against latest master. Specs pass.

In successful use now for many months a year on Siesta.

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

Successfully merging this pull request may close these issues.

None yet

5 participants