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

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

Issue 2510463002: [Cronet] Retain block passed to setRequestFilterBlock on iOS. (Closed)
Patch Set: Improve tests. 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"
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 115
116 class HttpTest : public ::testing::Test { 116 class HttpTest : public ::testing::Test {
117 protected: 117 protected:
118 HttpTest() {} 118 HttpTest() {}
119 ~HttpTest() override {} 119 ~HttpTest() override {}
120 120
121 void SetUp() override { 121 void SetUp() override {
122 grpc_support::StartQuicTestServer(); 122 grpc_support::StartQuicTestServer();
123 TestServer::Start(); 123 TestServer::Start();
124 124
125 [Cronet setRequestFilterBlock:^(NSURLRequest* request) {
126 return YES;
127 }];
125 StartCronetIfNecessary(grpc_support::GetQuicTestServerPort()); 128 StartCronetIfNecessary(grpc_support::GetQuicTestServerPort());
129 [Cronet registerHttpProtocolHandler];
126 NSURLSessionConfiguration* config = 130 NSURLSessionConfiguration* config =
127 [NSURLSessionConfiguration ephemeralSessionConfiguration]; 131 [NSURLSessionConfiguration ephemeralSessionConfiguration];
128 [Cronet installIntoSessionConfiguration:config]; 132 [Cronet installIntoSessionConfiguration:config];
129 delegate_.reset([[TestDelegate alloc] init]); 133 delegate_.reset([[TestDelegate alloc] init]);
130 NSURLSession* session = [NSURLSession sessionWithConfiguration:config 134 NSURLSession* session = [NSURLSession sessionWithConfiguration:config
131 delegate:delegate_ 135 delegate:delegate_
132 delegateQueue:nil]; 136 delegateQueue:nil];
133 // Take a reference to the session and store it so it doesn't get 137 // Take a reference to the session and store it so it doesn't get
134 // deallocated until this object does. 138 // deallocated until this object does.
135 session_.reset([session retain]); 139 session_.reset([session retain]);
(...skipping 12 matching lines...) Expand all
148 dispatch_semaphore_wait([delegate_ semaphore], 152 dispatch_semaphore_wait([delegate_ semaphore],
149 dispatch_time(DISPATCH_TIME_NOW, deadline_ns)); 153 dispatch_time(DISPATCH_TIME_NOW, deadline_ns));
150 } 154 }
151 155
152 base::scoped_nsobject<NSURLSession> session_; 156 base::scoped_nsobject<NSURLSession> session_;
153 base::scoped_nsobject<TestDelegate> delegate_; 157 base::scoped_nsobject<TestDelegate> delegate_;
154 }; 158 };
155 159
156 TEST_F(HttpTest, NSURLSessionReceivesData) { 160 TEST_F(HttpTest, NSURLSessionReceivesData) {
157 NSURL* url = net::NSURLWithGURL(GURL(grpc_support::kTestServerUrl)); 161 NSURL* url = net::NSURLWithGURL(GURL(grpc_support::kTestServerUrl));
162 __block BOOL block_used = NO;
158 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; 163 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url];
164 [Cronet setRequestFilterBlock:^(NSURLRequest* request) {
165 block_used = YES;
166 EXPECT_EQ([request URL], url);
167 return YES;
168 }];
159 StartDataTaskAndWaitForCompletion(task); 169 StartDataTaskAndWaitForCompletion(task);
170 EXPECT_TRUE(block_used);
160 EXPECT_EQ(nil, [delegate_ error]); 171 EXPECT_EQ(nil, [delegate_ error]);
161 EXPECT_STREQ(grpc_support::kHelloBodyValue, 172 EXPECT_STREQ(grpc_support::kHelloBodyValue,
162 base::SysNSStringToUTF8([delegate_ responseBody]).c_str()); 173 base::SysNSStringToUTF8([delegate_ responseBody]).c_str());
163 } 174 }
164 175
165 TEST_F(HttpTest, GetGlobalMetricsDeltas) { 176 TEST_F(HttpTest, GetGlobalMetricsDeltas) {
166 NSData* delta1 = [Cronet getGlobalMetricsDeltas]; 177 NSData* delta1 = [Cronet getGlobalMetricsDeltas];
167 178
168 NSURL* url = net::NSURLWithGURL(GURL(grpc_support::kTestServerUrl)); 179 NSURL* url = net::NSURLWithGURL(GURL(grpc_support::kTestServerUrl));
169 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; 180 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url];
(...skipping 20 matching lines...) Expand all
190 net::NSURLWithGURL(GURL(TestServer::GetEchoHeaderURL("Accept-Language"))); 201 net::NSURLWithGURL(GURL(TestServer::GetEchoHeaderURL("Accept-Language")));
191 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; 202 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url];
192 StartDataTaskAndWaitForCompletion(task); 203 StartDataTaskAndWaitForCompletion(task);
193 EXPECT_EQ(nil, [delegate_ error]); 204 EXPECT_EQ(nil, [delegate_ error]);
194 ASSERT_STREQ("en-US,en", [[delegate_ responseBody] UTF8String]); 205 ASSERT_STREQ("en-US,en", [[delegate_ responseBody] UTF8String]);
195 } 206 }
196 207
197 TEST_F(HttpTest, SetUserAgentIsExact) { 208 TEST_F(HttpTest, SetUserAgentIsExact) {
198 NSURL* url = 209 NSURL* url =
199 net::NSURLWithGURL(GURL(TestServer::GetEchoHeaderURL("User-Agent"))); 210 net::NSURLWithGURL(GURL(TestServer::GetEchoHeaderURL("User-Agent")));
211 [Cronet setRequestFilterBlock:nil];
200 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; 212 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url];
201 StartDataTaskAndWaitForCompletion(task); 213 StartDataTaskAndWaitForCompletion(task);
202 EXPECT_EQ(nil, [delegate_ error]); 214 EXPECT_EQ(nil, [delegate_ error]);
203 EXPECT_STREQ(kUserAgent, [[delegate_ responseBody] UTF8String]); 215 EXPECT_STREQ(kUserAgent, [[delegate_ responseBody] UTF8String]);
204 } 216 }
205 217
218 TEST_F(HttpTest, FilterOutRequest) {
219 NSURL* url =
220 net::NSURLWithGURL(GURL(TestServer::GetEchoHeaderURL("User-Agent")));
221 __block BOOL block_used = NO;
222 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url];
223 [Cronet setRequestFilterBlock:^(NSURLRequest* request) {
224 block_used = YES;
225 EXPECT_EQ([request URL], url);
226 return NO;
227 }];
228 StartDataTaskAndWaitForCompletion(task);
229 EXPECT_TRUE(block_used);
230 EXPECT_EQ(nil, [delegate_ error]);
231 EXPECT_STRNE(kUserAgent,
kapishnikov 2016/11/16 17:58:53 nit: We can make this assert stronger by using 'co
mef 2016/11/16 18:11:11 Done.
232 base::SysNSStringToUTF8([delegate_ responseBody]).c_str());
233 EXPECT_TRUE([[delegate_ responseBody] containsString:@"CFNetwork"]);
234 }
235
206 } // namespace cronet 236 } // namespace cronet
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698