OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "ios/web/net/web_http_protocol_handler_delegate.h" | 5 #import "ios/web/net/web_http_protocol_handler_delegate.h" |
6 | 6 |
7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
11 #import "base/mac/scoped_nsobject.h" | |
12 #include "base/macros.h" | 11 #include "base/macros.h" |
13 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
14 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
15 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
16 #include "ios/web/public/test/scoped_testing_web_client.h" | 15 #include "ios/web/public/test/scoped_testing_web_client.h" |
17 #import "ios/web/public/web_client.h" | 16 #import "ios/web/public/web_client.h" |
18 #include "net/url_request/url_request_test_util.h" | 17 #include "net/url_request/url_request_test_util.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
20 #import "third_party/ocmock/OCMock/OCMock.h" | 19 #import "third_party/ocmock/OCMock/OCMock.h" |
21 | 20 |
| 21 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 22 #error "This file requires ARC support." |
| 23 #endif |
| 24 |
22 namespace web { | 25 namespace web { |
23 | 26 |
24 namespace { | 27 namespace { |
25 | 28 |
26 // Test application specific scheme. | 29 // Test application specific scheme. |
27 const char kAppSpecificScheme[] = "appspecific"; | 30 const char kAppSpecificScheme[] = "appspecific"; |
28 | 31 |
29 // URLs expected to be supported. | 32 // URLs expected to be supported. |
30 const char* kSupportedURLs[] = { | 33 const char* kSupportedURLs[] = { |
31 "http://foo.com", | 34 "http://foo.com", |
(...skipping 26 matching lines...) Expand all Loading... |
58 protected: | 61 protected: |
59 base::MessageLoop message_loop_; | 62 base::MessageLoop message_loop_; |
60 scoped_refptr<net::URLRequestContextGetter> context_getter_; | 63 scoped_refptr<net::URLRequestContextGetter> context_getter_; |
61 std::unique_ptr<WebHTTPProtocolHandlerDelegate> delegate_; | 64 std::unique_ptr<WebHTTPProtocolHandlerDelegate> delegate_; |
62 web::ScopedTestingWebClient web_client_; | 65 web::ScopedTestingWebClient web_client_; |
63 }; | 66 }; |
64 | 67 |
65 } // namespace | 68 } // namespace |
66 | 69 |
67 TEST_F(WebHTTPProtocolHandlerDelegateTest, IsRequestSupported) { | 70 TEST_F(WebHTTPProtocolHandlerDelegateTest, IsRequestSupported) { |
68 base::scoped_nsobject<NSMutableURLRequest> request; | 71 NSMutableURLRequest* request; |
69 | 72 |
70 for (unsigned int i = 0; i < arraysize(kSupportedURLs); ++i) { | 73 for (unsigned int i = 0; i < arraysize(kSupportedURLs); ++i) { |
71 base::scoped_nsobject<NSString> url_string( | 74 NSString* url_string = |
72 [[NSString alloc] initWithUTF8String:kSupportedURLs[i]]); | 75 [[NSString alloc] initWithUTF8String:kSupportedURLs[i]]; |
73 request.reset([[NSMutableURLRequest alloc] | 76 request = [[NSMutableURLRequest alloc] |
74 initWithURL:[NSURL URLWithString:url_string]]); | 77 initWithURL:[NSURL URLWithString:url_string]]; |
75 EXPECT_TRUE(delegate_->IsRequestSupported(request)) | 78 EXPECT_TRUE(delegate_->IsRequestSupported(request)) |
76 << kSupportedURLs[i] << " should be supported."; | 79 << kSupportedURLs[i] << " should be supported."; |
77 } | 80 } |
78 | 81 |
79 for (unsigned int i = 0; i < arraysize(kUnsupportedURLs); ++i) { | 82 for (unsigned int i = 0; i < arraysize(kUnsupportedURLs); ++i) { |
80 base::scoped_nsobject<NSString> url_string( | 83 NSString* url_string = |
81 [[NSString alloc] initWithUTF8String:kUnsupportedURLs[i]]); | 84 [[NSString alloc] initWithUTF8String:kUnsupportedURLs[i]]; |
82 request.reset([[NSMutableURLRequest alloc] | 85 request = [[NSMutableURLRequest alloc] |
83 initWithURL:[NSURL URLWithString:url_string]]); | 86 initWithURL:[NSURL URLWithString:url_string]]; |
84 EXPECT_FALSE(delegate_->IsRequestSupported(request)) | 87 EXPECT_FALSE(delegate_->IsRequestSupported(request)) |
85 << kUnsupportedURLs[i] << " should NOT be supported."; | 88 << kUnsupportedURLs[i] << " should NOT be supported."; |
86 } | 89 } |
87 | 90 |
88 // Application specific scheme with main document URL. | 91 // Application specific scheme with main document URL. |
89 request.reset([[NSMutableURLRequest alloc] | 92 request = [[NSMutableURLRequest alloc] |
90 initWithURL:[NSURL URLWithString:@"appspecific:blank"]]); | 93 initWithURL:[NSURL URLWithString:@"appspecific:blank"]]; |
91 [request setMainDocumentURL:[NSURL URLWithString:@"http://foo"]]; | 94 [request setMainDocumentURL:[NSURL URLWithString:@"http://foo"]]; |
92 EXPECT_FALSE(delegate_->IsRequestSupported(request)); | 95 EXPECT_FALSE(delegate_->IsRequestSupported(request)); |
93 [request setMainDocumentURL:[NSURL URLWithString:@"appspecific:main"]]; | 96 [request setMainDocumentURL:[NSURL URLWithString:@"appspecific:main"]]; |
94 EXPECT_TRUE(delegate_->IsRequestSupported(request)); | 97 EXPECT_TRUE(delegate_->IsRequestSupported(request)); |
95 request.reset([[NSMutableURLRequest alloc] | 98 request = [[NSMutableURLRequest alloc] |
96 initWithURL:[NSURL URLWithString:@"foo:blank"]]); | 99 initWithURL:[NSURL URLWithString:@"foo:blank"]]; |
97 [request setMainDocumentURL:[NSURL URLWithString:@"appspecific:main"]]; | 100 [request setMainDocumentURL:[NSURL URLWithString:@"appspecific:main"]]; |
98 EXPECT_FALSE(delegate_->IsRequestSupported(request)); | 101 EXPECT_FALSE(delegate_->IsRequestSupported(request)); |
99 } | 102 } |
100 | 103 |
101 TEST_F(WebHTTPProtocolHandlerDelegateTest, IsRequestSupportedMalformed) { | 104 TEST_F(WebHTTPProtocolHandlerDelegateTest, IsRequestSupportedMalformed) { |
102 base::scoped_nsobject<NSURLRequest> request; | 105 NSURLRequest* request; |
103 | 106 |
104 // Null URL. | 107 // Null URL. |
105 request.reset([[NSMutableURLRequest alloc] init]); | 108 request = [[NSMutableURLRequest alloc] init]; |
106 ASSERT_FALSE([request URL]); | 109 ASSERT_FALSE([request URL]); |
107 EXPECT_FALSE(delegate_->IsRequestSupported(request)); | 110 EXPECT_FALSE(delegate_->IsRequestSupported(request)); |
108 | 111 |
109 // URL with no scheme. | 112 // URL with no scheme. |
110 request.reset( | 113 request = |
111 [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"foo"]]); | 114 [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"foo"]]; |
112 ASSERT_TRUE([request URL]); | 115 ASSERT_TRUE([request URL]); |
113 ASSERT_FALSE([[request URL] scheme]); | 116 ASSERT_FALSE([[request URL] scheme]); |
114 EXPECT_FALSE(delegate_->IsRequestSupported(request)); | 117 EXPECT_FALSE(delegate_->IsRequestSupported(request)); |
115 | 118 |
116 // Empty scheme. | 119 // Empty scheme. |
117 request.reset( | 120 request = |
118 [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@":foo"]]); | 121 [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@":foo"]]; |
119 ASSERT_TRUE([request URL]); | 122 ASSERT_TRUE([request URL]); |
120 ASSERT_TRUE([[request URL] scheme]); | 123 ASSERT_TRUE([[request URL] scheme]); |
121 ASSERT_FALSE([[[request URL] scheme] length]); | 124 ASSERT_FALSE([[[request URL] scheme] length]); |
122 EXPECT_FALSE(delegate_->IsRequestSupported(request)); | 125 EXPECT_FALSE(delegate_->IsRequestSupported(request)); |
123 } | 126 } |
124 | 127 |
125 // Tests that requests for images are considered as static file requests, | 128 // Tests that requests for images are considered as static file requests, |
126 // regardless of the user agent. | 129 // regardless of the user agent. |
127 TEST_F(WebHTTPProtocolHandlerDelegateTest, TestIsStaticImageRequestTrue) { | 130 TEST_F(WebHTTPProtocolHandlerDelegateTest, TestIsStaticImageRequestTrue) { |
128 // Empty dictionary so User-Agent check fails. | 131 // Empty dictionary so User-Agent check fails. |
(...skipping 23 matching lines...) Expand all Loading... |
152 // Empty dictionary so User-Agent check fails. | 155 // Empty dictionary so User-Agent check fails. |
153 NSDictionary* headers = @{}; | 156 NSDictionary* headers = @{}; |
154 NSURL* url = [NSURL URLWithString:@"file:///steal/this/file.html"]; | 157 NSURL* url = [NSURL URLWithString:@"file:///steal/this/file.html"]; |
155 id mock_request = [OCMockObject mockForClass:[NSURLRequest class]]; | 158 id mock_request = [OCMockObject mockForClass:[NSURLRequest class]]; |
156 [[[mock_request stub] andReturn:headers] allHTTPHeaderFields]; | 159 [[[mock_request stub] andReturn:headers] allHTTPHeaderFields]; |
157 [[[mock_request stub] andReturn:url] URL]; | 160 [[[mock_request stub] andReturn:url] URL]; |
158 EXPECT_FALSE(IsStaticFileRequest(mock_request)); | 161 EXPECT_FALSE(IsStaticFileRequest(mock_request)); |
159 } | 162 } |
160 | 163 |
161 } // namespace web | 164 } // namespace web |
OLD | NEW |