OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include <atlbase.h> | 5 #include <atlbase.h> |
6 #include <atlcom.h> | 6 #include <atlcom.h> |
7 | 7 |
| 8 #include "base/string16.h" |
8 #include "base/string_util.h" | 9 #include "base/string_util.h" |
9 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
10 #include "base/win/scoped_bstr.h" | 11 #include "base/win/scoped_bstr.h" |
11 #include "base/win/scoped_comptr.h" | 12 #include "base/win/scoped_comptr.h" |
12 #include "chrome_frame/http_negotiate.h" | 13 #include "chrome_frame/http_negotiate.h" |
13 #include "chrome_frame/html_utils.h" | 14 #include "chrome_frame/html_utils.h" |
| 15 #include "chrome_frame/registry_list_preferences_holder.h" |
14 #include "chrome_frame/test/chrome_frame_test_utils.h" | 16 #include "chrome_frame/test/chrome_frame_test_utils.h" |
15 #include "chrome_frame/utils.h" | 17 #include "chrome_frame/utils.h" |
16 #include "gtest/gtest.h" | 18 #include "gtest/gtest.h" |
17 #include "gmock/gmock.h" | 19 #include "gmock/gmock.h" |
18 | 20 |
19 class HttpNegotiateTest : public testing::Test { | 21 class HttpNegotiateTest : public testing::Test { |
20 protected: | 22 protected: |
21 HttpNegotiateTest() { | 23 HttpNegotiateTest() { |
22 } | 24 } |
23 }; | 25 }; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 | 60 |
59 TEST_F(HttpNegotiateTest, BeginningTransaction) { | 61 TEST_F(HttpNegotiateTest, BeginningTransaction) { |
60 static const int kBeginningTransactionIndex = 3; | 62 static const int kBeginningTransactionIndex = 3; |
61 CComObjectStackEx<TestHttpNegotiate> test_http; | 63 CComObjectStackEx<TestHttpNegotiate> test_http; |
62 IHttpNegotiate_BeginningTransaction_Fn original = | 64 IHttpNegotiate_BeginningTransaction_Fn original = |
63 reinterpret_cast<IHttpNegotiate_BeginningTransaction_Fn>( | 65 reinterpret_cast<IHttpNegotiate_BeginningTransaction_Fn>( |
64 (*reinterpret_cast<void***>( | 66 (*reinterpret_cast<void***>( |
65 static_cast<IHttpNegotiate*>( | 67 static_cast<IHttpNegotiate*>( |
66 &test_http)))[kBeginningTransactionIndex]); | 68 &test_http)))[kBeginningTransactionIndex]); |
67 | 69 |
68 std::wstring cf_ua( | 70 string16 cf_ua( |
69 ASCIIToWide(http_utils::GetDefaultUserAgentHeaderWithCFTag())); | 71 ASCIIToWide(http_utils::GetDefaultUserAgentHeaderWithCFTag())); |
70 std::wstring cf_tag( | 72 string16 cf_tag( |
71 ASCIIToWide(http_utils::GetChromeFrameUserAgent())); | 73 ASCIIToWide(http_utils::GetChromeFrameUserAgent())); |
72 | 74 |
73 EXPECT_NE(std::wstring::npos, cf_ua.find(L"chromeframe/")); | 75 EXPECT_NE(string16::npos, cf_ua.find(L"chromeframe/")); |
74 | 76 |
75 struct TestCase { | 77 struct TestCase { |
76 const std::wstring original_headers_; | 78 const string16 original_headers_; |
77 const std::wstring delegate_additional_; | 79 const string16 delegate_additional_; |
78 const std::wstring expected_additional_; | 80 const string16 expected_additional_; |
79 HRESULT delegate_return_value_; | 81 HRESULT delegate_return_value_; |
80 } test_cases[] = { | 82 } test_cases[] = { |
81 { L"Accept: */*\r\n", | 83 { L"Accept: */*\r\n", |
82 L"", | 84 L"", |
83 cf_ua + L"\r\n", | 85 cf_ua + L"\r\n", |
84 S_OK }, | 86 S_OK }, |
85 { L"Accept: */*\r\n", | 87 { L"Accept: */*\r\n", |
86 L"", | 88 L"", |
87 L"", | 89 L"", |
88 E_OUTOFMEMORY }, | 90 E_OUTOFMEMORY }, |
(...skipping 20 matching lines...) Expand all Loading... |
109 wchar_t* additional = NULL; | 111 wchar_t* additional = NULL; |
110 test_http.beginning_transaction_ret_ = test.delegate_return_value_; | 112 test_http.beginning_transaction_ret_ = test.delegate_return_value_; |
111 test_http.additional_headers_ = test.delegate_additional_.c_str(); | 113 test_http.additional_headers_ = test.delegate_additional_.c_str(); |
112 HttpNegotiatePatch::BeginningTransaction(original, &test_http, | 114 HttpNegotiatePatch::BeginningTransaction(original, &test_http, |
113 L"http://www.google.com", test.original_headers_.c_str(), 0, | 115 L"http://www.google.com", test.original_headers_.c_str(), 0, |
114 &additional); | 116 &additional); |
115 EXPECT_TRUE(additional != NULL); | 117 EXPECT_TRUE(additional != NULL); |
116 | 118 |
117 if (additional) { | 119 if (additional) { |
118 // Check against the expected additional headers. | 120 // Check against the expected additional headers. |
119 EXPECT_EQ(test.expected_additional_, std::wstring(additional)); | 121 EXPECT_EQ(test.expected_additional_, string16(additional)); |
120 ::CoTaskMemFree(additional); | 122 ::CoTaskMemFree(additional); |
121 } | 123 } |
122 } | 124 } |
123 } | 125 } |
124 | 126 |
| 127 TEST_F(HttpNegotiateTest, BeginningTransactionUARemoval) { |
| 128 static const int kBeginningTransactionIndex = 3; |
| 129 CComObjectStackEx<TestHttpNegotiate> test_http; |
| 130 IHttpNegotiate_BeginningTransaction_Fn original = |
| 131 reinterpret_cast<IHttpNegotiate_BeginningTransaction_Fn>( |
| 132 (*reinterpret_cast<void***>( |
| 133 static_cast<IHttpNegotiate*>( |
| 134 &test_http)))[kBeginningTransactionIndex]); |
| 135 |
| 136 string16 nocf_ua( |
| 137 ASCIIToWide(http_utils::RemoveChromeFrameFromUserAgentValue( |
| 138 http_utils::GetDefaultUserAgentHeaderWithCFTag()))); |
| 139 string16 cf_ua( |
| 140 ASCIIToWide(http_utils::AddChromeFrameToUserAgentValue( |
| 141 WideToASCII(nocf_ua)))); |
| 142 |
| 143 EXPECT_EQ(string16::npos, nocf_ua.find(L"chromeframe/")); |
| 144 EXPECT_NE(string16::npos, cf_ua.find(L"chromeframe/")); |
| 145 |
| 146 string16 ua_url(L"www.withua.com"); |
| 147 string16 no_ua_url(L"www.noua.com"); |
| 148 |
| 149 RegistryListPreferencesHolder& ua_holder = |
| 150 GetUserAgentPreferencesHolderForTesting(); |
| 151 ua_holder.AddStringForTesting(no_ua_url); |
| 152 |
| 153 struct TestCase { |
| 154 const string16 url_; |
| 155 const string16 original_headers_; |
| 156 const string16 delegate_additional_; |
| 157 const string16 expected_additional_; |
| 158 } test_cases[] = { |
| 159 { ua_url, |
| 160 L"", |
| 161 L"Accept: */*\r\n" + cf_ua + L"\r\n", |
| 162 L"Accept: */*\r\n" + cf_ua + L"\r\n" }, |
| 163 { ua_url, |
| 164 L"", |
| 165 L"Accept: */*\r\n" + nocf_ua + L"\r\n", |
| 166 L"Accept: */*\r\n" + cf_ua + L"\r\n" }, |
| 167 { no_ua_url, |
| 168 L"", |
| 169 L"Accept: */*\r\n" + cf_ua + L"\r\n", |
| 170 L"Accept: */*\r\n" + nocf_ua + L"\r\n" }, |
| 171 { no_ua_url, |
| 172 L"", |
| 173 L"Accept: */*\r\n" + nocf_ua + L"\r\n", |
| 174 L"Accept: */*\r\n" + nocf_ua + L"\r\n" }, |
| 175 }; |
| 176 |
| 177 for (int i = 0; i < arraysize(test_cases); ++i) { |
| 178 TestCase& test = test_cases[i]; |
| 179 wchar_t* additional = NULL; |
| 180 test_http.beginning_transaction_ret_ = S_OK; |
| 181 test_http.additional_headers_ = test.delegate_additional_.c_str(); |
| 182 HttpNegotiatePatch::BeginningTransaction(original, &test_http, |
| 183 test.url_.c_str(), test.original_headers_.c_str(), 0, |
| 184 &additional); |
| 185 EXPECT_TRUE(additional != NULL); |
| 186 |
| 187 if (additional) { |
| 188 // Check against the expected additional headers. |
| 189 EXPECT_EQ(test.expected_additional_, string16(additional)) |
| 190 << "Iteration: " << i; |
| 191 ::CoTaskMemFree(additional); |
| 192 } |
| 193 } |
| 194 } |
| 195 |
| 196 |
125 class TestInternetProtocolSink | 197 class TestInternetProtocolSink |
126 : public CComObjectRootEx<CComMultiThreadModel>, | 198 : public CComObjectRootEx<CComMultiThreadModel>, |
127 public IInternetProtocolSink { | 199 public IInternetProtocolSink { |
128 public: | 200 public: |
129 TestInternetProtocolSink() : status_(0) { | 201 TestInternetProtocolSink() : status_(0) { |
130 // Create an instance of IE to fullfill the requirements of being able | 202 // Create an instance of IE to fullfill the requirements of being able |
131 // to detect whether a sub-frame or top-frame is being loaded (see | 203 // to detect whether a sub-frame or top-frame is being loaded (see |
132 // IsSubFrameRequest) and to be able to mark an IBrowserService | 204 // IsSubFrameRequest) and to be able to mark an IBrowserService |
133 // implementation as a target for CF navigation. | 205 // implementation as a target for CF navigation. |
134 HRESULT hr = browser_.CreateInstance(CLSID_InternetExplorer); | 206 HRESULT hr = browser_.CreateInstance(CLSID_InternetExplorer); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 | 240 |
169 STDMETHOD(ReportResult)(HRESULT hr, DWORD err, LPCWSTR result) { | 241 STDMETHOD(ReportResult)(HRESULT hr, DWORD err, LPCWSTR result) { |
170 NOTREACHED(); | 242 NOTREACHED(); |
171 return S_OK; | 243 return S_OK; |
172 } | 244 } |
173 | 245 |
174 ULONG last_status() const { | 246 ULONG last_status() const { |
175 return status_; | 247 return status_; |
176 } | 248 } |
177 | 249 |
178 const std::wstring& last_status_text() const { | 250 const string16& last_status_text() const { |
179 return status_text_; | 251 return status_text_; |
180 } | 252 } |
181 | 253 |
182 protected: | 254 protected: |
183 ULONG status_; | 255 ULONG status_; |
184 std::wstring status_text_; | 256 string16 status_text_; |
185 base::win::ScopedComPtr<IWebBrowser2> browser_; | 257 base::win::ScopedComPtr<IWebBrowser2> browser_; |
186 }; | 258 }; |
187 | 259 |
188 using testing::AllOf; | 260 using testing::AllOf; |
189 using testing::ContainsRegex; | 261 using testing::ContainsRegex; |
190 using testing::HasSubstr; | 262 using testing::HasSubstr; |
191 | 263 |
192 TEST(AppendUserAgent, Append) { | 264 TEST(AppendUserAgent, Append) { |
193 EXPECT_THAT(AppendCFUserAgentString(NULL, NULL), | 265 EXPECT_THAT(AppendCFUserAgentString(NULL, NULL), |
194 testing::ContainsRegex("User-Agent:.+chromeframe.+\r\n")); | 266 testing::ContainsRegex("User-Agent:.+chromeframe.+\r\n")); |
(...skipping 14 matching lines...) Expand all Loading... |
209 | 281 |
210 // Check additional headers are preserved. | 282 // Check additional headers are preserved. |
211 EXPECT_THAT(AppendCFUserAgentString(NULL, | 283 EXPECT_THAT(AppendCFUserAgentString(NULL, |
212 L"Authorization: A Zoo That I Ruin\r\n" | 284 L"Authorization: A Zoo That I Ruin\r\n" |
213 L"User-Agent: Get a Nurse;\r\n" | 285 L"User-Agent: Get a Nurse;\r\n" |
214 L"Accept-Language: Cleanup a Cat Egg\r\n"), | 286 L"Accept-Language: Cleanup a Cat Egg\r\n"), |
215 AllOf(ContainsRegex("User-Agent: Get a Nurse; chromeframe.+\r\n"), | 287 AllOf(ContainsRegex("User-Agent: Get a Nurse; chromeframe.+\r\n"), |
216 HasSubstr("Authorization: A Zoo That I Ruin\r\n"), | 288 HasSubstr("Authorization: A Zoo That I Ruin\r\n"), |
217 HasSubstr("Accept-Language: Cleanup a Cat Egg\r\n"))); | 289 HasSubstr("Accept-Language: Cleanup a Cat Egg\r\n"))); |
218 } | 290 } |
OLD | NEW |