Skip to content

Commit f7f1676

Browse files
committed
Fixing jsonmodel#58
JSONHTTPClient now takes as request parameters NSString and NSNumber objects
1 parent 66ed1e2 commit f7f1676

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

‎JSONModel/JSONModelNetworking/JSONHTTPClient.m‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,18 @@ +(NSString*)contentTypeForRequestString:(NSString*)requestString
125125
return [NSString stringWithFormat:@"%@; charset=%@", contentType, charset];
126126
}
127127

128-
+(NSString*)urlEncode:(NSString*)string
128+
+(NSString*)urlEncode:(id<NSObject>)value
129129
{
130+
//make sure param is a string
131+
if ([value isKindOfClass:[NSNumber class]]) {
132+
value = [(NSNumber*)value stringValue];
133+
}
134+
135+
NSAssert([value isKindOfClass:[NSString class]], @"request parameters can be only of NSString or NSNumber classes. '%@' is of class %@.", value, [value class]);
136+
130137
return (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
131138
NULL,
132-
(__bridge CFStringRef) string,
139+
(__bridge CFStringRef) value,
133140
NULL,
134141
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
135142
kCFStringEncodingUTF8));

‎JSONModelDemoTests/UnitTests/HTTPClientSuite.m‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,5 +338,37 @@ -(void)testPostJSONWithError
338338
[[MTTestSemaphor semaphore] waitForKey: semaphorKey];
339339
}
340340

341+
//https://github.com/icanzilb/JSONModel/issues/58
342+
-(void)testNumberQueryParams
343+
{
344+
NSString* jsonURLString = @"http://localhost/test.json?testGetJSONFromURLWithParamsNumber";
345+
NSString* semaphorKey = @"testGetJSONFromURLWithParamsNumber";
346+
347+
NSHTTPURLResponse* response = [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:jsonURLString]
348+
statusCode:200
349+
HTTPVersion:@"1.1"
350+
headerFields:nil];
351+
352+
[NSURLConnection setNextResponse:response data:[jsonContents dataUsingEncoding:NSUTF8StringEncoding] error:nil];
353+
354+
[JSONHTTPClient getJSONFromURLWithString:jsonURLString
355+
params:@{@"key1":@100.56,@"key2":@"test"}
356+
completion:^(NSDictionary *json, JSONModelError *err) {
357+
358+
//check block parameters
359+
NSAssert(json, @"getJSONFromURLWithString:completion: returned nil, object expected");
360+
NSAssert(!err, @"getJSONFromURLWithString:completion: returned error, nil error expected");
361+
362+
//check the request
363+
NSURLRequest* request = [NSURLConnection lastRequest];
364+
NSAssert([request.URL.absoluteString isEqualToString: @"http://localhost/test.json?testGetJSONFromURLWithParamsNumber&key1=100.56&key2=test"], @"request.URL did not match the request URL");
365+
366+
//release the semaphore lock
367+
[[MTTestSemaphor semaphore] lift: semaphorKey];
368+
}];
369+
370+
[[MTTestSemaphor semaphore] waitForKey: semaphorKey];
371+
372+
}
341373

342374
@end

0 commit comments

Comments
 (0)