Fix and test coverage for base URLs containing a path

This commit is contained in:
Blake Watters
2011-07-06 15:57:05 -04:00
parent 6376274900
commit c7d8d0db3b
2 changed files with 11 additions and 1 deletions

View File

@@ -28,15 +28,20 @@
}
- (id)initWithBaseURLString:(NSString*)baseURLString resourcePath:(NSString*)resourcePath queryParams:(NSDictionary*)queryParams {
// Ensure the resource path always has a leading slash
if ([resourcePath length] > 0) {
resourcePath = ([resourcePath characterAtIndex:0] != '/') ? [NSString stringWithFormat:@"/%@", resourcePath] : resourcePath;
}
NSString* resourcePathWithQueryString = RKPathAppendQueryParams(resourcePath, queryParams);
NSURL *baseURL = [NSURL URLWithString:baseURLString];
NSURL *completeURL = [NSURL URLWithString:resourcePathWithQueryString relativeToURL:baseURL];
NSURL *completeURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", baseURL, resourcePathWithQueryString]];
if (!completeURL) {
[self release];
return nil;
}
// You can't safely use initWithString:relativeToURL: in a NSURL subclass, see http://www.openradar.me/9729706
NSLog(@"Initialized the string: %@", [completeURL absoluteString]);
self = [self initWithString:[completeURL absoluteString]];
if (self) {
_baseURLString = [baseURLString copy];

View File

@@ -53,5 +53,10 @@
assertThat([URL absoluteString], is(equalTo(@"http://restkit.org")));
}
- (void)itShouldHandleBaseURLsWithAPath {
RKURL* URL = [RKURL URLWithBaseURLString:@"http://restkit.org/this" resourcePath:@"/test" queryParams:nil];
assertThat([URL absoluteString], is(equalTo(@"http://restkit.org/this/test")));
}
@end