Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(201)

Side by Side Diff: components/cronet/ios/test/cronet_http_test.mm

Issue 2492703002: Third try at landing gRPC refactoring. Previous issue failed on the waterfall (Closed)
Patch Set: Change DCHECK Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 grpc_support::StartQuicTestServer();
123 TestServer::Start();
124
125 StartCronetIfNecessary(grpc_support::GetQuicTestServerPort());
125 NSURLSessionConfiguration* config = 126 NSURLSessionConfiguration* config =
126 [NSURLSessionConfiguration ephemeralSessionConfiguration]; 127 [NSURLSessionConfiguration ephemeralSessionConfiguration];
127 [Cronet installIntoSessionConfiguration:config]; 128 [Cronet installIntoSessionConfiguration:config];
128 delegate_.reset([[TestDelegate alloc] init]); 129 delegate_.reset([[TestDelegate alloc] init]);
129 NSURLSession* session = [NSURLSession sessionWithConfiguration:config 130 NSURLSession* session = [NSURLSession sessionWithConfiguration:config
130 delegate:delegate_ 131 delegate:delegate_
131 delegateQueue:nil]; 132 delegateQueue:nil];
132 // Take a reference to the session and store it so it doesn't get 133 // Take a reference to the session and store it so it doesn't get
133 // deallocated until this object does. 134 // deallocated until this object does.
134 session_.reset([session retain]); 135 session_.reset([session retain]);
135 StartQuicTestServer();
136 TestServer::Start();
137 } 136 }
138 137
139 void TearDown() override { 138 void TearDown() override {
140 ShutdownQuicTestServer(); 139 grpc_support::ShutdownQuicTestServer();
141 TestServer::Shutdown(); 140 TestServer::Shutdown();
142 } 141 }
143 142
144 // Launches the supplied |task| and blocks until it completes, with a timeout 143 // Launches the supplied |task| and blocks until it completes, with a timeout
145 // of 1 second. 144 // of 1 second.
146 void StartDataTaskAndWaitForCompletion(NSURLSessionDataTask* task) { 145 void StartDataTaskAndWaitForCompletion(NSURLSessionDataTask* task) {
147 [task resume]; 146 [task resume];
148 int64_t deadline_ns = 1 * ns_in_second; 147 int64_t deadline_ns = 1 * ns_in_second;
149 dispatch_semaphore_wait([delegate_ semaphore], 148 dispatch_semaphore_wait([delegate_ semaphore],
150 dispatch_time(DISPATCH_TIME_NOW, deadline_ns)); 149 dispatch_time(DISPATCH_TIME_NOW, deadline_ns));
151 } 150 }
152 151
153 base::scoped_nsobject<NSURLSession> session_; 152 base::scoped_nsobject<NSURLSession> session_;
154 base::scoped_nsobject<TestDelegate> delegate_; 153 base::scoped_nsobject<TestDelegate> delegate_;
155 }; 154 };
156 155
157 TEST_F(HttpTest, NSURLSessionReceivesData) { 156 TEST_F(HttpTest, NSURLSessionReceivesData) {
158 NSURL* url = net::NSURLWithGURL(GURL(kTestServerUrl)); 157 NSURL* url = net::NSURLWithGURL(GURL(grpc_support::kTestServerUrl));
159 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; 158 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url];
160 StartDataTaskAndWaitForCompletion(task); 159 StartDataTaskAndWaitForCompletion(task);
161 EXPECT_EQ(nil, [delegate_ error]); 160 EXPECT_EQ(nil, [delegate_ error]);
162 EXPECT_STREQ(kHelloBodyValue, [[delegate_ responseBody] UTF8String]); 161 EXPECT_STREQ(grpc_support::kHelloBodyValue,
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(kTestServerUrl)); 168 NSURL* url = net::NSURLWithGURL(GURL(grpc_support::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(kHelloBodyValue, [[delegate_ responseBody] UTF8String]); 172 EXPECT_STREQ(grpc_support::kHelloBodyValue,
173 base::SysNSStringToUTF8([delegate_ responseBody]).c_str());
173 174
174 NSData* delta2 = [Cronet getGlobalMetricsDeltas]; 175 NSData* delta2 = [Cronet getGlobalMetricsDeltas];
175 EXPECT_FALSE([delta2 isEqualToData:delta1]); 176 EXPECT_FALSE([delta2 isEqualToData:delta1]);
176 } 177 }
177 178
178 TEST_F(HttpTest, SdchDisabledByDefault) { 179 TEST_F(HttpTest, SdchDisabledByDefault) {
179 NSURL* url = 180 NSURL* url =
180 net::NSURLWithGURL(GURL(TestServer::GetEchoHeaderURL("Accept-Encoding"))); 181 net::NSURLWithGURL(GURL(TestServer::GetEchoHeaderURL("Accept-Encoding")));
181 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; 182 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url];
182 StartDataTaskAndWaitForCompletion(task); 183 StartDataTaskAndWaitForCompletion(task);
(...skipping 13 matching lines...) Expand all
196 TEST_F(HttpTest, SetUserAgentIsExact) { 197 TEST_F(HttpTest, SetUserAgentIsExact) {
197 NSURL* url = 198 NSURL* url =
198 net::NSURLWithGURL(GURL(TestServer::GetEchoHeaderURL("User-Agent"))); 199 net::NSURLWithGURL(GURL(TestServer::GetEchoHeaderURL("User-Agent")));
199 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; 200 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url];
200 StartDataTaskAndWaitForCompletion(task); 201 StartDataTaskAndWaitForCompletion(task);
201 EXPECT_EQ(nil, [delegate_ error]); 202 EXPECT_EQ(nil, [delegate_ error]);
202 EXPECT_STREQ(kUserAgent, [[delegate_ responseBody] UTF8String]); 203 EXPECT_STREQ(kUserAgent, [[delegate_ responseBody] UTF8String]);
203 } 204 }
204 205
205 } // namespace cronet 206 } // namespace cronet
OLDNEW
« no previous file with comments | « components/cronet/ios/test/cronet_bidirectional_stream_test.mm ('k') | components/cronet/ios/test/cronet_test_runner.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698