Skip to content

Commit

Permalink
Feature/wk web view (AFNetworking#4439)
Browse files Browse the repository at this point in the history
* Removed UIWebView in favor of WKWebView

Since UIWebView is deprecated and will be rejected soon by Apple i've removed all references to UIWebView and implemented the WKWebView extension

* Fixed one last reference

* Added WKNavigation to LoadRequest calls

- WKNavigation is needed to call didFinishNavigation and didStartProvisionalNavigation
- Fixed testcases (not sure if this is the right way?)

* Added correct Copyright stuffs

* Updated podsubspec UIKit iOS deployment target to 9.0

* Updated test progressIsSet by using a large download URL request.

* XCTFailing and fulfilling in error cases of AFWKWebViewTests

* Adopted WKNavigationDelegate protocol on AFWKWebViewTetsts

* Revert "Updated podsubspec UIKit iOS deployment target to 9.0"

This reverts commit 4a4013a.

Co-authored-by: Sebastiaan <[email protected]>
  • Loading branch information
tjanela and Sebastiaan authored Mar 27, 2020
1 parent 92bb7dd commit f484fdc
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 104 deletions.
25 changes: 13 additions & 12 deletions AFNetworking.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@
299522AD1BBF13C700859F49 /* UIProgressView+AFNetworking.m in Sources */ = {isa = PBXBuildFile; fileRef = 299522971BBF13C700859F49 /* UIProgressView+AFNetworking.m */; };
299522AE1BBF13C700859F49 /* UIRefreshControl+AFNetworking.h in Headers */ = {isa = PBXBuildFile; fileRef = 299522981BBF13C700859F49 /* UIRefreshControl+AFNetworking.h */; settings = {ATTRIBUTES = (Public, ); }; };
299522AF1BBF13C700859F49 /* UIRefreshControl+AFNetworking.m in Sources */ = {isa = PBXBuildFile; fileRef = 299522991BBF13C700859F49 /* UIRefreshControl+AFNetworking.m */; };
299522B01BBF13C700859F49 /* UIWebView+AFNetworking.h in Headers */ = {isa = PBXBuildFile; fileRef = 2995229A1BBF13C700859F49 /* UIWebView+AFNetworking.h */; settings = {ATTRIBUTES = (Public, ); }; };
299522B11BBF13C700859F49 /* UIWebView+AFNetworking.m in Sources */ = {isa = PBXBuildFile; fileRef = 2995229B1BBF13C700859F49 /* UIWebView+AFNetworking.m */; };
29D3413F1C20D46400A7D266 /* AFCompoundResponseSerializerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 29D3413E1C20D46400A7D266 /* AFCompoundResponseSerializerTests.m */; };
29D341401C20D46400A7D266 /* AFCompoundResponseSerializerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 29D3413E1C20D46400A7D266 /* AFCompoundResponseSerializerTests.m */; };
29D341411C20D46400A7D266 /* AFCompoundResponseSerializerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 29D3413E1C20D46400A7D266 /* AFCompoundResponseSerializerTests.m */; };
Expand Down Expand Up @@ -170,11 +168,13 @@
29D96E981BCC406B00F571A5 /* UIImage+AFNetworking.h in Headers */ = {isa = PBXBuildFile; fileRef = 299522921BBF13C700859F49 /* UIImage+AFNetworking.h */; settings = {ATTRIBUTES = (Public, ); }; };
29D96E991BCC406B00F571A5 /* UIImageView+AFNetworking.h in Headers */ = {isa = PBXBuildFile; fileRef = 299522931BBF13C700859F49 /* UIImageView+AFNetworking.h */; settings = {ATTRIBUTES = (Public, ); }; };
29D96E9A1BCC406B00F571A5 /* UIProgressView+AFNetworking.h in Headers */ = {isa = PBXBuildFile; fileRef = 299522961BBF13C700859F49 /* UIProgressView+AFNetworking.h */; settings = {ATTRIBUTES = (Public, ); }; };
29F5EF031C47E64F008B976A /* AFUIWebViewTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 29F5EF021C47E64F008B976A /* AFUIWebViewTests.m */; };
2D4563901DB1179D00AE4812 /* AFXMLParserResponseSerializerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D45638F1DB1179D00AE4812 /* AFXMLParserResponseSerializerTests.m */; };
2D4563911DB117A200AE4812 /* AFXMLParserResponseSerializerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D45638F1DB1179D00AE4812 /* AFXMLParserResponseSerializerTests.m */; };
2D4563921DB117A200AE4812 /* AFXMLParserResponseSerializerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D45638F1DB1179D00AE4812 /* AFXMLParserResponseSerializerTests.m */; };
2D4563941DB11DDB00AE4812 /* AFXMLDocumentResponseSerializerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D4563931DB11DDB00AE4812 /* AFXMLDocumentResponseSerializerTests.m */; };
323D83E2231D185400C5BFC6 /* WKWebView+AFNetworking.h in Headers */ = {isa = PBXBuildFile; fileRef = 323D83E0231D185400C5BFC6 /* WKWebView+AFNetworking.h */; settings = {ATTRIBUTES = (Public, ); }; };
323D83E3231D185400C5BFC6 /* WKWebView+AFNetworking.m in Sources */ = {isa = PBXBuildFile; fileRef = 323D83E1231D185400C5BFC6 /* WKWebView+AFNetworking.m */; };
323D83E5231D188400C5BFC6 /* AFWKWebViewTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 323D83E4231D188400C5BFC6 /* AFWKWebViewTests.m */; };
5F4323BB1BF63741003B8749 /* Equifax_Secure_Certificate_Authority_Root.cer in Resources */ = {isa = PBXBuildFile; fileRef = 5F4323B31BF63741003B8749 /* Equifax_Secure_Certificate_Authority_Root.cer */; };
5F4323BC1BF63741003B8749 /* Equifax_Secure_Certificate_Authority_Root.cer in Resources */ = {isa = PBXBuildFile; fileRef = 5F4323B31BF63741003B8749 /* Equifax_Secure_Certificate_Authority_Root.cer */; };
5F4323BD1BF63741003B8749 /* Equifax_Secure_Certificate_Authority_Root.cer in Resources */ = {isa = PBXBuildFile; fileRef = 5F4323B31BF63741003B8749 /* Equifax_Secure_Certificate_Authority_Root.cer */; };
Expand Down Expand Up @@ -303,12 +303,12 @@
299522971BBF13C700859F49 /* UIProgressView+AFNetworking.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIProgressView+AFNetworking.m"; sourceTree = "<group>"; };
299522981BBF13C700859F49 /* UIRefreshControl+AFNetworking.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIRefreshControl+AFNetworking.h"; sourceTree = "<group>"; };
299522991BBF13C700859F49 /* UIRefreshControl+AFNetworking.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIRefreshControl+AFNetworking.m"; sourceTree = "<group>"; };
2995229A1BBF13C700859F49 /* UIWebView+AFNetworking.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIWebView+AFNetworking.h"; sourceTree = "<group>"; };
2995229B1BBF13C700859F49 /* UIWebView+AFNetworking.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIWebView+AFNetworking.m"; sourceTree = "<group>"; };
29D3413E1C20D46400A7D266 /* AFCompoundResponseSerializerTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFCompoundResponseSerializerTests.m; sourceTree = "<group>"; };
29F5EF021C47E64F008B976A /* AFUIWebViewTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFUIWebViewTests.m; sourceTree = "<group>"; };
2D45638F1DB1179D00AE4812 /* AFXMLParserResponseSerializerTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFXMLParserResponseSerializerTests.m; sourceTree = "<group>"; };
2D4563931DB11DDB00AE4812 /* AFXMLDocumentResponseSerializerTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFXMLDocumentResponseSerializerTests.m; sourceTree = "<group>"; };
323D83E0231D185400C5BFC6 /* WKWebView+AFNetworking.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "WKWebView+AFNetworking.h"; sourceTree = "<group>"; };
323D83E1231D185400C5BFC6 /* WKWebView+AFNetworking.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "WKWebView+AFNetworking.m"; sourceTree = "<group>"; };
323D83E4231D188400C5BFC6 /* AFWKWebViewTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFWKWebViewTests.m; sourceTree = "<group>"; };
5F4323B31BF63741003B8749 /* Equifax_Secure_Certificate_Authority_Root.cer */ = {isa = PBXFileReference; lastKnownFileType = file; path = Equifax_Secure_Certificate_Authority_Root.cer; sourceTree = "<group>"; };
5F4323B41BF63741003B8749 /* GeoTrust_Global_CA-cross.cer */ = {isa = PBXFileReference; lastKnownFileType = file; path = "GeoTrust_Global_CA-cross.cer"; sourceTree = "<group>"; };
5F4323B51BF63741003B8749 /* google.com.cer */ = {isa = PBXFileReference; lastKnownFileType = file; path = google.com.cer; sourceTree = "<group>"; };
Expand Down Expand Up @@ -473,7 +473,7 @@
298D7C8D1BC2C88F00FD3B3E /* AFUIImageViewTests.m */,
298D7C8E1BC2C88F00FD3B3E /* AFUIRefreshControlTests.m */,
2960BAC21C1B2F1A00BA02F0 /* AFUIButtonTests.m */,
29F5EF021C47E64F008B976A /* AFUIWebViewTests.m */,
323D83E4231D188400C5BFC6 /* AFWKWebViewTests.m */,
);
name = "AFNetworking UIKit Tests";
sourceTree = "<group>";
Expand Down Expand Up @@ -557,8 +557,8 @@
299522971BBF13C700859F49 /* UIProgressView+AFNetworking.m */,
299522981BBF13C700859F49 /* UIRefreshControl+AFNetworking.h */,
299522991BBF13C700859F49 /* UIRefreshControl+AFNetworking.m */,
2995229A1BBF13C700859F49 /* UIWebView+AFNetworking.h */,
2995229B1BBF13C700859F49 /* UIWebView+AFNetworking.m */,
323D83E0231D185400C5BFC6 /* WKWebView+AFNetworking.h */,
323D83E1231D185400C5BFC6 /* WKWebView+AFNetworking.m */,
);
path = "UIKit+AFNetworking";
sourceTree = "<group>";
Expand Down Expand Up @@ -615,11 +615,11 @@
299522A91BBF13C700859F49 /* UIImageView+AFNetworking.h in Headers */,
2995229E1BBF13C700859F49 /* AFImageDownloader.h in Headers */,
2995225E1BBF125A00859F49 /* AFURLSessionManager.h in Headers */,
323D83E2231D185400C5BFC6 /* WKWebView+AFNetworking.h in Headers */,
2995225C1BBF125A00859F49 /* AFURLResponseSerialization.h in Headers */,
299522A21BBF13C700859F49 /* UIActivityIndicatorView+AFNetworking.h in Headers */,
1F96D2A4203649560085FC3F /* AFCompatibilityMacros.h in Headers */,
2995223D1BBF104D00859F49 /* AFNetworking.h in Headers */,
299522B01BBF13C700859F49 /* UIWebView+AFNetworking.h in Headers */,
299522AC1BBF13C700859F49 /* UIProgressView+AFNetworking.h in Headers */,
299522A61BBF13C700859F49 /* UIButton+AFNetworking.h in Headers */,
299522A01BBF13C700859F49 /* AFNetworkActivityIndicatorManager.h in Headers */,
Expand Down Expand Up @@ -824,6 +824,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
);
mainGroup = 2995222F1BBF104D00859F49;
Expand Down Expand Up @@ -1017,8 +1018,8 @@
298D7CD91BC2CAF200FD3B3E /* AFNetworkReachabilityManagerTests.m in Sources */,
297824AA1BC2DAD80041C395 /* AFAutoPurgingImageCacheTests.m in Sources */,
298D7C981BC2CA2500FD3B3E /* AFURLSessionManagerTests.m in Sources */,
323D83E5231D188400C5BFC6 /* AFWKWebViewTests.m in Sources */,
297824AC1BC2DB450041C395 /* AFImageDownloaderTests.m in Sources */,
29F5EF031C47E64F008B976A /* AFUIWebViewTests.m in Sources */,
2D4563901DB1179D00AE4812 /* AFXMLParserResponseSerializerTests.m in Sources */,
298D7CD51BC2CAEC00FD3B3E /* AFHTTPSessionManagerTests.m in Sources */,
298D7CD71BC2CAEF00FD3B3E /* AFJSONSerializationTests.m in Sources */,
Expand Down Expand Up @@ -1055,10 +1056,10 @@
299522571BBF125A00859F49 /* AFNetworkReachabilityManager.m in Sources */,
299522AF1BBF13C700859F49 /* UIRefreshControl+AFNetworking.m in Sources */,
299522AA1BBF13C700859F49 /* UIImageView+AFNetworking.m in Sources */,
299522B11BBF13C700859F49 /* UIWebView+AFNetworking.m in Sources */,
299522591BBF125A00859F49 /* AFSecurityPolicy.m in Sources */,
299522A71BBF13C700859F49 /* UIButton+AFNetworking.m in Sources */,
299522541BBF125A00859F49 /* AFHTTPSessionManager.m in Sources */,
323D83E3231D185400C5BFC6 /* WKWebView+AFNetworking.m in Sources */,
2995225F1BBF125A00859F49 /* AFURLSessionManager.m in Sources */,
2995225B1BBF125A00859F49 /* AFURLRequestSerialization.m in Sources */,
2995229D1BBF13C700859F49 /* AFAutoPurgingImageCache.m in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion Framework/AFNetworking.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ FOUNDATION_EXPORT const unsigned char AFNetworkingVersionString[];
#if TARGET_OS_IOS
#import <AFNetworking/AFNetworkActivityIndicatorManager.h>
#import <AFNetworking/UIRefreshControl+AFNetworking.h>
#import <AFNetworking/UIWebView+AFNetworking.h>
#import <AFNetworking/WKWebView+AFNetworking.h>
#endif


Expand Down
102 changes: 67 additions & 35 deletions Tests/Tests/AFUIWebViewTests.m → Tests/Tests/AFWKWebViewTests.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// AFUIWebViewTests.h
// AFWKWebViewTests.m
// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
Expand All @@ -20,85 +20,117 @@
// THE SOFTWARE.

#import <XCTest/XCTest.h>
#import <WebKit/WebKit.h>
#import "AFTestCase.h"
#import "UIWebView+AFNetworking.h"
#import "WKWebView+AFNetworking.h"

@interface AFUIWebViewTests : AFTestCase
@interface AFWKWebViewTests : AFTestCase <WKNavigationDelegate>

@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic, strong) WKNavigation *navigation;
@property (nonatomic, strong) NSURLRequest *HTMLRequest;
@property (nonatomic, strong) NSURLRequest *largeHTMLRequest;
@property (nonatomic, strong) NSURLRequest *headerRequest;
@property (nonatomic, strong) NSProgress *progressCapture;

@end

@implementation AFUIWebViewTests
@implementation AFWKWebViewTests

- (void)setUp {
-(void)setUp {
[super setUp];
self.webView = [UIWebView new];
self.HTMLRequest = [NSURLRequest requestWithURL:[self.baseURL URLByAppendingPathComponent:@"html"]];
self.webView = [WKWebView new];
self.webView.navigationDelegate = self;
self.navigation = [WKNavigation new];
self.HTMLRequest = [NSURLRequest requestWithURL:[self.baseURL URLByAppendingPathComponent:@"html"]
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:self.networkTimeout];
NSURL * largeURL = [[self.baseURL URLByAppendingPathComponent:@"bytes"] URLByAppendingPathComponent:@(1024 * 1024).stringValue];
self.largeHTMLRequest = [NSURLRequest requestWithURL:largeURL
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:self.networkTimeout];
NSMutableURLRequest *customHeaderRequest = [NSMutableURLRequest requestWithURL:[self.baseURL URLByAppendingPathComponent:@"headers"]];
[customHeaderRequest setValue:@"Custom-Header-Value" forHTTPHeaderField:@"Custom-Header-Field"];
self.headerRequest = customHeaderRequest;
}

- (void)testNilProgressDoesNotCauseCrash {
XCTestExpectation *expectation = [self expectationWithDescription:@"Request should succeed"];
[self.webView
loadRequest:self.HTMLRequest
navigation:self.navigation
progress:nil
success:^NSString * _Nonnull(NSHTTPURLResponse * _Nonnull response, NSString * _Nonnull HTML) {
[expectation fulfill];
return HTML;
}
failure:nil];
} failure:^(NSError * _Nonnull error) {
XCTFail(@"Request %@ failed with error %@", self.HTMLRequest, error);
[expectation fulfill];
}];
[self waitForExpectationsWithCommonTimeout];
}

- (void)testNULLProgressDoesNotCauseCrash {
- (void)testNUllProgressDoesNotCauseCrash {
XCTestExpectation *expectation = [self expectationWithDescription:@"Request should succeed"];
[self.webView
loadRequest:self.HTMLRequest
navigation:self.navigation
progress:NULL
success:^NSString * _Nonnull(NSHTTPURLResponse * _Nonnull response, NSString * _Nonnull HTML) {
[expectation fulfill];
return HTML;
}
failure:nil];
} failure:^(NSError * _Nonnull error) {
XCTFail(@"Request %@ failed with error %@", self.HTMLRequest, error);
[expectation fulfill];
}];
[self waitForExpectationsWithCommonTimeout];
}

//- (void)testProgressIsSet {
// NSProgress* progress = nil;
// XCTestExpectation *expectation = [self expectationWithDescription:@"Request should succeed"];
// [self.webView
// loadRequest:self.HTMLRequest
// progress:&progress
// success:^NSString * _Nonnull(NSHTTPURLResponse * _Nonnull response, NSString * _Nonnull HTML) {
// [expectation fulfill];
// return HTML;
// }
// failure:nil];
// [self keyValueObservingExpectationForObject:progress
// keyPath:@"fractionCompleted"
// expectedValue:@(1.0)];
// [self waitForExpectationsWithCommonTimeout];
//}
- (void)testProgressIsSet {
NSProgress* progress = nil;
XCTestExpectation *expectation = [self expectationWithDescription:@"Request should succeed"];

[self.webView
loadRequest:self.largeHTMLRequest
navigation:self.navigation
progress:&progress
success:^NSString * _Nonnull(NSHTTPURLResponse * _Nonnull response, NSString * _Nonnull HTML) {
[expectation fulfill];
return HTML;
} failure:^(NSError * _Nonnull error) {
XCTFail(@"Request %@ failed with error %@", self.largeHTMLRequest, error);
[expectation fulfill];
}];
[self keyValueObservingExpectationForObject:progress
keyPath:@"fractionCompleted"
expectedValue:@(1.0)];
[self waitForExpectationsWithCommonTimeout];
}

- (void)testRequestWithCustomHeaders {
NSMutableURLRequest *customHeaderRequest = [NSMutableURLRequest requestWithURL:[self.baseURL URLByAppendingPathComponent:@"headers"]];
[customHeaderRequest setValue:@"Custom-Header-Value" forHTTPHeaderField:@"Custom-Header-Field"];

XCTestExpectation *expectation = [self expectationWithDescription:@"Request should succeed"];
[self.webView
loadRequest:customHeaderRequest
loadRequest:self.headerRequest
navigation:self.navigation
progress:NULL
success:^NSString * _Nonnull(NSHTTPURLResponse * _Nonnull response, NSString * _Nonnull string) {
// Here string is actually JSON.
// Here string is actually JSON
NSDictionary<NSString *, NSDictionary *> *responseObject = [NSJSONSerialization JSONObjectWithData:[string dataUsingEncoding:NSUTF8StringEncoding] options:(NSJSONReadingOptions)0 error:nil];

NSDictionary<NSString *, NSString *> *headers = responseObject[@"headers"];
XCTAssertTrue([headers[@"Custom-Header-Field"] isEqualToString:@"Custom-Header-Value"]);
[expectation fulfill];
return string;
}
failure:nil];
} failure:^(NSError * _Nonnull error) {
XCTFail(@"Request %@ failed with error %@", self.headerRequest, error);
[expectation fulfill];
}];
[self waitForExpectationsWithCommonTimeout];
}

- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
XCTFail(@"Navigation failed with error %@", error);
}

@end
2 changes: 1 addition & 1 deletion UIKit+AFNetworking/UIKit+AFNetworking.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#import "AFImageDownloader.h"
#import "AFNetworkActivityIndicatorManager.h"
#import "UIRefreshControl+AFNetworking.h"
#import "UIWebView+AFNetworking.h"
#import "WKWebView+AFNetworking.h"
#endif

#import "UIActivityIndicatorView+AFNetworking.h"
Expand Down
Loading

0 comments on commit f484fdc

Please sign in to comment.