Wrap requestWithMethod:path:parameters: instead of reimplementing it. closes #959

This commit is contained in:
Blake Watters
2012-10-05 23:26:21 -04:00
parent 9ab37a7617
commit a4a788b889
2 changed files with 56 additions and 20 deletions

View File

@@ -202,30 +202,23 @@ static NSString *RKMIMETypeFromAFHTTPClientParameterEncoding(AFHTTPClientParamet
path:(NSString *)path
parameters:(NSDictionary *)parameters
{
NSURL *url = [NSURL URLWithString:path relativeToURL:self.HTTPClient.baseURL];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:method];
[request setAllHTTPHeaderFields:self.defaultHeaders];
if (parameters) {
if ([method isEqualToString:@"GET"] || [method isEqualToString:@"HEAD"] || [method isEqualToString:@"DELETE"]) {
url = [NSURL URLWithString:[[url absoluteString] stringByAppendingFormat:[path rangeOfString:@"?"].location == NSNotFound ? @"?%@" : @"&%@", AFQueryStringFromParametersWithEncoding(parameters, self.HTTPClient.stringEncoding)]];
[request setURL:url];
} else {
NSError *error = nil;
NSString *charset = (__bridge NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(self.HTTPClient.stringEncoding));
[request setValue:[NSString stringWithFormat:@"%@; charset=%@", self.requestSerializationMIMEType, charset] forHTTPHeaderField:@"Content-Type"];
NSData *requestBody = [RKMIMETypeSerialization dataFromObject:parameters MIMEType:self.requestSerializationMIMEType error:&error];
if (! requestBody) {
RKLogError(@"Failed to generate request body from parameters: RKMIMETypeSerialization error: %@", error);
return nil;
}
[request setHTTPBody:requestBody];
}
NSMutableURLRequest* request;
if (parameters && !([method isEqualToString:@"GET"] || [method isEqualToString:@"HEAD"] || [method isEqualToString:@"DELETE"])) {
request = [self.HTTPClient requestWithMethod:method path:path parameters:nil];
NSError *error = nil;
NSString *charset = (__bridge NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(self.HTTPClient.stringEncoding));
[request setValue:[NSString stringWithFormat:@"%@; charset=%@", self.requestSerializationMIMEType, charset] forHTTPHeaderField:@"Content-Type"];
NSData *requestBody = [RKMIMETypeSerialization dataFromObject:parameters MIMEType:self.requestSerializationMIMEType error:&error];
[request setHTTPBody:requestBody];
} else {
request = [self.HTTPClient requestWithMethod:method path:path parameters:parameters];
}
if (self.acceptHeaderValue) [request setValue:self.acceptHeaderValue forHTTPHeaderField:@"Accept"];
return request;
}
- (NSMutableURLRequest *)requestWithPathForRouteNamed:(NSString *)routeName
object:(id)object
parameters:(NSDictionary *)parameters

View File

@@ -26,6 +26,22 @@
#import "RKCat.h"
#import "RKObjectMapperTestModel.h"
@interface RKTestAFHTTPClient : AFHTTPClient
@end
@implementation RKTestAFHTTPClient
- (NSMutableURLRequest *)requestWithMethod:(NSString *)method
path:(NSString *)path
parameters:(NSDictionary *)parameters
{
NSMutableURLRequest *request = [super requestWithMethod:method path:path parameters:parameters];
[request setAllHTTPHeaderFields:@{@"test": @"value", @"Accept": @"text/html"}];
return request;
}
@end
@interface RKObjectManagerTest : RKTestCase
@property (nonatomic, strong) RKObjectManager *objectManager;
@@ -351,6 +367,33 @@
expect(string).to.equal(@"key=value");
}
- (void)testAFHTTPClientCanModifyRequestsBuiltByObjectManager
{
RKTestAFHTTPClient *testClient = [[RKTestAFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://test.com"]];
RKObjectManager *manager = [[RKObjectManager alloc] initWithHTTPClient:testClient];
RKHuman *temporaryHuman = [RKTestFactory insertManagedObjectForEntityForName:@"RKHuman" inManagedObjectContext:nil withProperties:nil];
NSURLRequest *request = [manager requestWithObject:temporaryHuman method:RKRequestMethodPATCH path:@"/the/path" parameters:@{@"key": @"value"}];
expect([request.URL absoluteString]).to.equal(@"http://test.com/the/path");
expect(request.HTTPMethod).to.equal(@"PATCH");
expect([request allHTTPHeaderFields][@"test"]).to.equal(@"value");
expect([request allHTTPHeaderFields][@"Accept"]).to.equal(@"text/html");
}
- (void)testDefaultAcceptHeaderOfObjectManagerOverridesValueOfHTTPClient
{
RKTestAFHTTPClient *testClient = [[RKTestAFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://test.com"]];
RKObjectManager *manager = [[RKObjectManager alloc] initWithHTTPClient:testClient];
[manager setAcceptHeaderWithMIMEType:@"application/json"];
RKHuman *temporaryHuman = [RKTestFactory insertManagedObjectForEntityForName:@"RKHuman" inManagedObjectContext:nil withProperties:nil];
NSURLRequest *request = [manager requestWithObject:temporaryHuman method:RKRequestMethodPATCH path:@"/the/path" parameters:@{@"key": @"value"}];
expect([request.URL absoluteString]).to.equal(@"http://test.com/the/path");
expect(request.HTTPMethod).to.equal(@"PATCH");
expect([request allHTTPHeaderFields][@"test"]).to.equal(@"value");
expect([request allHTTPHeaderFields][@"Accept"]).to.equal(@"application/json");
}
// TODO: Move to Core Data specific spec file...
//- (void)testShouldLoadAHuman
//{