-
Notifications
You must be signed in to change notification settings - Fork 601
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
In Test cases, Fix potential memory leaks by use of NSURLSession #250
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,14 +45,15 @@ - (void)setUp | |
{ | ||
[super setUp]; | ||
[OHHTTPStubs removeAllStubs]; | ||
|
||
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; | ||
self.session = [NSURLSession sessionWithConfiguration:configuration delegate:nil delegateQueue:nil]; | ||
} | ||
|
||
- (void)tearDown | ||
{ | ||
[super tearDown]; | ||
[self.session invalidateAndCancel]; | ||
self.session = nil; | ||
} | ||
|
||
|
@@ -110,18 +111,18 @@ - (NSHTTPURLResponse *)runLogin | |
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url | ||
cachePolicy:NSURLRequestUseProtocolCachePolicy | ||
timeoutInterval:60.0]; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And if course that applies to everywhere it changed here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @AliSoftware, Sorry I missed that. It's fixed now. My Xcode is already up as you mentioned, but I just didn't CTRL-i to repair the indentation on that one file. It's in the new commit of this PR now. |
||
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; | ||
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"]; | ||
|
||
request.HTTPMethod = @"POST"; | ||
NSDictionary *mapData = @{@"iloveit": @"happyuser1", | ||
@"password": @"username"}; | ||
NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:NULL]; | ||
request.HTTPBody = postData; | ||
|
||
XCTestExpectation* expectation = [self expectationWithDescription:@"NSURLSessionDataTask completed"]; | ||
|
||
__block NSHTTPURLResponse *capturedResponse; | ||
NSURLSessionDataTask *postDataTask = [self.session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { | ||
if ([response isKindOfClass:[NSHTTPURLResponse class]]) capturedResponse = (id)response; | ||
|
@@ -131,18 +132,18 @@ - (NSHTTPURLResponse *)runLogin | |
{ | ||
json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; | ||
} | ||
|
||
XCTAssertNotNil(json, @"The response is not a json object"); | ||
XCTAssertEqualObjects(json[@"status"], @"SUCCESS", @"The response does to return a successful status"); | ||
XCTAssertNotNil(json[@"user_token"], @"The response does not contain a user token"); | ||
|
||
[expectation fulfill]; | ||
}]; | ||
|
||
[postDataTask resume]; | ||
|
||
[self waitForExpectationsWithTimeout:10 handler:nil]; | ||
|
||
return capturedResponse; | ||
} | ||
|
||
|
@@ -152,36 +153,36 @@ - (NSHTTPURLResponse *)runGetCards | |
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url | ||
cachePolicy:NSURLRequestUseProtocolCachePolicy | ||
timeoutInterval:60.0]; | ||
|
||
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; | ||
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"]; | ||
|
||
request.HTTPMethod = @"GET"; | ||
|
||
XCTestExpectation* expectation = [self expectationWithDescription:@"NSURLSessionDataTask completed"]; | ||
|
||
__block NSHTTPURLResponse *capturedResponse; | ||
NSURLSessionDataTask *getDataTask = [self.session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { | ||
if ([response isKindOfClass:[NSHTTPURLResponse class]]) capturedResponse = (id)response; | ||
XCTAssertNil(error, @"Error while getting cards."); | ||
|
||
NSArray *json = nil; | ||
if(!error && [@"application/json" isEqual:response.MIMEType]) | ||
{ | ||
json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; | ||
} | ||
|
||
XCTAssertNotNil(json, @"The response is not a json object"); | ||
XCTAssertEqual(json.count, 2, @"The response does not contain 2 cards"); | ||
XCTAssertEqualObjects([json firstObject][@"amount"], @"$25.28", @"The first card amount does not match"); | ||
|
||
[expectation fulfill]; | ||
}]; | ||
|
||
[getDataTask resume]; | ||
|
||
[self waitForExpectationsWithTimeout:10 handler:nil]; | ||
|
||
return capturedResponse; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be possible to get rid of those changes? 🙏 (by using the trick in the Xcode prefs I mentioned you in the last review or even manually of you don't want to change your prefs, that works too)
Basically all empty lines should really be empty and not contain indentation and spaces. I know it's nitpicking and a matter of taste, I don't care for one style or another but I care about consistency throughout the code base 😉