Skip to content

Commit

Permalink
Merge pull request AFNetworking#3386 from AFNetworking/bug/remove_que…
Browse files Browse the repository at this point in the history
…ry_string_question_mark_if_parameters_are_empty

Removed trailing question mark in query string if parameters are empty
  • Loading branch information
kcharwood committed Mar 14, 2016
2 parents 3e8addb + b737d74 commit 9a70ba9
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 9a70ba9

Please sign in to comment.