Skip to content

Commit

Permalink
Merge pull request AFNetworking#3733 from skyline75489/master
Browse files Browse the repository at this point in the history
Fixed issue where UIWebView extension did not preserve all of the request information
  • Loading branch information
kcharwood authored Oct 10, 2016
2 parents e8fde52 + 0fc675e commit f39ec68
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
19 changes: 19 additions & 0 deletions Tests/Tests/AFUIWebViewTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#import "UIWebView+AFNetworking.h"

@interface AFUIWebViewTests : AFTestCase

@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) NSURLRequest *HTMLRequest;

Expand Down Expand Up @@ -80,6 +81,24 @@ - (void)testProgressIsSet {
[self waitForExpectationsWithCommonTimeoutUsingHandler:nil];
}

- (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
progress:NULL
success:^NSString * _Nonnull(NSHTTPURLResponse * _Nonnull response, NSString * _Nonnull string) {
// 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];
[self waitForExpectationsWithCommonTimeoutUsingHandler:nil];
}

@end
41 changes: 21 additions & 20 deletions UIKit+AFNetworking/UIWebView+AFNetworking.m
Original file line number Diff line number Diff line change
Expand Up @@ -119,27 +119,28 @@ - (void)loadRequest:(NSURLRequest *)request
self.af_URLSessionTask = nil;

__weak __typeof(self)weakSelf = self;
NSURLSessionDataTask *dataTask;
__block NSURLSessionDataTask *dataTask;
dataTask = [self.sessionManager
GET:request.URL.absoluteString
parameters:nil
progress:nil
success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
__strong __typeof(weakSelf) strongSelf = weakSelf;
if (success) {
success((NSHTTPURLResponse *)task.response, responseObject);
}
[strongSelf loadData:responseObject MIMEType:MIMEType textEncodingName:textEncodingName baseURL:[task.currentRequest URL]];

if ([strongSelf.delegate respondsToSelector:@selector(webViewDidFinishLoad:)]) {
[strongSelf.delegate webViewDidFinishLoad:strongSelf];
}
}
failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}
}];
dataTaskWithRequest:request
uploadProgress:nil
downloadProgress:nil
completionHandler:^(NSURLResponse * _Nonnull response, id _Nonnull responseObject, NSError * _Nullable error) {
__strong __typeof(weakSelf) strongSelf = weakSelf;
if (error) {
if (failure) {
failure(error);
}
} else {
if (success) {
success((NSHTTPURLResponse *)response, responseObject);
}
[strongSelf loadData:responseObject MIMEType:MIMEType textEncodingName:textEncodingName baseURL:[dataTask.currentRequest URL]];

if ([strongSelf.delegate respondsToSelector:@selector(webViewDidFinishLoad:)]) {
[strongSelf.delegate webViewDidFinishLoad:strongSelf];
}
}
}];
self.af_URLSessionTask = dataTask;
if (progress != nil) {
*progress = [self.sessionManager downloadProgressForTask:dataTask];
Expand Down

0 comments on commit f39ec68

Please sign in to comment.