diff --git a/Mocktail/Mocktail.m b/Mocktail/Mocktail.m index 1d8ae75..5a7cb78 100644 --- a/Mocktail/Mocktail.m +++ b/Mocktail/Mocktail.m @@ -103,8 +103,9 @@ + (MocktailResponse *)mockResponseForURL:(NSURL *)url method:(NSString *)method; MocktailResponse *matchingResponse = nil; NSUInteger matchingRegexLength = 0; - - NSString *absoluteURL = [url absoluteString]; + + url = [url absoluteURL]; + NSString *absoluteURL = [NSString stringWithFormat:@"%@:%@", [[url scheme] lowercaseString], [url resourceSpecifier]]; for (Mocktail *mocktail in [Mocktail allMocktails]) { for (MocktailResponse *response in mocktail.mockResponses) { if ([response.absoluteURLRegex numberOfMatchesInString:absoluteURL options:0 range:NSMakeRange(0, absoluteURL.length)] > 0) { @@ -193,8 +194,11 @@ - (void)registerFileAtURL:(NSURL *)url; NSMutableDictionary *headers = [[NSMutableDictionary alloc] init]; for (NSString *line in [lines subarrayWithRange:NSMakeRange(3, lines.count - 3)]) { NSArray* parts = [line componentsSeparatedByString:@":"]; - [headers setObject:[[parts lastObject] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] - forKey:[parts firstObject]]; + + NSString *key = [parts firstObject]; + NSString *value = [[parts subarrayWithRange:NSMakeRange(1, [parts count]-1)] componentsJoinedByString:@":"]; + [headers setObject:[value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] + forKey:key]; } response.headers = headers; response.fileURL = url; diff --git a/Mocktail/MocktailURLProtocol.m b/Mocktail/MocktailURLProtocol.m index 20e1cf9..ca6df6e 100644 --- a/Mocktail/MocktailURLProtocol.m +++ b/Mocktail/MocktailURLProtocol.m @@ -73,22 +73,31 @@ - (void)startLoading; } NSHTTPURLResponse *urlResponse = [[NSHTTPURLResponse alloc] initWithURL:self.request.URL statusCode:response.statusCode HTTPVersion:@"1.1" headerFields:headers]; - [self.client URLProtocol:self didReceiveResponse:urlResponse cacheStoragePolicy:NSURLCacheStorageAllowedInMemoryOnly]; NSArray *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:headers forURL:self.request.URL]; [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:cookies forURL:self.request.URL mainDocumentURL:nil]; - - dispatch_block_t sendResponse = ^{ - if (!self.canceled) { - [self.client URLProtocol:self didLoadData:body]; - [self.client URLProtocolDidFinishLoading:self]; - } - }; - if (mocktail.networkDelay == 0.0) { - sendResponse(); + + BOOL isRedirect = ((300 <= response.statusCode) && (response.statusCode <= 399)); + if (isRedirect) { + NSString *location = [headers objectForKey:@"Location"]; + NSMutableURLRequest *newRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:location relativeToURL:self.request.URL]]; + [self.client URLProtocol:self wasRedirectedToRequest:newRequest redirectResponse:urlResponse]; + } else { - dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, mocktail.networkDelay * NSEC_PER_SEC); - dispatch_after(popTime, dispatch_get_main_queue(), sendResponse); + [self.client URLProtocol:self didReceiveResponse:urlResponse cacheStoragePolicy:NSURLCacheStorageAllowedInMemoryOnly]; + + dispatch_block_t sendResponse = ^{ + if (!self.canceled) { + [self.client URLProtocol:self didLoadData:body]; + [self.client URLProtocolDidFinishLoading:self]; + } + }; + if (mocktail.networkDelay == 0.0) { + sendResponse(); + } else { + dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, mocktail.networkDelay * NSEC_PER_SEC); + dispatch_after(popTime, dispatch_get_main_queue(), sendResponse); + } } }