mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-24 04:46:01 +08:00
Wrap requestWithMethod:path:parameters: instead of reimplementing it. closes #959
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
//{
|
||||
|
||||
Reference in New Issue
Block a user