| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #import "ios/crnet/CrNet.h" | 8 #import "ios/crnet/CrNet.h" |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/mac/scoped_nsobject.h" | 11 #include "base/mac/scoped_nsobject.h" |
| 12 #include "base/strings/sys_string_conversions.h" | 12 #include "base/strings/sys_string_conversions.h" |
| 13 #import "ios/third_party/gcdwebserver/src/GCDWebServer/Core/GCDWebServer.h" | 13 #import "ios/third_party/gcdwebserver/src/GCDWebServer/Core/GCDWebServer.h" |
| 14 #import "ios/third_party/gcdwebserver/src/GCDWebServer/Responses/GCDWebServerDat
aResponse.h" |
| 14 #include "net/base/mac/url_conversions.h" | 15 #include "net/base/mac/url_conversions.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "testing/gtest_mac.h" | 17 #include "testing/gtest_mac.h" |
| 17 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 18 | 19 |
| 19 @interface TestDelegate : NSObject<NSURLSessionDataDelegate, | 20 @interface TestDelegate : NSObject<NSURLSessionDataDelegate, |
| 20 NSURLSessionDelegate, | 21 NSURLSessionDelegate, |
| 21 NSURLSessionTaskDelegate> | 22 NSURLSessionTaskDelegate> |
| 22 | 23 |
| 23 // Completion semaphore for this TestDelegate. When the request this delegate is | 24 // Completion semaphore for this TestDelegate. When the request this delegate is |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 completionHandler: | 93 completionHandler: |
| 93 (void (^)(NSCachedURLResponse* cachedResponse))completionHandler { | 94 (void (^)(NSCachedURLResponse* cachedResponse))completionHandler { |
| 94 completionHandler(proposedResponse); | 95 completionHandler(proposedResponse); |
| 95 } | 96 } |
| 96 | 97 |
| 97 @end | 98 @end |
| 98 | 99 |
| 99 // base::TimeDelta would normally be ideal for this but it does not support | 100 // base::TimeDelta would normally be ideal for this but it does not support |
| 100 // nanosecond resolution. | 101 // nanosecond resolution. |
| 101 static const int64_t ns_in_second = 1000000000LL; | 102 static const int64_t ns_in_second = 1000000000LL; |
| 103 const char kUserAgent[] = "CrNetTest/1.0.0.0"; |
| 102 | 104 |
| 103 class HttpTest : public ::testing::Test { | 105 class HttpTest : public ::testing::Test { |
| 104 protected: | 106 protected: |
| 105 HttpTest() {} | 107 HttpTest() {} |
| 106 ~HttpTest() override {} | 108 ~HttpTest() override {} |
| 107 | 109 |
| 108 void SetUp() override { | 110 void SetUp() override { |
| 109 [CrNet setPartialUserAgent:@"CrNetTest/1.0.0.0"]; | 111 [CrNet setUserAgent:base::SysUTF8ToNSString(kUserAgent) partial:NO]; |
| 110 [CrNet install]; | 112 [CrNet install]; |
| 111 NSURLSessionConfiguration* config = | 113 NSURLSessionConfiguration* config = |
| 112 [NSURLSessionConfiguration ephemeralSessionConfiguration]; | 114 [NSURLSessionConfiguration ephemeralSessionConfiguration]; |
| 113 [CrNet installIntoSessionConfiguration:config]; | 115 [CrNet installIntoSessionConfiguration:config]; |
| 114 delegate_.reset([[TestDelegate alloc] init]); | 116 delegate_.reset([[TestDelegate alloc] init]); |
| 115 NSURLSession* session = [NSURLSession sessionWithConfiguration:config | 117 NSURLSession* session = [NSURLSession sessionWithConfiguration:config |
| 116 delegate:delegate_ | 118 delegate:delegate_ |
| 117 delegateQueue:nil]; | 119 delegateQueue:nil]; |
| 118 // Take a reference to the session and store it so it doesn't get | 120 // Take a reference to the session and store it so it doesn't get |
| 119 // deallocated until this object does. | 121 // deallocated until this object does. |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 StartWebServer(); | 203 StartWebServer(); |
| 202 | 204 |
| 203 NSURL* url = net::NSURLWithGURL(GetURL(kPath)); | 205 NSURL* url = net::NSURLWithGURL(GetURL(kPath)); |
| 204 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; | 206 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; |
| 205 StartDataTaskAndWaitForCompletion(task); | 207 StartDataTaskAndWaitForCompletion(task); |
| 206 EXPECT_EQ(nil, [delegate_ error]); | 208 EXPECT_EQ(nil, [delegate_ error]); |
| 207 EXPECT_EQ(strlen(kData), [delegate_ receivedBytes]); | 209 EXPECT_EQ(strlen(kData), [delegate_ receivedBytes]); |
| 208 } | 210 } |
| 209 | 211 |
| 210 TEST_F(HttpTest, SdchDisabledByDefault) { | 212 TEST_F(HttpTest, SdchDisabledByDefault) { |
| 211 const char kPath[] = "/foo"; | 213 const char kPath[] = "/sdchtest"; |
| 212 RegisterPathHandler(kPath, | 214 RegisterPathHandler(kPath, |
| 213 ^GCDWebServerResponse* (GCDWebServerRequest* req) { | 215 ^GCDWebServerResponse* (GCDWebServerRequest* req) { |
| 214 EXPECT_FALSE(HeaderValueContains(req, "Accept-Encoding", "sdch")); | 216 EXPECT_FALSE(HeaderValueContains(req, "Accept-Encoding", "sdch")); |
| 215 return nil; | 217 return [GCDWebServerDataResponse responseWithText:@"woot!"]; |
| 216 }); | 218 }); |
| 217 StartWebServer(); | 219 StartWebServer(); |
| 218 NSURL* url = net::NSURLWithGURL(GetURL(kPath)); | 220 NSURL* url = net::NSURLWithGURL(GetURL(kPath)); |
| 219 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; | 221 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; |
| 220 StartDataTaskAndWaitForCompletion(task); | 222 StartDataTaskAndWaitForCompletion(task); |
| 221 EXPECT_EQ(nil, [delegate_ error]); | 223 EXPECT_EQ(nil, [delegate_ error]); |
| 222 EXPECT_TRUE([delegate_ receivedBytes]); | 224 EXPECT_TRUE([delegate_ receivedBytes]); |
| 223 } | 225 } |
| 224 | 226 |
| 227 TEST_F(HttpTest, SetUserAgentIsExact) { |
| 228 const char kPath[] = "/uatest"; |
| 229 RegisterPathHandler(kPath, ^GCDWebServerResponse*(GCDWebServerRequest* req) { |
| 230 EXPECT_STREQ(kUserAgent, |
| 231 [[req.headers valueForKey:@"User-Agent"] UTF8String]); |
| 232 return [GCDWebServerDataResponse responseWithText:@"yay!"]; |
| 233 }); |
| 234 StartWebServer(); |
| 235 NSURL* url = net::NSURLWithGURL(GetURL(kPath)); |
| 236 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; |
| 237 StartDataTaskAndWaitForCompletion(task); |
| 238 EXPECT_EQ(nil, [delegate_ error]); |
| 239 EXPECT_TRUE([delegate_ receivedBytes]); |
| 240 } |
| 241 |
| 225 // TODO(ellyjones): There needs to be a test that enabling SDCH works, but | 242 // TODO(ellyjones): There needs to be a test that enabling SDCH works, but |
| 226 // because CrNet is static and 'uninstall' only disables it, there is no way to | 243 // because CrNet is static and 'uninstall' only disables it, there is no way to |
| 227 // have an individual test enable or disable SDCH. | 244 // have an individual test enable or disable SDCH. |
| 228 // Probably there is a way to get gtest tests to run in a separate process, but | 245 // Probably there is a way to get gtest tests to run in a separate process, but |
| 229 // I'm not sure what it is. | 246 // I'm not sure what it is. |
| OLD | NEW |