1 // Copyright 2013 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 "chrome/browser/translate/translate_script.h" 6 7 #include "base/bind.h" 8 #include "base/command_line.h" 9 #include "base/strings/stringprintf.h" 10 #include "chrome/common/chrome_switches.h" 11 #include "net/base/load_flags.h" 12 #include "net/base/url_util.h" 13 #include "net/http/http_request_headers.h" 14 #include "net/url_request/test_url_fetcher_factory.h" 15 #include "testing/gtest/include/gtest/gtest.h" 16 #include "url/gurl.h" 17 18 class TranslateScriptTest : public testing::Test { 19 public: 20 TranslateScriptTest() : testing::Test() {} 21 22 protected: 23 virtual void SetUp() { 24 script_.reset(new TranslateScript); 25 DCHECK(script_.get()); 26 } 27 28 virtual void TearDown() { 29 script_.reset(); 30 } 31 32 void Request() { 33 script_->Request( 34 base::Bind(&TranslateScriptTest::OnComplete, base::Unretained(this))); 35 } 36 37 net::TestURLFetcher* GetTestURLFetcher() { 38 return url_fetcher_factory_.GetFetcherByID(TranslateScript::kFetcherId); 39 } 40 41 private: 42 void OnComplete(bool success, const std::string& script) { 43 } 44 45 scoped_ptr<TranslateScript> script_; 46 net::TestURLFetcherFactory url_fetcher_factory_; 47 48 DISALLOW_COPY_AND_ASSIGN(TranslateScriptTest); 49 }; 50 51 TEST_F(TranslateScriptTest, CheckScriptParameters) { 52 Request(); 53 net::TestURLFetcher* fetcher = GetTestURLFetcher(); 54 ASSERT_TRUE(fetcher); 55 56 GURL expected_url(TranslateScript::kScriptURL); 57 GURL url = fetcher->GetOriginalURL(); 58 EXPECT_TRUE(url.is_valid()); 59 EXPECT_EQ(expected_url.GetOrigin().spec(), url.GetOrigin().spec()); 60 EXPECT_EQ(expected_url.path(), url.path()); 61 62 int load_flags = fetcher->GetLoadFlags(); 63 EXPECT_EQ(net::LOAD_DO_NOT_SEND_COOKIES, 64 load_flags & net::LOAD_DO_NOT_SEND_COOKIES); 65 EXPECT_EQ(net::LOAD_DO_NOT_SAVE_COOKIES, 66 load_flags & net::LOAD_DO_NOT_SAVE_COOKIES); 67 68 std::string expected_extra_headers = 69 base::StringPrintf("%s\r\n\r\n", TranslateScript::kRequestHeader); 70 net::HttpRequestHeaders extra_headers; 71 fetcher->GetExtraRequestHeaders(&extra_headers); 72 EXPECT_EQ(expected_extra_headers, extra_headers.ToString()); 73 74 std::string always_use_ssl; 75 net::GetValueForKeyInQuery( 76 url, TranslateScript::kAlwaysUseSslQueryName, &always_use_ssl); 77 EXPECT_EQ(std::string(TranslateScript::kAlwaysUseSslQueryValue), 78 always_use_ssl); 79 80 std::string callback; 81 net::GetValueForKeyInQuery( 82 url, TranslateScript::kCallbackQueryName, &callback); 83 EXPECT_EQ(std::string(TranslateScript::kCallbackQueryValue), callback); 84 85 std::string css_loader_callback; 86 net::GetValueForKeyInQuery( 87 url, TranslateScript::kCssLoaderCallbackQueryName, &css_loader_callback); 88 EXPECT_EQ(std::string(TranslateScript::kCssLoaderCallbackQueryValue), 89 css_loader_callback); 90 91 std::string javascript_loader_callback; 92 net::GetValueForKeyInQuery( 93 url, 94 TranslateScript::kJavascriptLoaderCallbackQueryName, 95 &javascript_loader_callback); 96 EXPECT_EQ(std::string(TranslateScript::kJavascriptLoaderCallbackQueryValue), 97 javascript_loader_callback); 98 } 99 100 TEST_F(TranslateScriptTest, CheckScriptURL) { 101 const std::string script_url("http://www.tamurayukari.com/mero-n.js"); 102 CommandLine* command_line = CommandLine::ForCurrentProcess(); 103 command_line->AppendSwitchASCII(switches::kTranslateScriptURL, 104 script_url); 105 106 Request(); 107 net::TestURLFetcher* fetcher = GetTestURLFetcher(); 108 ASSERT_TRUE(fetcher); 109 110 GURL expected_url(script_url); 111 GURL url = fetcher->GetOriginalURL(); 112 EXPECT_TRUE(url.is_valid()); 113 EXPECT_EQ(expected_url.GetOrigin().spec(), url.GetOrigin().spec()); 114 EXPECT_EQ(expected_url.path(), url.path()); 115 } 116