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

Side by Side Diff: ios/crnet/test/crnet_http_tests.mm

Issue 1291673003: SdchManager: remove EnableSdchSupport (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 4 months 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 <Foundation/Foundation.h> 5 #import <Foundation/Foundation.h>
6 6
7 #import "CrNet.h" 7 #import "CrNet.h"
8 8
9 #include "base/mac/scoped_nsobject.h" 9 #include "base/mac/scoped_nsobject.h"
10 #import "ios/third_party/gcdwebserver/src/GCDWebServer/Core/GCDWebServer.h" 10 #import "ios/third_party/gcdwebserver/src/GCDWebServer/Core/GCDWebServer.h"
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 // specific resource under the root URL. 128 // specific resource under the root URL.
129 void StartWebServer() { 129 void StartWebServer() {
130 [web_server_ startWithPort:8080 bonjourName:nil]; 130 [web_server_ startWithPort:8080 bonjourName:nil];
131 server_root_ = net::GURLWithNSURL([web_server_ serverURL]); 131 server_root_ = net::GURLWithNSURL([web_server_ serverURL]);
132 } 132 }
133 133
134 // Registers a fixed response |text| to be returned to requests for |path|, 134 // Registers a fixed response |text| to be returned to requests for |path|,
135 // which is relative to |server_root_|. 135 // which is relative to |server_root_|.
136 void RegisterPathText(const std::string& path, const std::string& text) { 136 void RegisterPathText(const std::string& path, const std::string& text) {
137 NSString* nspath = 137 NSString* nspath =
138 [NSString stringWithCString:path.c_str() encoding:NSUTF8StringEncoding]; 138 [NSString stringWithCString:path.c_str() encoding:NSUTF8StringEncoding];
droger 2015/08/17 15:47:30 Optional convert this to base::SysUTF8ToNSString.
Elly Fong-Jones 2015/08/17 17:26:49 Done.
139 NSData* data = [NSData dataWithBytes:text.c_str() length:text.length()]; 139 NSData* data = [NSData dataWithBytes:text.c_str() length:text.length()];
140 [web_server_ addGETHandlerForPath:nspath 140 [web_server_ addGETHandlerForPath:nspath
141 staticData:data 141 staticData:data
142 contentType:@"text/plain" 142 contentType:@"text/plain"
143 cacheAge:30]; 143 cacheAge:30];
144 } 144 }
145 145
146 void RegisterPathHandler(const std::string& path,
147 GCDWebServerProcessBlock handler) {
148 NSString* nspath =
149 [NSString stringWithCString:path.c_str() encoding:NSUTF8StringEncoding];
droger 2015/08/17 15:47:30 Use base::SysUTF8ToNSString, and same everywhere b
Elly Fong-Jones 2015/08/17 17:26:50 Done.
150 [web_server_ addHandlerForMethod:@"GET"
151 path:nspath
152 requestClass:NSClassFromString(@"GCDWebServerRequest")
153 processBlock:handler];
154 }
155
146 // Launches the supplied |task| and blocks until it completes, with a timeout 156 // Launches the supplied |task| and blocks until it completes, with a timeout
147 // of 1 second. 157 // of 1 second.
148 void StartDataTaskAndWaitForCompletion(NSURLSessionDataTask* task) { 158 void StartDataTaskAndWaitForCompletion(NSURLSessionDataTask* task) {
149 [task resume]; 159 [task resume];
150 int64_t deadline_ns = 1 * ns_in_second; 160 int64_t deadline_ns = 1 * ns_in_second;
151 dispatch_semaphore_wait([delegate_ semaphore], 161 dispatch_semaphore_wait([delegate_ semaphore],
152 dispatch_time(DISPATCH_TIME_NOW, deadline_ns)); 162 dispatch_time(DISPATCH_TIME_NOW, deadline_ns));
153 } 163 }
154 164
155 // Returns a URL to refer to the resource named |path| served by the test 165 // Returns a URL to refer to the resource named |path| served by the test
156 // server. If |path| starts with a /, the leading / will be stripped. 166 // server. If |path| starts with a /, the leading / will be stripped.
157 GURL GetURL(const std::string& path) { 167 GURL GetURL(const std::string& path) {
158 std::string real_path = path[0] == '/' ? path.substr(1) : path; 168 std::string real_path = path[0] == '/' ? path.substr(1) : path;
159 return server_root_.Resolve(real_path); 169 return server_root_.Resolve(real_path);
160 } 170 }
161 171
172 // Some convenience functions for working with GCDWebServerRequest and
173 // GCDWebServerResponse.
174
175 // Returns true if the value for the request header |header| is not nil and
176 // contains the string |target|.
177 bool HeaderValueContains(GCDWebServerRequest* request,
178 const std::string& header,
179 const std::string& target) {
180 NSString* key =
181 [NSString stringWithCString:header.c_str()
182 encoding:NSUTF8StringEncoding];
183 NSString* needle =
184 [NSString stringWithCString:target.c_str()
185 encoding:NSUTF8StringEncoding];
186 NSString* haystack = [request.headers objectForKey:key];
droger 2015/08/17 15:47:30 Optional: request.headers[key];
Elly Fong-Jones 2015/08/17 17:26:49 Done.
187 if (haystack == nil)
droger 2015/08/17 15:47:30 if (!haystack)
Elly Fong-Jones 2015/08/17 17:26:50 Done.
188 return false;
189 return [haystack rangeOfString:needle].location != NSNotFound;
190 }
191
162 base::scoped_nsobject<NSURLSession> session_; 192 base::scoped_nsobject<NSURLSession> session_;
163 base::scoped_nsobject<TestDelegate> delegate_; 193 base::scoped_nsobject<TestDelegate> delegate_;
164 194
165 private: 195 private:
166 base::scoped_nsobject<GCDWebServer> web_server_; 196 base::scoped_nsobject<GCDWebServer> web_server_;
167 GURL server_root_; 197 GURL server_root_;
168 }; 198 };
169 199
170 TEST_F(HttpTest, NSURLConnectionReceivesData) { 200 TEST_F(HttpTest, NSURLConnectionReceivesData) {
171 const char kData[] = "foobar"; 201 const char kData[] = "foobar";
172 const char kPath[] = "/foo"; 202 const char kPath[] = "/foo";
173 RegisterPathText(kPath, kData); 203 RegisterPathText(kPath, kData);
174 StartWebServer(); 204 StartWebServer();
175 205
176 NSURL* url = net::NSURLWithGURL(GetURL(kPath)); 206 NSURL* url = net::NSURLWithGURL(GetURL(kPath));
177 NSURLRequest* req = [NSURLRequest requestWithURL:url]; 207 NSURLRequest* req = [NSURLRequest requestWithURL:url];
178 NSURLResponse* resp = nil; 208 NSURLResponse* resp = nil;
179 NSError* error = nil; 209 NSError* error = nil;
180 NSData* received = [NSURLConnection sendSynchronousRequest:req 210 NSData* received = [NSURLConnection sendSynchronousRequest:req
181 returningResponse:&resp 211 returningResponse:&resp
182 error:&error]; 212 error:&error];
183 EXPECT_EQ(0, memcmp([received bytes], kData, sizeof(kData))); 213 EXPECT_EQ(0, memcmp([received bytes], kData, sizeof(kData)));
184 } 214 }
185 215
186 TEST_F(HttpTest, NSURLSessionReceivesData) { 216 TEST_F(HttpTest, NSURLSessionReceivesData) {
187 const char data[] = "foobar"; 217 const char kPath[] = "/foo";
188 RegisterPathText("/foo", data); 218 const char kData[] = "foobar";
219 RegisterPathText(kPath, kData);
189 StartWebServer(); 220 StartWebServer();
190 221
191 NSURL* url = net::NSURLWithGURL(GetURL("foo")); 222 NSURL* url = net::NSURLWithGURL(GetURL(kPath));
192 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; 223 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url];
193 StartDataTaskAndWaitForCompletion(task); 224 StartDataTaskAndWaitForCompletion(task);
194 EXPECT_EQ(nil, [delegate_ error]); 225 EXPECT_EQ(nil, [delegate_ error]);
195 EXPECT_EQ(strlen(data), [delegate_ receivedBytes]); 226 EXPECT_EQ(strlen(kData), [delegate_ receivedBytes]);
196 } 227 }
228
229 TEST_F(HttpTest, SdchDisabledByDefault) {
230 const char kPath[] = "/foo";
231 RegisterPathHandler(kPath,
232 ^GCDWebServerResponse* (GCDWebServerRequest* req) {
233 EXPECT_FALSE(HeaderValueContains(req, "Accept-Encoding", "sdch"));
234 return nil;
235 });
236 StartWebServer();
237 NSURL* url = net::NSURLWithGURL(GetURL(kPath));
238 NSURLRequest* req = [NSURLRequest requestWithURL:url];
239 NSURLResponse* resp = nil;
240 NSError* error = nil;
241 NSData* received = [NSURLConnection sendSynchronousRequest:req
242 returningResponse:&resp
243 error:&error];
droger 2015/08/17 15:47:30 Remove the |error| variable and pass nullptr inste
Elly Fong-Jones 2015/08/17 17:26:50 Done.
244 DCHECK_NE(static_cast<NSData*>(nil), received);
droger 2015/08/17 15:47:30 Not sure, but I think maybe just DCHECK(received);
Elly Fong-Jones 2015/08/17 17:26:50 Done.
245 }
246
247 // TODO(ellyjones): There needs to be a test that enabling SDCH works, but
248 // because CrNet is static and 'uninstall' only disables it, there is no way to
249 // have an individual test enable or disable SDCH.
250 // Probably there is a way to get gtest tests to run in a separate process, but
251 // I'm not sure what it is.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698