Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2868 from jcayzac/fix-2864
Browse files Browse the repository at this point in the history
Fix #2864: allow empty x-www-form-urlencoded bodies
  • Loading branch information
kcharwood committed Oct 8, 2015
2 parents 262ebd4 + 9cb72ef commit 824f7c5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
20 changes: 13 additions & 7 deletions AFNetworking/AFURLRequestSerialization.m
Expand Up @@ -499,8 +499,8 @@ - (NSURLRequest *)requestBySerializingRequest:(NSURLRequest *)request
}
}];

NSString *query = nil;
if (parameters) {
NSString *query = nil;
if (self.queryStringSerialization) {
NSError *serializationError;
query = self.queryStringSerialization(request, parameters, &serializationError);
Expand All @@ -519,15 +519,21 @@ - (NSURLRequest *)requestBySerializingRequest:(NSURLRequest *)request
break;
}
}
}

if ([self.HTTPMethodsEncodingParametersInURI containsObject:[[request HTTPMethod] uppercaseString]]) {
if ([self.HTTPMethodsEncodingParametersInURI containsObject:[[request HTTPMethod] uppercaseString]]) {
if (query) {
mutableRequest.URL = [NSURL URLWithString:[[mutableRequest.URL absoluteString] stringByAppendingFormat:mutableRequest.URL.query ? @"&%@" : @"?%@", query]];
} else {
if (![mutableRequest valueForHTTPHeaderField:@"Content-Type"]) {
[mutableRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
}
[mutableRequest setHTTPBody:[query dataUsingEncoding:self.stringEncoding]];
}
} else {
// #2864: an empty string is a valid x-www-form-urlencoded payload
if (!query) {
query = @"";
}
if (![mutableRequest valueForHTTPHeaderField:@"Content-Type"]) {
[mutableRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
}
[mutableRequest setHTTPBody:[query dataUsingEncoding:self.stringEncoding]];
}

return mutableRequest;
Expand Down
28 changes: 28 additions & 0 deletions Tests/Tests/AFHTTPRequestSerializationTests.m
Expand Up @@ -65,6 +65,34 @@ - (void)setUp {

#pragma mark -

- (void)testThatAFHTTPRequestSerializationSerializesPOSTRequestsProperly {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com"]];
request.HTTPMethod = @"POST";

NSURLRequest *serializedRequest = [self.requestSerializer requestBySerializingRequest:request withParameters:@{@"key":@"value"} error:nil];
NSString *contentType = serializedRequest.allHTTPHeaderFields[@"Content-Type"];

XCTAssertNotNil(contentType);
XCTAssertEqualObjects(contentType, @"application/x-www-form-urlencoded");

XCTAssertNotNil(serializedRequest.HTTPBody);
XCTAssertEqualObjects(serializedRequest.HTTPBody, [@"key=value" dataUsingEncoding:NSUTF8StringEncoding]);
}

- (void)testThatAFHTTPRequestSerializationSerializesPOSTRequestsProperlyWhenNoParameterIsProvided {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com"]];
request.HTTPMethod = @"POST";

NSURLRequest *serializedRequest = [self.requestSerializer requestBySerializingRequest:request withParameters:nil error:nil];
NSString *contentType = serializedRequest.allHTTPHeaderFields[@"Content-Type"];

XCTAssertNotNil(contentType);
XCTAssertEqualObjects(contentType, @"application/x-www-form-urlencoded");

XCTAssertNotNil(serializedRequest.HTTPBody);
XCTAssertEqualObjects(serializedRequest.HTTPBody, [NSData data]);
}

- (void)testThatAFHTTPRequestSerialiationSerializesQueryParametersCorrectly {
NSURLRequest *originalRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com"]];
NSURLRequest *serializedRequest = [self.requestSerializer requestBySerializingRequest:originalRequest withParameters:@{@"key":@"value"} error:nil];
Expand Down

0 comments on commit 824f7c5

Please sign in to comment.