Skip to content

Commit

Permalink
Merge pull request AFNetworking#3292 from AFNetworking/bug/properly_h…
Browse files Browse the repository at this point in the history
…andle_205

Fixed status code 204/205 handling
  • Loading branch information
kcharwood committed Mar 14, 2016
2 parents aba212c + 3e0c986 commit 3e8addb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
4 changes: 3 additions & 1 deletion AFNetworking/AFURLResponseSerialization.m
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ - (BOOL)validateResponse:(NSHTTPURLResponse *)response
NSError *validationError = nil;

if (response && [response isKindOfClass:[NSHTTPURLResponse class]]) {
if (self.acceptableContentTypes && ![self.acceptableContentTypes containsObject:[response MIMEType]]) {
if (self.acceptableContentTypes && ![self.acceptableContentTypes containsObject:[response MIMEType]] &&
!([response MIMEType] == nil && [data length] == 0)) {

if ([data length] > 0 && [response URL]) {
NSMutableDictionary *mutableUserInfo = [@{
NSLocalizedDescriptionKey: [NSString stringWithFormat:NSLocalizedStringFromTable(@"Request failed: unacceptable content-type: %@", @"AFNetworking", nil), [response MIMEType]],
Expand Down
28 changes: 28 additions & 0 deletions Tests/Tests/AFHTTPResponseSerializationTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,34 @@ - (void)testThatAFHTTPResponseSerializationHandlesAll2XXCodes {
}];
}

- (void)testThatAFHTTPResponseSerializationSucceedsWith205WithNoResponseContentTypeAndNoResponseData {
NSInteger statusCode = 205;
NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:self.baseURL statusCode:statusCode HTTPVersion:@"1.1" headerFields:@{}];

XCTAssert([self.responseSerializer.acceptableStatusCodes containsIndex:statusCode], @"Status code %@ should be acceptable", @(statusCode));

NSError *error = nil;
self.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

XCTAssertTrue([self.responseSerializer validateResponse:response data:nil error:&error]);
XCTAssertNil(error, @"Error handling status code %@", @(statusCode));

XCTAssertFalse([self.responseSerializer validateResponse:response data:[@"test" dataUsingEncoding:NSUTF8StringEncoding] error:&error]);
}

- (void)testThatAFHTTPResponseSerializationFailsWith205WithNoResponseContentTypeAndResponseData {
NSInteger statusCode = 205;
NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:self.baseURL statusCode:statusCode HTTPVersion:@"1.1" headerFields:@{}];

XCTAssert([self.responseSerializer.acceptableStatusCodes containsIndex:statusCode], @"Status code %@ should be acceptable", @(statusCode));

NSError *error = nil;
self.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

XCTAssertFalse([self.responseSerializer validateResponse:response data:[@"test" dataUsingEncoding:NSUTF8StringEncoding] error:&error]);
XCTAssertNotNil(error, @"Error handling status code %@", @(statusCode));
}

- (void)testThatAFHTTPResponseSerializationFailsAll4XX5XXStatusCodes {
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(400, 200)];
[indexSet enumerateIndexesUsingBlock:^(NSUInteger statusCode, BOOL *stop) {
Expand Down

0 comments on commit 3e8addb

Please sign in to comment.