Chromium Code Reviews| Index: ios/crnet/test/crnet_http_tests.mm |
| diff --git a/ios/crnet/test/crnet_http_tests.mm b/ios/crnet/test/crnet_http_tests.mm |
| index 4436e09d8ed0ae65844ecca5fff31b9770f5fd08..2d6a7163ab98af8329648f415dcc5da37f6499eb 100644 |
| --- a/ios/crnet/test/crnet_http_tests.mm |
| +++ b/ios/crnet/test/crnet_http_tests.mm |
| @@ -143,6 +143,16 @@ class HttpTest : public ::testing::Test { |
| cacheAge:30]; |
| } |
| + void RegisterPathHandler(const std::string& path, |
| + GCDWebServerProcessBlock handler) { |
| + NSString* nspath = |
| + [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.
|
| + [web_server_ addHandlerForMethod:@"GET" |
| + path:nspath |
| + requestClass:NSClassFromString(@"GCDWebServerRequest") |
| + processBlock:handler]; |
| + } |
| + |
| // Launches the supplied |task| and blocks until it completes, with a timeout |
| // of 1 second. |
| void StartDataTaskAndWaitForCompletion(NSURLSessionDataTask* task) { |
| @@ -159,6 +169,26 @@ class HttpTest : public ::testing::Test { |
| return server_root_.Resolve(real_path); |
| } |
| + // Some convenience functions for working with GCDWebServerRequest and |
| + // GCDWebServerResponse. |
| + |
| + // Returns true if the value for the request header |header| is not nil and |
| + // contains the string |target|. |
| + bool HeaderValueContains(GCDWebServerRequest* request, |
| + const std::string& header, |
| + const std::string& target) { |
| + NSString* key = |
| + [NSString stringWithCString:header.c_str() |
| + encoding:NSUTF8StringEncoding]; |
| + NSString* needle = |
| + [NSString stringWithCString:target.c_str() |
| + encoding:NSUTF8StringEncoding]; |
| + 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.
|
| + if (haystack == nil) |
|
droger
2015/08/17 15:47:30
if (!haystack)
Elly Fong-Jones
2015/08/17 17:26:50
Done.
|
| + return false; |
| + return [haystack rangeOfString:needle].location != NSNotFound; |
| + } |
| + |
| base::scoped_nsobject<NSURLSession> session_; |
| base::scoped_nsobject<TestDelegate> delegate_; |
| @@ -184,13 +214,38 @@ TEST_F(HttpTest, NSURLConnectionReceivesData) { |
| } |
| TEST_F(HttpTest, NSURLSessionReceivesData) { |
| - const char data[] = "foobar"; |
| - RegisterPathText("/foo", data); |
| + const char kPath[] = "/foo"; |
| + const char kData[] = "foobar"; |
| + RegisterPathText(kPath, kData); |
| StartWebServer(); |
| - NSURL* url = net::NSURLWithGURL(GetURL("foo")); |
| + NSURL* url = net::NSURLWithGURL(GetURL(kPath)); |
| NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; |
| StartDataTaskAndWaitForCompletion(task); |
| EXPECT_EQ(nil, [delegate_ error]); |
| - EXPECT_EQ(strlen(data), [delegate_ receivedBytes]); |
| + EXPECT_EQ(strlen(kData), [delegate_ receivedBytes]); |
| +} |
| + |
| +TEST_F(HttpTest, SdchDisabledByDefault) { |
| + const char kPath[] = "/foo"; |
| + RegisterPathHandler(kPath, |
| + ^GCDWebServerResponse* (GCDWebServerRequest* req) { |
| + EXPECT_FALSE(HeaderValueContains(req, "Accept-Encoding", "sdch")); |
| + return nil; |
| + }); |
| + StartWebServer(); |
| + NSURL* url = net::NSURLWithGURL(GetURL(kPath)); |
| + NSURLRequest* req = [NSURLRequest requestWithURL:url]; |
| + NSURLResponse* resp = nil; |
| + NSError* error = nil; |
| + NSData* received = [NSURLConnection sendSynchronousRequest:req |
| + returningResponse:&resp |
| + 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.
|
| + 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.
|
| } |
| + |
| +// TODO(ellyjones): There needs to be a test that enabling SDCH works, but |
| +// because CrNet is static and 'uninstall' only disables it, there is no way to |
| +// have an individual test enable or disable SDCH. |
| +// Probably there is a way to get gtest tests to run in a separate process, but |
| +// I'm not sure what it is. |