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

Matching request headers on iOS 8 #90

Open
baut opened this issue Nov 27, 2014 · 3 comments
Open

Matching request headers on iOS 8 #90

baut opened this issue Nov 27, 2014 · 3 comments

Comments

@baut
Copy link

baut commented Nov 27, 2014

I'm stubbing a request like the below

stubRequest(@"GET", wildUrl.regex).withHeaders(@{@"Authorization": @"Bearer 123"}).andReturn(200);

It's working fine on the iOS 7.1 sim but isn't matching the request on iOS 8 sim - it seems that the headers don't exist on the request in startLoading on LSHTTPStubURLProtocol.

Anyone else experiencing this? Stubbing is working fine on iOS 8 when not trying to match headers.

@jjohnson4014
Copy link

I'm running into this as well using Xcode 6.0.1 targetting 8.0, trying to verify that a header with a UUID is matched. Could be I'm missing something?

#define EXP_SHORTHAND
#import "Expecta.h"
#import "Specta.h"
#import "Nocilla.h"

SpecBegin(Requests)

describe(@"Nocilla", ^{

    __block NSString *fakeURL;

    beforeAll(^{
        fakeURL = @"http://somefakeplace.com";
        [[LSNocilla sharedInstance] start];
    });

    afterAll(^{
        [[LSNocilla sharedInstance] stop];
    });
    afterEach(^{
        [[LSNocilla sharedInstance] clearStubs];
    });

    it(@"control to verify regex is valid", ^{
        NSRegularExpression *headerRegex = [NSRegularExpression regularExpressionWithPattern:@"[\\w]{8}(-[\\w]{4}){3}-[\\w]{12}" options:0 error:nil];
        NSString *uuid = [[NSUUID UUID] UUIDString];
        NSArray *matches = [headerRegex matchesInString:uuid options:0 range:NSMakeRange(0, [uuid length])];
        expect([matches count]).to.beGreaterThan(0);
    });

    it(@"should match a stub that has a UUID in a header", ^AsyncBlock {
        NSRegularExpression *headerRegex = [NSRegularExpression regularExpressionWithPattern:@"[\\w]{8}(-[\\w]{4}){3}-[\\w]{12}" options:0 error:nil];
        stubRequest(@"GET", fakeURL)
        .withHeaders(@{@"Accept": @"application/json", @"TraceContext":headerRegex})
        .andReturn(200).withBody(@"{\"ok\":true}");

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:fakeURL]];
        [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [request setValue:[[NSUUID UUID] UUIDString] forHTTPHeaderField:@"TraceContext"];
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            expect(data).to.equal(@"{\"ok\":true}".data);
            done();
        }];
    });
});

SpecEnd

@itspage
Copy link
Contributor

itspage commented Dec 17, 2014

@jjohnson4014 This is because you can't put regular expressions in the header matching - you can only use regular expressions to match the URL.

@itspage
Copy link
Contributor

itspage commented Dec 17, 2014

OK further update on this it seems to happen with the iOS7 introduced NSURLSession dataTaskWithURL but not with the old school NSURLConnection requests.

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

3 participants