Skip to content

Commit

Permalink
Removed trailing question mark in query string if parameters are empty
Browse files Browse the repository at this point in the history
Related to #3376
  • Loading branch information
kcharwood committed Mar 14, 2016
1 parent 326ccb1 commit b737d74
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion AFNetworking/AFURLRequestSerialization.m
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ - (NSURLRequest *)requestBySerializingRequest:(NSURLRequest *)request
}

if ([self.HTTPMethodsEncodingParametersInURI containsObject:[[request HTTPMethod] uppercaseString]]) {
if (query) {
if (query && query.length > 0) {
mutableRequest.URL = [NSURL URLWithString:[[mutableRequest.URL absoluteString] stringByAppendingFormat:mutableRequest.URL.query ? @"&%@" : @"?%@", query]];
}
} else {
Expand Down
6 changes: 6 additions & 0 deletions Tests/Tests/AFHTTPRequestSerializationTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ - (void)testThatAFHTTPRequestSerialiationSerializesQueryParametersCorrectly {
XCTAssertTrue([[[serializedRequest URL] query] isEqualToString:@"key=value"], @"Query parameters have not been serialized correctly (%@)", [[serializedRequest URL] query]);
}

- (void)testThatEmptyDictionaryParametersAreProperlyEncoded {
NSURLRequest *originalRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com"]];
NSURLRequest *serializedRequest = [self.requestSerializer requestBySerializingRequest:originalRequest withParameters:@{} error:nil];
XCTAssertFalse([serializedRequest.URL.absoluteString hasSuffix:@"?"]);
}

- (void)testThatAFHTTPRequestSerialiationSerializesURLEncodableQueryParametersCorrectly {
NSURLRequest *originalRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com"]];
NSURLRequest *serializedRequest = [self.requestSerializer requestBySerializingRequest:originalRequest withParameters:@{@"key":@" :#[]@!$&'()*+,;=/?"} error:nil];
Expand Down

0 comments on commit b737d74

Please sign in to comment.