| 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 <Cronet/Cronet.h> | 5 #import <Cronet/Cronet.h> |
| 6 #import <Foundation/Foundation.h> | 6 #import <Foundation/Foundation.h> |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.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 #include "components/cronet/ios/test/start_cronet.h" | 13 #include "components/cronet/ios/test/quic_test_server.h" |
| 14 #include "components/cronet/ios/test/test_server.h" | 14 #include "components/cronet/ios/test/test_server.h" |
| 15 #include "components/grpc_support/test/quic_test_server.h" | |
| 16 #include "net/base/mac/url_conversions.h" | 15 #include "net/base/mac/url_conversions.h" |
| 17 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 18 #include "net/cert/mock_cert_verifier.h" | 17 #include "net/cert/mock_cert_verifier.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "testing/gtest_mac.h" | 19 #include "testing/gtest_mac.h" |
| 21 | 20 |
| 22 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 23 | 22 |
| 24 @interface TestDelegate : NSObject<NSURLSessionDataDelegate, | 23 @interface TestDelegate : NSObject<NSURLSessionDataDelegate, |
| 25 NSURLSessionDelegate, | 24 NSURLSessionDelegate, |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 } | 105 } |
| 107 | 106 |
| 108 @end | 107 @end |
| 109 | 108 |
| 110 namespace cronet { | 109 namespace cronet { |
| 111 // base::TimeDelta would normally be ideal for this but it does not support | 110 // base::TimeDelta would normally be ideal for this but it does not support |
| 112 // nanosecond resolution. | 111 // nanosecond resolution. |
| 113 static const int64_t ns_in_second = 1000000000LL; | 112 static const int64_t ns_in_second = 1000000000LL; |
| 114 const char kUserAgent[] = "CronetTest/1.0.0.0"; | 113 const char kUserAgent[] = "CronetTest/1.0.0.0"; |
| 115 | 114 |
| 115 // TODO(mef): Create common header file to declare this. |
| 116 void StartCronetIfNecessary(); |
| 117 |
| 116 class HttpTest : public ::testing::Test { | 118 class HttpTest : public ::testing::Test { |
| 117 protected: | 119 protected: |
| 118 HttpTest() {} | 120 HttpTest() {} |
| 119 ~HttpTest() override {} | 121 ~HttpTest() override {} |
| 120 | 122 |
| 121 void SetUp() override { | 123 void SetUp() override { |
| 122 grpc_support::StartQuicTestServer(); | 124 StartCronetIfNecessary(); |
| 123 TestServer::Start(); | |
| 124 | |
| 125 StartCronetIfNecessary(grpc_support::GetQuicTestServerPort()); | |
| 126 NSURLSessionConfiguration* config = | 125 NSURLSessionConfiguration* config = |
| 127 [NSURLSessionConfiguration ephemeralSessionConfiguration]; | 126 [NSURLSessionConfiguration ephemeralSessionConfiguration]; |
| 128 [Cronet installIntoSessionConfiguration:config]; | 127 [Cronet installIntoSessionConfiguration:config]; |
| 129 delegate_.reset([[TestDelegate alloc] init]); | 128 delegate_.reset([[TestDelegate alloc] init]); |
| 130 NSURLSession* session = [NSURLSession sessionWithConfiguration:config | 129 NSURLSession* session = [NSURLSession sessionWithConfiguration:config |
| 131 delegate:delegate_ | 130 delegate:delegate_ |
| 132 delegateQueue:nil]; | 131 delegateQueue:nil]; |
| 133 // Take a reference to the session and store it so it doesn't get | 132 // Take a reference to the session and store it so it doesn't get |
| 134 // deallocated until this object does. | 133 // deallocated until this object does. |
| 135 session_.reset([session retain]); | 134 session_.reset([session retain]); |
| 135 StartQuicTestServer(); |
| 136 TestServer::Start(); |
| 136 } | 137 } |
| 137 | 138 |
| 138 void TearDown() override { | 139 void TearDown() override { |
| 139 grpc_support::ShutdownQuicTestServer(); | 140 ShutdownQuicTestServer(); |
| 140 TestServer::Shutdown(); | 141 TestServer::Shutdown(); |
| 141 } | 142 } |
| 142 | 143 |
| 143 // Launches the supplied |task| and blocks until it completes, with a timeout | 144 // Launches the supplied |task| and blocks until it completes, with a timeout |
| 144 // of 1 second. | 145 // of 1 second. |
| 145 void StartDataTaskAndWaitForCompletion(NSURLSessionDataTask* task) { | 146 void StartDataTaskAndWaitForCompletion(NSURLSessionDataTask* task) { |
| 146 [task resume]; | 147 [task resume]; |
| 147 int64_t deadline_ns = 1 * ns_in_second; | 148 int64_t deadline_ns = 1 * ns_in_second; |
| 148 dispatch_semaphore_wait([delegate_ semaphore], | 149 dispatch_semaphore_wait([delegate_ semaphore], |
| 149 dispatch_time(DISPATCH_TIME_NOW, deadline_ns)); | 150 dispatch_time(DISPATCH_TIME_NOW, deadline_ns)); |
| 150 } | 151 } |
| 151 | 152 |
| 152 base::scoped_nsobject<NSURLSession> session_; | 153 base::scoped_nsobject<NSURLSession> session_; |
| 153 base::scoped_nsobject<TestDelegate> delegate_; | 154 base::scoped_nsobject<TestDelegate> delegate_; |
| 154 }; | 155 }; |
| 155 | 156 |
| 156 TEST_F(HttpTest, NSURLSessionReceivesData) { | 157 TEST_F(HttpTest, NSURLSessionReceivesData) { |
| 157 NSURL* url = net::NSURLWithGURL(GURL(grpc_support::kTestServerUrl)); | 158 NSURL* url = net::NSURLWithGURL(GURL(kTestServerUrl)); |
| 158 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; | 159 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; |
| 159 StartDataTaskAndWaitForCompletion(task); | 160 StartDataTaskAndWaitForCompletion(task); |
| 160 EXPECT_EQ(nil, [delegate_ error]); | 161 EXPECT_EQ(nil, [delegate_ error]); |
| 161 EXPECT_STREQ(grpc_support::kHelloBodyValue, | 162 EXPECT_STREQ(kHelloBodyValue, [[delegate_ responseBody] UTF8String]); |
| 162 base::SysNSStringToUTF8([delegate_ responseBody]).c_str()); | |
| 163 } | 163 } |
| 164 | 164 |
| 165 TEST_F(HttpTest, GetGlobalMetricsDeltas) { | 165 TEST_F(HttpTest, GetGlobalMetricsDeltas) { |
| 166 NSData* delta1 = [Cronet getGlobalMetricsDeltas]; | 166 NSData* delta1 = [Cronet getGlobalMetricsDeltas]; |
| 167 | 167 |
| 168 NSURL* url = net::NSURLWithGURL(GURL(grpc_support::kTestServerUrl)); | 168 NSURL* url = net::NSURLWithGURL(GURL(kTestServerUrl)); |
| 169 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; | 169 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; |
| 170 StartDataTaskAndWaitForCompletion(task); | 170 StartDataTaskAndWaitForCompletion(task); |
| 171 EXPECT_EQ(nil, [delegate_ error]); | 171 EXPECT_EQ(nil, [delegate_ error]); |
| 172 EXPECT_STREQ(grpc_support::kHelloBodyValue, | 172 EXPECT_STREQ(kHelloBodyValue, [[delegate_ responseBody] UTF8String]); |
| 173 base::SysNSStringToUTF8([delegate_ responseBody]).c_str()); | |
| 174 | 173 |
| 175 NSData* delta2 = [Cronet getGlobalMetricsDeltas]; | 174 NSData* delta2 = [Cronet getGlobalMetricsDeltas]; |
| 176 EXPECT_FALSE([delta2 isEqualToData:delta1]); | 175 EXPECT_FALSE([delta2 isEqualToData:delta1]); |
| 177 } | 176 } |
| 178 | 177 |
| 179 TEST_F(HttpTest, SdchDisabledByDefault) { | 178 TEST_F(HttpTest, SdchDisabledByDefault) { |
| 180 NSURL* url = | 179 NSURL* url = |
| 181 net::NSURLWithGURL(GURL(TestServer::GetEchoHeaderURL("Accept-Encoding"))); | 180 net::NSURLWithGURL(GURL(TestServer::GetEchoHeaderURL("Accept-Encoding"))); |
| 182 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; | 181 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; |
| 183 StartDataTaskAndWaitForCompletion(task); | 182 StartDataTaskAndWaitForCompletion(task); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 197 TEST_F(HttpTest, SetUserAgentIsExact) { | 196 TEST_F(HttpTest, SetUserAgentIsExact) { |
| 198 NSURL* url = | 197 NSURL* url = |
| 199 net::NSURLWithGURL(GURL(TestServer::GetEchoHeaderURL("User-Agent"))); | 198 net::NSURLWithGURL(GURL(TestServer::GetEchoHeaderURL("User-Agent"))); |
| 200 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; | 199 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; |
| 201 StartDataTaskAndWaitForCompletion(task); | 200 StartDataTaskAndWaitForCompletion(task); |
| 202 EXPECT_EQ(nil, [delegate_ error]); | 201 EXPECT_EQ(nil, [delegate_ error]); |
| 203 EXPECT_STREQ(kUserAgent, [[delegate_ responseBody] UTF8String]); | 202 EXPECT_STREQ(kUserAgent, [[delegate_ responseBody] UTF8String]); |
| 204 } | 203 } |
| 205 | 204 |
| 206 } // namespace cronet | 205 } // namespace cronet |
| OLD | NEW |