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

Fixed status code 204/205 handling #3292

Merged
merged 1 commit into from
Mar 14, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
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