diff --git a/AFNetworking/AFURLResponseSerialization.m b/AFNetworking/AFURLResponseSerialization.m index fe1fc06b13..5e4679928d 100755 --- a/AFNetworking/AFURLResponseSerialization.m +++ b/AFNetworking/AFURLResponseSerialization.m @@ -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]], diff --git a/Tests/Tests/AFHTTPResponseSerializationTests.m b/Tests/Tests/AFHTTPResponseSerializationTests.m index 5078867580..a3aae2255c 100644 --- a/Tests/Tests/AFHTTPResponseSerializationTests.m +++ b/Tests/Tests/AFHTTPResponseSerializationTests.m @@ -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) {