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