OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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. |
| 4 |
| 5 #include <atlbase.h> |
| 6 #include <atlcom.h> |
| 7 |
| 8 #include "base/string_util.h" |
| 9 #include "chrome_frame/http_negotiate.h" |
| 10 #include "chrome_frame/html_utils.h" |
| 11 #include "gtest/gtest.h" |
| 12 #include "gmock/gmock.h" |
| 13 |
| 14 class HttpNegotiateTest : public testing::Test { |
| 15 protected: |
| 16 HttpNegotiateTest() { |
| 17 } |
| 18 }; |
| 19 |
| 20 |
| 21 class TestHttpNegotiate |
| 22 : public CComObjectRootEx<CComMultiThreadModel>, |
| 23 public IHttpNegotiate { |
| 24 public: |
| 25 TestHttpNegotiate() |
| 26 : beginning_transaction_ret_(S_OK), additional_headers_(NULL) { |
| 27 } |
| 28 |
| 29 BEGIN_COM_MAP(TestHttpNegotiate) |
| 30 COM_INTERFACE_ENTRY(IHttpNegotiate) |
| 31 END_COM_MAP() |
| 32 STDMETHOD(BeginningTransaction)(LPCWSTR url, LPCWSTR headers, // NOLINT |
| 33 DWORD reserved, // NOLINT |
| 34 LPWSTR* additional_headers) { // NOLINT |
| 35 if (additional_headers_) { |
| 36 int len = lstrlenW(additional_headers_); |
| 37 len++; |
| 38 *additional_headers = reinterpret_cast<wchar_t*>( |
| 39 ::CoTaskMemAlloc(len * sizeof(wchar_t))); |
| 40 lstrcpyW(*additional_headers, additional_headers_); |
| 41 } |
| 42 return beginning_transaction_ret_; |
| 43 } |
| 44 |
| 45 STDMETHOD(OnResponse)(DWORD response_code, LPCWSTR response_header, |
| 46 LPCWSTR request_header, |
| 47 LPWSTR* additional_request_headers) { |
| 48 return S_OK; |
| 49 } |
| 50 |
| 51 HRESULT beginning_transaction_ret_; |
| 52 const wchar_t* additional_headers_; |
| 53 }; |
| 54 |
| 55 TEST_F(HttpNegotiateTest, BeginningTransaction) { |
| 56 static const int kBeginningTransactionIndex = 3; |
| 57 CComObjectStackEx<TestHttpNegotiate> test_http; |
| 58 IHttpNegotiate_BeginningTransaction_Fn original = |
| 59 reinterpret_cast<IHttpNegotiate_BeginningTransaction_Fn>( |
| 60 (*reinterpret_cast<void***>( |
| 61 static_cast<IHttpNegotiate*>(&test_http)))[kBeginningTransactionIndex]); |
| 62 |
| 63 std::wstring cf_ua( |
| 64 ASCIIToWide(http_utils::GetDefaultUserAgentHeaderWithCFTag())); |
| 65 std::wstring cf_tag( |
| 66 ASCIIToWide(http_utils::GetChromeFrameUserAgent())); |
| 67 |
| 68 EXPECT_NE(std::wstring::npos, cf_ua.find(cf_tag)); |
| 69 |
| 70 struct TestCase { |
| 71 const std::wstring original_headers_; |
| 72 const std::wstring delegate_additional_; |
| 73 const std::wstring expected_additional_; |
| 74 HRESULT delegate_return_value_; |
| 75 } test_cases[] = { |
| 76 { L"Accept: */*\r\n\r\n", |
| 77 L"", |
| 78 cf_ua + L"\r\n\r\n", |
| 79 S_OK }, |
| 80 { L"Accept: */*\r\n\r\n", |
| 81 L"", |
| 82 L"", |
| 83 E_OUTOFMEMORY }, |
| 84 { L"", |
| 85 L"Accept: */*\r\n\r\n", |
| 86 L"Accept: */*\r\n" + cf_ua + L"\r\n\r\n", |
| 87 S_OK }, |
| 88 { L"User-Agent: Bingo/1.0\r\n\r\n", |
| 89 L"", |
| 90 L"User-Agent: Bingo/1.0 " + cf_tag + L"\r\n\r\n", |
| 91 S_OK }, |
| 92 { L"User-Agent: NotMe/1.0\r\n\r\n", |
| 93 L"User-Agent: MeMeMe/1.0\r\n\r\n", |
| 94 L"User-Agent: MeMeMe/1.0 " + cf_tag + L"\r\n\r\n", |
| 95 S_OK }, |
| 96 { L"", |
| 97 L"User-Agent: MeMeMe/1.0\r\n\r\n", |
| 98 L"User-Agent: MeMeMe/1.0 " + cf_tag + L"\r\n\r\n", |
| 99 S_OK }, |
| 100 }; |
| 101 |
| 102 for (int i = 0; i < arraysize(test_cases); ++i) { |
| 103 TestCase& test = test_cases[i]; |
| 104 wchar_t* additional = NULL; |
| 105 test_http.beginning_transaction_ret_ = test.delegate_return_value_; |
| 106 test_http.additional_headers_ = test.delegate_additional_.c_str(); |
| 107 HttpNegotiatePatch::BeginningTransaction(original, &test_http, |
| 108 L"http://www.google.com", test.original_headers_.c_str(), 0, |
| 109 &additional); |
| 110 EXPECT_TRUE(additional != NULL); |
| 111 |
| 112 if (additional) { |
| 113 // Check against the expected additional headers. |
| 114 EXPECT_EQ(test.expected_additional_, std::wstring(additional)); |
| 115 ::CoTaskMemFree(additional); |
| 116 } |
| 117 } |
| 118 } |
OLD | NEW |