QUIC - cleanup changes to sync chromium tree with internal source.
[chromium-blink-merge.git] / ios / web / net / web_http_protocol_handler_delegate_unittest.mm
blob5045374a005ebce20b730d8cc0cccb9a31ead425
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ios/web/net/web_http_protocol_handler_delegate.h"
7 #import <Foundation/Foundation.h>
9 #include "base/mac/scoped_nsobject.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "ios/web/public/web_client.h"
14 #include "net/url_request/url_request_test_util.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace web {
19 namespace {
21 // Test application specific scheme.
22 const char kAppSpecificScheme[] = "appspecific";
24 // URLs expected to be supported.
25 const char* kSupportedURLs[] = {
26     "http://foo.com",
27     "https://foo.com",
28     "data:text/html;charset=utf-8,Hello",
31 // URLs expected to be unsupported.
32 const char* kUnsupportedURLs[] = {
33     "foo:blank",          // Unknown scheme.
34     "appspecific:blank",  // No main document URL.
37 // Test web client with an application specific scheme.
38 class AppSpecificURLTestWebClient : public WebClient {
39  public:
40   bool IsAppSpecificURL(const GURL& url) const override {
41     return url.SchemeIs(kAppSpecificScheme);
42   }
45 class WebHTTPProtocolHandlerDelegateTest : public testing::Test {
46  public:
47   WebHTTPProtocolHandlerDelegateTest()
48       : context_getter_(new net::TestURLRequestContextGetter(
49             base::ThreadTaskRunnerHandle::Get())),
50         delegate_(new WebHTTPProtocolHandlerDelegate(context_getter_.get())),
51         original_web_client_(GetWebClient()),
52         web_client_(new AppSpecificURLTestWebClient) {
53     SetWebClient(web_client_.get());
54   }
56   ~WebHTTPProtocolHandlerDelegateTest() override {
57     SetWebClient(original_web_client_);
58   }
60  protected:
61   base::MessageLoop message_loop_;
62   scoped_refptr<net::URLRequestContextGetter> context_getter_;
63   scoped_ptr<WebHTTPProtocolHandlerDelegate> delegate_;
64   WebClient* original_web_client_;  // Stash the web client, Weak.
65   scoped_ptr<AppSpecificURLTestWebClient> web_client_;
68 }  // namespace
70 TEST_F(WebHTTPProtocolHandlerDelegateTest, IsRequestSupported) {
71   base::scoped_nsobject<NSMutableURLRequest> request;
73   for (unsigned int i = 0; i < arraysize(kSupportedURLs); ++i) {
74     base::scoped_nsobject<NSString> url_string(
75         [[NSString alloc] initWithUTF8String:kSupportedURLs[i]]);
76     request.reset([[NSMutableURLRequest alloc]
77         initWithURL:[NSURL URLWithString:url_string]]);
78     EXPECT_TRUE(delegate_->IsRequestSupported(request))
79         << kSupportedURLs[i] << " should be supported.";
80   }
82   for (unsigned int i = 0; i < arraysize(kUnsupportedURLs); ++i) {
83     base::scoped_nsobject<NSString> url_string(
84         [[NSString alloc] initWithUTF8String:kUnsupportedURLs[i]]);
85     request.reset([[NSMutableURLRequest alloc]
86         initWithURL:[NSURL URLWithString:url_string]]);
87     EXPECT_FALSE(delegate_->IsRequestSupported(request))
88         << kUnsupportedURLs[i] << " should NOT be supported.";
89   }
91   // Application specific scheme with main document URL.
92   request.reset([[NSMutableURLRequest alloc]
93       initWithURL:[NSURL URLWithString:@"appspecific:blank"]]);
94   [request setMainDocumentURL:[NSURL URLWithString:@"http://foo"]];
95   EXPECT_FALSE(delegate_->IsRequestSupported(request));
96   [request setMainDocumentURL:[NSURL URLWithString:@"appspecific:main"]];
97   EXPECT_TRUE(delegate_->IsRequestSupported(request));
98   request.reset([[NSMutableURLRequest alloc]
99       initWithURL:[NSURL URLWithString:@"foo:blank"]]);
100   [request setMainDocumentURL:[NSURL URLWithString:@"appspecific:main"]];
101   EXPECT_FALSE(delegate_->IsRequestSupported(request));
104 TEST_F(WebHTTPProtocolHandlerDelegateTest, IsRequestSupportedMalformed) {
105   base::scoped_nsobject<NSURLRequest> request;
107   // Null URL.
108   request.reset([[NSMutableURLRequest alloc] init]);
109   ASSERT_FALSE([request URL]);
110   EXPECT_FALSE(delegate_->IsRequestSupported(request));
112   // URL with no scheme.
113   request.reset(
114       [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"foo"]]);
115   ASSERT_TRUE([request URL]);
116   ASSERT_FALSE([[request URL] scheme]);
117   EXPECT_FALSE(delegate_->IsRequestSupported(request));
119   // Empty scheme.
120   request.reset(
121       [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@":foo"]]);
122   ASSERT_TRUE([request URL]);
123   ASSERT_TRUE([[request URL] scheme]);
124   ASSERT_FALSE([[[request URL] scheme] length]);
125   EXPECT_FALSE(delegate_->IsRequestSupported(request));
128 }  // namespace web