From b737d74fe920fe7715121e0ea9b579698d7c9ce2 Mon Sep 17 00:00:00 2001 From: Kevin Harwood Date: Mon, 14 Mar 2016 09:34:52 -0500 Subject: [PATCH] Removed trailing question mark in query string if parameters are empty Related to #3376 --- AFNetworking/AFURLRequestSerialization.m | 2 +- Tests/Tests/AFHTTPRequestSerializationTests.m | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/AFNetworking/AFURLRequestSerialization.m b/AFNetworking/AFURLRequestSerialization.m index c0a6618caf..9a2ac98092 100644 --- a/AFNetworking/AFURLRequestSerialization.m +++ b/AFNetworking/AFURLRequestSerialization.m @@ -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 { diff --git a/Tests/Tests/AFHTTPRequestSerializationTests.m b/Tests/Tests/AFHTTPRequestSerializationTests.m index 1aedf72d4c..3c1765b9f4 100644 --- a/Tests/Tests/AFHTTPRequestSerializationTests.m +++ b/Tests/Tests/AFHTTPRequestSerializationTests.m @@ -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];