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
3 // found in the LICENSE file.
5 #import <Foundation/Foundation.h>
9 #import "crnet_consumer_app_delegate.h"
11 @interface TestDelegate : NSObject<NSURLSessionDelegate,
12 NSURLSessionDataDelegate,
13 NSURLSessionTaskDelegate>
15 - (id)initWithSemaphore:(dispatch_semaphore_t)sem;
19 @implementation TestDelegate {
20 dispatch_semaphore_t _sem;
23 - (id)initWithSemaphore:(dispatch_semaphore_t)sem {
28 - (void)URLSession:(NSURLSession*)session
29 didBecomeInvalidWithError:(NSError*)error {
30 NSLog(@"URLSession didBecomeInvalidWithError %@", error);
33 - (void)URLSession:(NSURLSession*)session
34 task:(NSURLSessionTask*)task
35 didCompleteWithError:(NSError*)error {
36 NSLog(@"URLSessionTask didCompleteWithError %@", error);
37 dispatch_semaphore_signal(_sem);
40 - (void)URLSession:(NSURLSession*)session
41 task:(NSURLSessionTask*)task
42 didReceiveChallenge:(NSURLAuthenticationChallenge*)challenge
44 (void (^)(NSURLSessionAuthChallengeDisposition disp,
45 NSURLCredential* credential))completionHandler {
46 NSLog(@"URLSessionTask didReceiveChallenge %@", challenge);
47 completionHandler(NSURLSessionAuthChallengeUseCredential, nil);
50 - (void)URLSession:(NSURLSession*)session
51 task:(NSURLSessionTask*)task
52 willPerformHTTPRedirection:(NSHTTPURLResponse*)response
53 newRequest:(NSURLRequest*)request
54 completionHandler:(void (^)(NSURLRequest*))completionHandler {
55 NSLog(@"URLSessionTask willPerformHttpRedirection %@", request);
56 completionHandler(request);
59 - (void)URLSession:(NSURLSession*)session
60 dataTask:(NSURLSessionDataTask*)dataTask
61 didReceiveResponse:(NSURLResponse*)response
62 completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))
64 NSHTTPURLResponse* resp = (NSHTTPURLResponse*)response;
65 NSLog(@"URLSessionDataTask didReceiveResponse status %ld",
66 (long)resp.statusCode);
67 completionHandler(NSURLSessionResponseAllow);
70 - (void)URLSession:(NSURLSession*)session
71 dataTask:(NSURLSessionDataTask*)dataTask
72 didReceiveData:(NSData*)data {
73 NSLog(@"URLSessionDataTask didReceiveData %lu bytes",
74 (unsigned long)data.length);
77 - (void)URLSession:(NSURLSession*)session
78 dataTask:(NSURLSessionDataTask*)dataTask
79 willCacheResponse:(NSCachedURLResponse*)proposedResponse
81 (void (^)(NSCachedURLResponse* cachedResponse))completionHandler {
82 NSLog(@"URLSessionDataTask willCacheResponse %@", proposedResponse);
83 completionHandler(proposedResponse);
88 void use_crnet(NSURLSessionConfiguration* config) {
89 [CrNet setPartialUserAgent:@"Foo/1.0"];
91 [CrNet installIntoSessionConfiguration:config];
94 void fetch(NSURLSession* session, NSString* url) {
95 NSURL* testURL = [NSURL URLWithString:url];
96 NSURLSessionDataTask* task = [session dataTaskWithURL:testURL];
97 NSLog(@"fetch: starting %@", url);
101 int main(int argc, char *argv[]) {
102 dispatch_semaphore_t sem = dispatch_semaphore_create(0);
103 TestDelegate* delegate = [[TestDelegate alloc] initWithSemaphore:sem];
104 NSURLSessionConfiguration* config =
105 [NSURLSessionConfiguration ephemeralSessionConfiguration];
106 NSURLSession* session = [NSURLSession sessionWithConfiguration:config
110 NSLog(@"main: installing crnet");
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");
118 int64_t secs = 1000000000LL;
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");