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

Unified Diff: components/cronet/ios/test/cronet_http_test.mm

Issue 2146643002: [Cronet] Integrate CrNet functionality into Cronet on iOS. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sync Created 4 years, 2 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 side-by-side diff with in-line comments
Download patch
Index: components/cronet/ios/test/cronet_http_test.mm
diff --git a/ios/crnet/test/crnet_http_tests.mm b/components/cronet/ios/test/cronet_http_test.mm
similarity index 53%
copy from ios/crnet/test/crnet_http_tests.mm
copy to components/cronet/ios/test/cronet_http_test.mm
index 7ab9b39dcda06b764eeeef5104e9d8c6a1d498bd..117185ce79ff8613cbb361ae620645ecf9751bee 100644
--- a/ios/crnet/test/crnet_http_tests.mm
+++ b/components/cronet/ios/test/cronet_http_test.mm
@@ -5,16 +5,18 @@
#import <Foundation/Foundation.h>
#include <stdint.h>
-#import <CrNet/CrNet.h>
+#import "components/cronet/ios/Cronet.h"
#include "base/logging.h"
#include "base/mac/scoped_nsobject.h"
#include "base/strings/sys_string_conversions.h"
-#import "ios/third_party/gcdwebserver/src/GCDWebServer/Core/GCDWebServer.h"
-#import "ios/third_party/gcdwebserver/src/GCDWebServer/Responses/GCDWebServerDataResponse.h"
+#include "components/cronet/ios/test/quic_test_server.h"
#include "net/base/mac/url_conversions.h"
+#include "net/base/net_errors.h"
+#include "net/cert/mock_cert_verifier.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/gtest_mac.h"
+
#include "url/gurl.h"
@interface TestDelegate : NSObject<NSURLSessionDataDelegate,
@@ -97,10 +99,14 @@
@end
+namespace cronet {
// base::TimeDelta would normally be ideal for this but it does not support
// nanosecond resolution.
static const int64_t ns_in_second = 1000000000LL;
-const char kUserAgent[] = "CrNetTest/1.0.0.0";
+const char kUserAgent[] = "CronetTest/1.0.0.0";
+
+// TODO(mef): Create common header file to declare this.
+void StartCronetIfNecessary();
class HttpTest : public ::testing::Test {
protected:
@@ -108,11 +114,10 @@ class HttpTest : public ::testing::Test {
~HttpTest() override {}
void SetUp() override {
- [CrNet setUserAgent:base::SysUTF8ToNSString(kUserAgent) partial:NO];
- [CrNet install];
+ StartCronetIfNecessary();
NSURLSessionConfiguration* config =
[NSURLSessionConfiguration ephemeralSessionConfiguration];
- [CrNet installIntoSessionConfiguration:config];
+ [Cronet installIntoSessionConfiguration:config];
delegate_.reset([[TestDelegate alloc] init]);
NSURLSession* session = [NSURLSession sessionWithConfiguration:config
delegate:delegate_
@@ -120,41 +125,10 @@ class HttpTest : public ::testing::Test {
// Take a reference to the session and store it so it doesn't get
// deallocated until this object does.
session_.reset([session retain]);
- web_server_.reset([[GCDWebServer alloc] init]);
+ StartQuicTestServer();
}
- void TearDown() override {
- [CrNet uninstall];
- [web_server_ stop];
- }
-
- // Starts a GCDWebServer instance on localhost port 8080, and remembers the
- // root URL for later; tests can use GetURL() to produce a URL referring to a
- // specific resource under the root URL.
- void StartWebServer() {
- [web_server_ startWithPort:8080 bonjourName:nil];
- server_root_ = net::GURLWithNSURL([web_server_ serverURL]);
- }
-
- // Registers a fixed response |text| to be returned to requests for |path|,
- // which is relative to |server_root_|.
- void RegisterPathText(const std::string& path, const std::string& text) {
- NSString* nspath = base::SysUTF8ToNSString(path);
- NSData* data = [NSData dataWithBytes:text.c_str() length:text.length()];
- [web_server_ addGETHandlerForPath:nspath
- staticData:data
- contentType:@"text/plain"
- cacheAge:30];
- }
-
- void RegisterPathHandler(const std::string& path,
- GCDWebServerProcessBlock handler) {
- NSString* nspath = base::SysUTF8ToNSString(path);
- [web_server_ addHandlerForMethod:@"GET"
- path:nspath
- requestClass:NSClassFromString(@"GCDWebServerRequest")
- processBlock:handler];
- }
+ void TearDown() override { ShutdownQuicTestServer(); }
// Launches the supplied |task| and blocks until it completes, with a timeout
// of 1 second.
@@ -165,82 +139,37 @@ class HttpTest : public ::testing::Test {
dispatch_time(DISPATCH_TIME_NOW, deadline_ns));
}
- // Returns a URL to refer to the resource named |path| served by the test
- // server. If |path| starts with a /, the leading / will be stripped.
- GURL GetURL(const std::string& path) {
- std::string real_path = path[0] == '/' ? path.substr(1) : path;
- 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 = base::SysUTF8ToNSString(header);
- NSString* needle = base::SysUTF8ToNSString(target);
- NSString* haystack = request.headers[key];
- if (!haystack)
- return false;
- return [haystack rangeOfString:needle].location != NSNotFound;
- }
-
base::scoped_nsobject<NSURLSession> session_;
base::scoped_nsobject<TestDelegate> delegate_;
-
- private:
- base::scoped_nsobject<GCDWebServer> web_server_;
- GURL server_root_;
};
TEST_F(HttpTest, NSURLSessionReceivesData) {
- const char kPath[] = "/foo";
- const char kData[] = "foobar";
- RegisterPathText(kPath, kData);
- StartWebServer();
-
- NSURL* url = net::NSURLWithGURL(GetURL(kPath));
+ NSURL* url = net::NSURLWithGURL(GURL(kTestServerUrl));
NSURLSessionDataTask* task = [session_ dataTaskWithURL:url];
StartDataTaskAndWaitForCompletion(task);
EXPECT_EQ(nil, [delegate_ error]);
- EXPECT_EQ(strlen(kData), [delegate_ receivedBytes]);
+ EXPECT_EQ(strlen(kHelloBodyValue), [delegate_ receivedBytes]);
}
-TEST_F(HttpTest, SdchDisabledByDefault) {
- const char kPath[] = "/sdchtest";
- RegisterPathHandler(kPath,
- ^GCDWebServerResponse* (GCDWebServerRequest* req) {
- EXPECT_FALSE(HeaderValueContains(req, "Accept-Encoding", "sdch"));
- return [GCDWebServerDataResponse responseWithText:@"woot!"];
- });
- StartWebServer();
- NSURL* url = net::NSURLWithGURL(GetURL(kPath));
+TEST_F(HttpTest, GetGlobalMetricsDeltas) {
+ NSData* delta1 = [Cronet getGlobalMetricsDeltas];
+
+ NSURL* url = net::NSURLWithGURL(GURL(kTestServerUrl));
NSURLSessionDataTask* task = [session_ dataTaskWithURL:url];
StartDataTaskAndWaitForCompletion(task);
EXPECT_EQ(nil, [delegate_ error]);
- EXPECT_TRUE([delegate_ receivedBytes]);
+ EXPECT_EQ(strlen(kHelloBodyValue), [delegate_ receivedBytes]);
+
+ NSData* delta2 = [Cronet getGlobalMetricsDeltas];
+ EXPECT_FALSE([delta2 isEqualToData:delta1]);
}
-TEST_F(HttpTest, SetUserAgentIsExact) {
- const char kPath[] = "/uatest";
- RegisterPathHandler(kPath, ^GCDWebServerResponse*(GCDWebServerRequest* req) {
- EXPECT_STREQ(kUserAgent,
- [[req.headers valueForKey:@"User-Agent"] UTF8String]);
- return [GCDWebServerDataResponse responseWithText:@"yay!"];
- });
- StartWebServer();
- NSURL* url = net::NSURLWithGURL(GetURL(kPath));
+TEST_F(HttpTest, NSURLSessionReceivesData2) {
+ NSURL* url = net::NSURLWithGURL(GURL(GetTestServerURL("/")));
NSURLSessionDataTask* task = [session_ dataTaskWithURL:url];
StartDataTaskAndWaitForCompletion(task);
EXPECT_EQ(nil, [delegate_ error]);
- EXPECT_TRUE([delegate_ receivedBytes]);
+ EXPECT_EQ(strlen(kUserAgent), [delegate_ receivedBytes]);
}
-// 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.
+} // namespace cronet
« no previous file with comments | « components/cronet/ios/test/cronet_bidirectional_stream_test.mm ('k') | components/cronet/ios/test/quic_test_server.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698