Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

optimization: when responseSerializer is set a AFHTTPResponseSerializer instance #51

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions AFNetworkActivityLogger/AFNetworkActivityConsoleLogger.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,24 @@ - (void)URLSessionTaskDidFinish:(NSURLSessionTask *)task withResponseObject:(id)
}
} else {
switch (self.level) {
case AFLoggerLevelDebug:
NSLog(@"%ld '%@' [%.04f s]: %@ %@", (long)responseStatusCode, [[task.response URL] absoluteString], elapsedTime, responseHeaderFields, responseObject);
break;
case AFLoggerLevelDebug: {
id responseBody = responseObject;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when log level is AFLoggerLevelDebug apply the optimization


if(responseHeaderFields != nil && [responseObject isKindOfClass:[NSData class]]) {
id contentTypeObj = [responseHeaderFields objectForKey:@"Content-Type"];
if([contentTypeObj isKindOfClass:[NSString class]]) {
NSString *contentType = contentTypeObj;
if([contentType containsString:@"application/json"]
|| [contentType containsString:@"application/xml"]
|| [contentType containsString:@"application/x-www-form-urlencoded"]
|| [contentType containsString:@"text/html"]) {
responseBody = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
}
}
}
NSLog(@"%ld '%@' [%.04f s]: %@ %@", (long)responseStatusCode, [[task.response URL] absoluteString], elapsedTime, responseHeaderFields, responseBody);
}
break;
case AFLoggerLevelInfo:
NSLog(@"%ld '%@' [%.04f s]", (long)responseStatusCode, [[task.response URL] absoluteString], elapsedTime);
break;
Expand Down
47 changes: 47 additions & 0 deletions Tests/AFNetworkActivityLoggerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -265,5 +265,52 @@ - (void)testThatIndividualLoggerIsNotCalledWhenLoggerIsNilledOut {
[manager invalidateSessionCancelingTasks:YES];
}

- (void)testThatResponseSerializerIsAFHTTPResponseSerializerAndResponseBodyIsText {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the test case: when manager.responseSerializer = [AFHTTPResponseSerializer serializer]; and the http response body is text.

NSURL *baseURL = [NSURL URLWithString:@"https://httpbin.org"];
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
AFNetworkActivityTestLogger *testLogger = [AFNetworkActivityTestLogger new];

XCTestExpectation *expectation = [self expectationWithDescription:@"Finish Block Should Be Called"];
[testLogger setFinishBlock:^(NSURLSessionTask *task, id responseObject, NSTimeInterval elpasedTime, NSError *error) {
[expectation fulfill];
}];
[self.logger addLogger:testLogger];
[self.logger setLogLevel:AFLoggerLevelDebug];
[self.logger startLogging];

[manager
GET:@"ip"
parameters:nil
progress:nil
success:nil
failure:nil];
[self waitForExpectationsWithTimeout:10.0 handler:nil];
[manager invalidateSessionCancelingTasks:YES];
}

- (void)testThatResponseSerializerIsAFHTTPResponseSerializerAndResponseBodyIsNotText {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the test case: when manager.responseSerializer = [AFHTTPResponseSerializer serializer]; and the http response body is not text, for example, the response body is a image

NSURL *baseURL = [NSURL URLWithString:@"https://httpbin.org"];
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
AFNetworkActivityTestLogger *testLogger = [AFNetworkActivityTestLogger new];

XCTestExpectation *expectation = [self expectationWithDescription:@"Finish Block Should Be Called"];
[testLogger setFinishBlock:^(NSURLSessionTask *task, id responseObject, NSTimeInterval elpasedTime, NSError *error) {
[expectation fulfill];
}];
[self.logger addLogger:testLogger];
[self.logger setLogLevel:AFLoggerLevelDebug];
[self.logger startLogging];

[manager
GET:@"image/jpeg"
parameters:nil
progress:nil
success:nil
failure:nil];
[self waitForExpectationsWithTimeout:10.0 handler:nil];
[manager invalidateSessionCancelingTasks:YES];
}

@end