Added test case `test29AcceptableStatusCodeAndContentType`

This commit is contained in:
DreamPiggy 2021-05-07 16:26:52 +08:00
parent df7d56373d
commit 43219b0739
2 changed files with 37 additions and 2 deletions

View File

@ -628,8 +628,8 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
#pragma mark - SDImageCache & SDImageCachesManager
- (void)test49SDImageCacheQueryOp {
XCTestExpectation *expectation = [self expectationWithDescription:@"SDImageCache query op works"];
NSData *data = [[SDImageCodersManager sharedManager] encodedDataWithImage:[self testJPEGImage] format:SDImageFormatJPEG options:nil];
[[SDImageCache sharedImageCache] storeImageDataToDisk:data forKey:kTestImageKeyJPEG];
NSData *imageData = [[SDImageCodersManager sharedManager] encodedDataWithImage:[self testJPEGImage] format:SDImageFormatJPEG options:nil];
[[SDImageCache sharedImageCache] storeImageDataToDisk:imageData forKey:kTestImageKeyJPEG];
[[SDImageCachesManager sharedManager] queryImageForKey:kTestImageKeyJPEG options:0 context:@{SDWebImageContextStoreCacheType : @(SDImageCacheTypeDisk)} cacheType:SDImageCacheTypeAll completion:^(UIImage * _Nullable image, NSData * _Nullable data, SDImageCacheType cacheType) {
expect(image).notTo.beNil();

View File

@ -733,6 +733,41 @@
}];
}
- (void)test29AcceptableStatusCodeAndContentType {
SDWebImageDownloaderConfig *config1 = [[SDWebImageDownloaderConfig alloc] init];
config1.acceptableStatusCodes = [NSIndexSet indexSetWithIndex:1];
SDWebImageDownloader *downloader1 = [[SDWebImageDownloader alloc] initWithConfig:config1];
XCTestExpectation *expectation1 = [self expectationWithDescription:@"Acceptable status code should work"];
SDWebImageDownloaderConfig *config2 = [[SDWebImageDownloaderConfig alloc] init];
config2.acceptableContentTypes = [NSSet setWithArray:@[@"application/json"]];
SDWebImageDownloader *downloader2 = [[SDWebImageDownloader alloc] initWithConfig:config2];
XCTestExpectation *expectation2 = [self expectationWithDescription:@"Acceptable content type should work"];
__block SDWebImageDownloadToken *token1;
token1 = [downloader1 downloadImageWithURL:[NSURL URLWithString:kTestJPEGURL] completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
expect(error).notTo.beNil();
expect(error.code).equal(SDWebImageErrorInvalidDownloadStatusCode);
NSInteger statusCode = ((NSHTTPURLResponse *)token1.response).statusCode;
expect(statusCode).equal(200);
[expectation1 fulfill];
}];
__block SDWebImageDownloadToken *token2;
token2 = [downloader2 downloadImageWithURL:[NSURL URLWithString:kTestJPEGURL] completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
expect(error).notTo.beNil();
expect(error.code).equal(SDWebImageErrorInvalidDownloadContentType);
NSString *contentType = ((NSHTTPURLResponse *)token1.response).MIMEType;
expect(contentType).equal(@"image/jpeg");
[expectation2 fulfill];
}];
[self waitForExpectationsWithCommonTimeoutUsingHandler:^(NSError * _Nullable error) {
[downloader1 invalidateSessionAndCancel:YES];
[downloader2 invalidateSessionAndCancel:YES];
}];
}
#pragma mark - SDWebImageLoader
- (void)test30CustomImageLoaderWorks {
XCTestExpectation *expectation = [self expectationWithDescription:@"Custom image not works"];