fixups for setting/getting nil/empty query string values

This commit is contained in:
Richard Frith-Macdonald 2020-04-04 22:37:40 +01:00
parent b0c763de36
commit 2a24e8d080
2 changed files with 75 additions and 35 deletions

View file

@ -2653,8 +2653,15 @@ GS_PRIVATE_INTERNAL(NSURLComponents)
[query appendString: value]; [query appendString: value];
} }
} }
result = AUTORELEASE([query copy]); if (nil == query)
RELEASE(query); {
result = @"";
}
else
{
result = AUTORELEASE([query copy]);
RELEASE(query);
}
} }
return result; return result;
} }
@ -2667,6 +2674,10 @@ GS_PRIVATE_INTERNAL(NSURLComponents)
{ {
[self setQueryItems: nil]; [self setQueryItems: nil];
} }
else if ([query length] == 0)
{
[self setQueryItems: [NSArray array]];
}
else else
{ {
NSMutableArray *result = [NSMutableArray arrayWithCapacity: 5]; NSMutableArray *result = [NSMutableArray arrayWithCapacity: 5];
@ -2674,6 +2685,7 @@ GS_PRIVATE_INTERNAL(NSURLComponents)
NSEnumerator *en = [items objectEnumerator]; NSEnumerator *en = [items objectEnumerator];
id item = nil; id item = nil;
items = [query componentsSeparatedByString: @"&"];
while ((item = [en nextObject]) != nil) while ((item = [en nextObject]) != nil)
{ {
NSURLQueryItem *qitem; NSURLQueryItem *qitem;
@ -2827,8 +2839,15 @@ GS_PRIVATE_INTERNAL(NSURLComponents)
[query appendString: value]; [query appendString: value];
} }
} }
result = AUTORELEASE([query copy]); if (nil == query)
RELEASE(query); {
result = @"";
}
else
{
result = AUTORELEASE([query copy]);
RELEASE(query);
}
} }
return result; return result;
} }

View file

@ -9,45 +9,66 @@ int main()
START_SET("components"); START_SET("components");
components = [NSURLComponents componentsWithURL:[NSURL URLWithString:@"https://user:password@some.host.com"] resolvingAgainstBaseURL:NO]; components = [NSURLComponents componentsWithURL:
[NSURL URLWithString: @"https://user:password@some.host.com"]
resolvingAgainstBaseURL: NO];
[components setQueryItems: [NSArray arrayWithObjects: [components setQueryItems: [NSArray arrayWithObjects:
[NSURLQueryItem queryItemWithName:@"lang" value:@"en"], [NSURLQueryItem queryItemWithName: @"lang" value: @"en"],
[NSURLQueryItem queryItemWithName:@"response_type" value:@"code"], [NSURLQueryItem queryItemWithName: @"response_type" value: @"code"],
[NSURLQueryItem queryItemWithName:@"uri" value:[[NSURL URLWithString:@"https://some.url.com/path?param1=one&param2=two"] absoluteString]], nil]]; [NSURLQueryItem queryItemWithName: @"uri" value:
[[NSURL URLWithString: @"https://some.url.com/path?param1=one&param2=two"]
absoluteString]], nil]];
// URL // URL
PASS([[components string] isEqualToString: PASS_EQUAL([components string],
@"https://user:password@some.host.com?lang=en&response_type=code&uri=https://some.url.com/path?param1%3Done%26param2%3Dtwo"], @"https://user:password@some.host.com?lang=en&response_type=code"
"URL string is correct"); @"&uri=https://some.url.com/path?param1%3Done%26param2%3Dtwo",
"URL string is correct")
// encoded... // encoded...
PASS([[components percentEncodedQuery] isEqualToString: PASS_EQUAL([components percentEncodedQuery],
@"lang=en&response_type=code&uri=https://some.url.com/path?param1%3Done%26param2%3Dtwo"], @"lang=en&response_type=code&uri=https://some.url.com/path"
"percentEncodedQuery is correct"); @"?param1%3Done%26param2%3Dtwo",
PASS([[components percentEncodedHost] isEqualToString: "percentEncodedQuery is correct")
@"some.host.com"], PASS_EQUAL([components percentEncodedHost], @"some.host.com",
"percentEncodedHost is correct"); "percentEncodedHost is correct")
PASS([[components percentEncodedUser] isEqualToString: PASS_EQUAL([components percentEncodedUser], @"user",
@"user"], "percentEncodedUser is correct")
"percentEncodedUser is correct"); PASS_EQUAL([components percentEncodedPassword], @"password",
PASS([[components percentEncodedPassword] isEqualToString: "percentEncodedPassword is correct")
@"password"],
"percentEncodedPassword is correct");
// unencoded... // unencoded...
PASS([[components query] isEqualToString: PASS_EQUAL([components query],
@"lang=en&response_type=code&uri=https://some.url.com/path?param1=one&param2=two"], @"lang=en&response_type=code&uri=https://some.url.com/path?"
"query is correct"); @"param1=one&param2=two",
PASS([[components host] isEqualToString: "query is correct")
@"some.host.com"], PASS_EQUAL([components host], @"some.host.com",
"host is correct"); "host is correct")
PASS([[components user] isEqualToString: PASS_EQUAL([components user], @"user",
@"user"], "user is correct")
"user is correct"); PASS_EQUAL([components password], @"password",
PASS([[components password] isEqualToString: "password is correct")
@"password"],
"password is correct");
[components setQuery: nil];
PASS_EQUAL([components query], nil,
"set query to nil")
PASS_EQUAL([components percentEncodedQuery], nil,
"percent encoded query is nil")
PASS_EQUAL([components queryItems], nil,
"query items is nil")
PASS_EQUAL([components percentEncodedQueryItems], nil,
"percent encoded query items is nil")
[components setQuery: @""];
NSArray *emptyArray = [NSArray array];
PASS_EQUAL([components query], @"",
"set query to empty")
PASS_EQUAL([components percentEncodedQuery], @"",
"percent encoded query is empty")
PASS_EQUAL([components queryItems], emptyArray,
"query items is empty")
PASS_EQUAL([components percentEncodedQueryItems], emptyArray,
"percent encoded query items is empty")
END_SET("components") END_SET("components")