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

Added ability to stub request with a redirect without needing to specify a Location header. #121

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Nocilla/Hooks/NSURLRequest/LSHTTPStubURLProtocol.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#import "LSStubRequest.h"
#import "NSURLRequest+DSL.h"

NSString * const kHTTPResponseHeaderLocationKey = @"Location";

@interface NSHTTPURLResponse(UndocumentedInitializer)
- (id)initWithURL:(NSURL*)URL statusCode:(NSInteger)statusCode headerFields:(NSDictionary*)headerFields requestTime:(double)requestTime;
@end
Expand Down Expand Up @@ -34,9 +36,10 @@ - (void)startLoading {
statusCode:stubbedResponse.statusCode
headerFields:stubbedResponse.headers
requestTime:0];

if (stubbedResponse.statusCode < 300 || stubbedResponse.statusCode > 399
|| stubbedResponse.statusCode == 304 || stubbedResponse.statusCode == 305 ) {
|| stubbedResponse.statusCode == 304 || stubbedResponse.statusCode == 305
|| ![stubbedResponse.headers.allKeys containsObject:kHTTPResponseHeaderLocationKey]) {
NSData *body = stubbedResponse.body;

[client URLProtocol:self didReceiveResponse:urlResponse
Expand All @@ -48,7 +51,7 @@ - (void)startLoading {
[cookieStorage setCookies:[NSHTTPCookie cookiesWithResponseHeaderFields:stubbedResponse.headers forURL:request.url]
forURL:request.URL mainDocumentURL:request.URL];

NSURL *newURL = [NSURL URLWithString:[stubbedResponse.headers objectForKey:@"Location"] relativeToURL:request.URL];
NSURL *newURL = [NSURL URLWithString:[stubbedResponse.headers objectForKey:kHTTPResponseHeaderLocationKey] relativeToURL:request.URL];
NSMutableURLRequest *redirectRequest = [NSMutableURLRequest requestWithURL:newURL];

[redirectRequest setAllHTTPHeaderFields:[NSHTTPCookie requestHeaderFieldsWithCookies:[cookieStorage cookiesForURL:newURL]]];
Expand Down
19 changes: 19 additions & 0 deletions NocillaTests/Hooks/NSURLRequest/LSHTTPStubURLProtocolSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,25 @@ - (void)URLProtocol:(NSURLProtocol *)protocol didCancelAuthenticationChallenge:(
}) should] raiseWithName:@"NocillaUnexpectedRequest" reason:expectedMessage];
});
});

context(@"that returns a 303 without specifying a Location header", ^{

beforeEach(^{

LSStubRequest *stubRequest = [[LSStubRequest alloc] initWithMethod:@"GET" url:stringUrl];
stubRequest.response = [[LSStubResponse alloc] initWithStatusCode:(NSInteger)303];

[[LSNocilla sharedInstance] stub:@selector(stubbedRequests) andReturn:@[stubRequest]];

[protocol startLoading];
});

it(@"should send back a response with 303", ^{

[[client.response should] beKindOfClass:[NSHTTPURLResponse class]];
[[theValue(((NSHTTPURLResponse *)client.response).statusCode) should] equal:theValue((NSInteger)303)];
});
});
});
});

Expand Down