| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import <Foundation/Foundation.h> | 5 #import <Foundation/Foundation.h> |
| 6 #import <UIKit/UIKit.h> | 6 |
| 7 #import "CrNet.h" |
| 7 | 8 |
| 8 #import "crnet_consumer_app_delegate.h" | 9 #import "crnet_consumer_app_delegate.h" |
| 9 | 10 |
| 11 @interface TestDelegate : NSObject <NSURLSessionDelegate, |
| 12 NSURLSessionDataDelegate, |
| 13 NSURLSessionTaskDelegate> |
| 14 |
| 15 - (id)initWithSemaphore:(dispatch_semaphore_t)sem; |
| 16 |
| 17 @end |
| 18 |
| 19 @implementation TestDelegate { |
| 20 dispatch_semaphore_t _sem; |
| 21 } |
| 22 |
| 23 - (id)initWithSemaphore:(dispatch_semaphore_t)sem { |
| 24 _sem = sem; |
| 25 return self; |
| 26 } |
| 27 |
| 28 - (void)URLSession:(NSURLSession *)session |
| 29 didBecomeInvalidWithError:(NSError *)error { |
| 30 NSLog(@"URLSession didBecomeInvalidWithError %@", error); |
| 31 } |
| 32 |
| 33 - (void)URLSession:(NSURLSession *)session |
| 34 task:(NSURLSessionTask *)task |
| 35 didCompleteWithError:(NSError *)error { |
| 36 NSLog(@"URLSessionTask didCompleteWithError %@", error); |
| 37 dispatch_semaphore_signal(_sem); |
| 38 } |
| 39 |
| 40 - (void)URLSession:(NSURLSession *)session |
| 41 task:(NSURLSessionTask *)task |
| 42 didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge |
| 43 completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disp, |
| 44 NSURLCredential* credential))completionHandler { |
| 45 NSLog(@"URLSessionTask didReceiveChallenge %@", challenge); |
| 46 completionHandler(NSURLSessionAuthChallengeUseCredential, nil); |
| 47 } |
| 48 |
| 49 - (void)URLSession:(NSURLSession *)session |
| 50 task:(NSURLSessionTask *)task |
| 51 willPerformHTTPRedirection:(NSHTTPURLResponse *)response |
| 52 newRequest:(NSURLRequest *)request |
| 53 completionHandler:(void (^)(NSURLRequest *))completionHandler { |
| 54 NSLog(@"URLSessionTask willPerformHttpRedirection %@", request); |
| 55 completionHandler(request); |
| 56 } |
| 57 |
| 58 - (void)URLSession:(NSURLSession *)session |
| 59 dataTask:(NSURLSessionDataTask *)dataTask |
| 60 didReceiveResponse:(NSURLResponse *)response |
| 61 completionHandler:(void (^)(NSURLSessionResponseDisposition |
| 62 disposition))completionHandler { |
| 63 NSHTTPURLResponse* resp = (NSHTTPURLResponse *)response; |
| 64 NSLog(@"URLSessionDataTask didReceiveResponse status %ld", |
| 65 (long)resp.statusCode); |
| 66 completionHandler(NSURLSessionResponseAllow); |
| 67 } |
| 68 |
| 69 - (void)URLSession:(NSURLSession *)session |
| 70 dataTask:(NSURLSessionDataTask *)dataTask |
| 71 didReceiveData:(NSData *)data { |
| 72 NSLog(@"URLSessionDataTask didReceiveData %lu bytes", |
| 73 (unsigned long)data.length); |
| 74 } |
| 75 |
| 76 - (void)URLSession:(NSURLSession *)session |
| 77 dataTask:(NSURLSessionDataTask *)dataTask |
| 78 willCacheResponse:(NSCachedURLResponse *)proposedResponse |
| 79 completionHandler:(void (^)(NSCachedURLResponse |
| 80 *cachedResponse))completionHandler { |
| 81 NSLog(@"URLSessionDataTask willCacheResponse %@", proposedResponse); |
| 82 completionHandler(proposedResponse); |
| 83 } |
| 84 |
| 85 @end |
| 86 |
| 87 void use_crnet(NSURLSessionConfiguration* config) { |
| 88 [CrNet setPartialUserAgent:@"Foo/1.0"]; |
| 89 [CrNet install]; |
| 90 [CrNet installIntoSessionConfiguration:config]; |
| 91 } |
| 92 |
| 93 void fetch(NSURLSession* session, NSString* url) { |
| 94 NSURL* testURL = [NSURL URLWithString:url]; |
| 95 NSURLSessionDataTask* task = [session dataTaskWithURL:testURL]; |
| 96 NSLog(@"fetch: starting %@", url); |
| 97 [task resume]; |
| 98 } |
| 99 |
| 10 int main(int argc, char *argv[]) { | 100 int main(int argc, char *argv[]) { |
| 11 @autoreleasepool { | 101 dispatch_semaphore_t sem = dispatch_semaphore_create(0); |
| 12 return UIApplicationMain( | 102 TestDelegate* delegate = [[TestDelegate alloc] initWithSemaphore:sem]; |
| 13 argc, argv, nil, NSStringFromClass([CrNetConsumerAppDelegate class])); | 103 NSURLSessionConfiguration* config = |
| 14 } | 104 [NSURLSessionConfiguration ephemeralSessionConfiguration]; |
| 105 NSURLSession* session = |
| 106 [NSURLSession sessionWithConfiguration:config |
| 107 delegate:delegate |
| 108 delegateQueue:nil]; |
| 109 |
| 110 NSLog(@"main: installing crnet"); |
| 111 use_crnet(config); |
| 112 |
| 113 fetch(session, @"https://www.google.com"); |
| 114 fetch(session, @"https://twitter.com"); |
| 115 fetch(session, @"https://m.facebook.com"); |
| 116 fetch(session, @"https://www.yahoo.com"); |
| 117 |
| 118 int64_t secs = 1000000000LL; |
| 119 secs *= 5LL; |
| 120 NSLog(@"main: waiting"); |
| 121 dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, secs)); |
| 122 dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, secs)); |
| 123 dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, secs)); |
| 124 dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, secs)); |
| 125 NSLog(@"main: timeout, exiting"); |
| 15 } | 126 } |
| OLD | NEW |