[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/command_line.h" |
| 6 | #include "base/path_service.h" |
| 7 | #include "base/string_util.h" |
| 8 | #include "base/stringprintf.h" |
| 9 | #include "base/utf_string_conversions.h" |
| 10 | #include "chrome/browser/ui/browser.h" |
| 11 | #include "chrome/browser/ui/browser_tabstrip.h" |
| 12 | #include "chrome/browser/ui/login/login_prompt.h" |
| 13 | #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 14 | #include "chrome/common/chrome_notification_types.h" |
| 15 | #include "chrome/common/chrome_paths.h" |
| 16 | #include "chrome/common/chrome_switches.h" |
| 17 | #include "chrome/common/pref_names.h" |
| 18 | #include "chrome/test/base/in_process_browser_test.h" |
| 19 | #include "chrome/test/base/ui_test_utils.h" |
| 20 | #include "content/public/browser/notification_details.h" |
| 21 | #include "content/public/browser/notification_source.h" |
| 22 | #include "content/public/browser/web_contents.h" |
| 23 | #include "content/public/browser/web_contents_observer.h" |
| 24 | #include "content/public/test/browser_test_utils.h" |
| 25 | #include "net/test/test_server.h" |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | // This class observes chrome::NOTIFICATION_AUTH_NEEDED and supplies |
| 30 | // the credential which is required by the test proxy server. |
| 31 | // "foo:bar" is the required username and password for our test proxy server. |
| 32 | class LoginPromptObserver : public content::NotificationObserver { |
| 33 | public: |
| 34 | LoginPromptObserver() : auth_handled_(false) {} |
| 35 | |
| 36 | virtual void Observe(int type, |
| 37 | const content::NotificationSource& source, |
| 38 | const content::NotificationDetails& details) OVERRIDE { |
| 39 | if (type == chrome::NOTIFICATION_AUTH_NEEDED) { |
| 40 | LoginNotificationDetails* login_details = |
| 41 | content::Details<LoginNotificationDetails>(details).ptr(); |
| 42 | // |login_details->handler()| is the associated LoginHandler object. |
| 43 | // SetAuth() will close the login dialog. |
| 44 | login_details->handler()->SetAuth(ASCIIToUTF16("foo"), |
| 45 | ASCIIToUTF16("bar")); |
| 46 | auth_handled_ = true; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | bool auth_handled() const { return auth_handled_; } |
| 51 | |
| 52 | private: |
| 53 | bool auth_handled_; |
| 54 | }; |
| 55 | |
| 56 | class ProxyBrowserTest : public InProcessBrowserTest { |
| 57 | public: |
| 58 | ProxyBrowserTest() |
| 59 | : proxy_server_(net::TestServer::TYPE_BASIC_AUTH_PROXY, |
| 60 | net::TestServer::kLocalhost, |
| 61 | FilePath()) { |
| 62 | } |
| 63 | |
| 64 | virtual void SetUp() OVERRIDE { |
| 65 | ASSERT_TRUE(proxy_server_.Start()); |
| 66 | InProcessBrowserTest::SetUp(); |
| 67 | } |
| 68 | |
| 69 | virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 70 | command_line->AppendSwitchASCII(switches::kProxyServer, |
| 71 | proxy_server_.host_port_pair().ToString()); |
| 72 | } |
| 73 | |
| 74 | protected: |
| 75 | net::TestServer proxy_server_; |
| 76 | }; |
| 77 | |
| 78 | #if defined(OS_CHROMEOS) |
| 79 | // We bypass manually installed proxy for localhost on chromeos. |
| 80 | #define MAYBE_BasicAuthWSConnect DISABLED_BasicAuthWSConnect |
| 81 | #else |
| 82 | #define MAYBE_BasicAuthWSConnect BasicAuthWSConnect |
| 83 | #endif |
| 84 | // Test that the browser can establish a WebSocket connection via a proxy |
| 85 | // that requires basic authentication. |
| 86 | IN_PROC_BROWSER_TEST_F(ProxyBrowserTest, MAYBE_BasicAuthWSConnect) { |
| 87 | // Launch WebSocket server. |
[email protected] | e0e6f9f | 2012-10-24 05:35:44 | [diff] [blame^] | 88 | net::TestServer ws_server(net::TestServer::TYPE_WS, |
| 89 | net::TestServer::kLocalhost, |
| 90 | FilePath(FILE_PATH_LITERAL("net/data/websocket"))); |
| 91 | ASSERT_TRUE(ws_server.Start()); |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 92 | |
| 93 | content::WebContents* tab = chrome::GetActiveWebContents(browser()); |
| 94 | content::NavigationController* controller = &tab->GetController(); |
| 95 | content::NotificationRegistrar registrar; |
| 96 | // The proxy server will request basic authentication. |
| 97 | // |observer| supplies the credential. |
| 98 | LoginPromptObserver observer; |
| 99 | registrar.Add(&observer, chrome::NOTIFICATION_AUTH_NEEDED, |
| 100 | content::Source<content::NavigationController>(controller)); |
| 101 | |
| 102 | content::TitleWatcher watcher(tab, ASCIIToUTF16("PASS")); |
| 103 | watcher.AlsoWaitForTitle(ASCIIToUTF16("FAIL")); |
| 104 | |
| 105 | // Visit a page that tries to establish WebSocket connection. The title |
| 106 | // of the page will be 'PASS' on success. |
[email protected] | e0e6f9f | 2012-10-24 05:35:44 | [diff] [blame^] | 107 | std::string scheme("http"); |
| 108 | GURL::Replacements replacements; |
| 109 | replacements.SetSchemeStr(scheme); |
| 110 | ui_test_utils::NavigateToURL( |
| 111 | browser(), |
| 112 | ws_server.GetURL("connect_check.html").ReplaceComponents(replacements)); |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 113 | |
| 114 | const string16 result = watcher.WaitAndGetTitle(); |
| 115 | EXPECT_TRUE(EqualsASCII(result, "PASS")); |
| 116 | EXPECT_TRUE(observer.auth_handled()); |
| 117 | } |
| 118 | |
| 119 | } // namespace |