| OLD | NEW | 
|---|
| (Empty) |  | 
|  | 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. | 
|  | 88   content::TestWebSocketServer ws_server; | 
|  | 89   int port = ws_server.UseRandomPort(); | 
|  | 90   FilePath ws_root_dir; | 
|  | 91   ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &ws_root_dir)); | 
|  | 92   ASSERT_TRUE(ws_server.Start(ws_root_dir)); | 
|  | 93 | 
|  | 94   content::WebContents* tab = chrome::GetActiveWebContents(browser()); | 
|  | 95   content::NavigationController* controller = &tab->GetController(); | 
|  | 96   content::NotificationRegistrar registrar; | 
|  | 97   // The proxy server will request basic authentication. | 
|  | 98   // |observer| supplies the credential. | 
|  | 99   LoginPromptObserver observer; | 
|  | 100   registrar.Add(&observer, chrome::NOTIFICATION_AUTH_NEEDED, | 
|  | 101                 content::Source<content::NavigationController>(controller)); | 
|  | 102 | 
|  | 103   content::TitleWatcher watcher(tab, ASCIIToUTF16("PASS")); | 
|  | 104   watcher.AlsoWaitForTitle(ASCIIToUTF16("FAIL")); | 
|  | 105 | 
|  | 106   // Visit a page that tries to establish WebSocket connection. The title | 
|  | 107   // of the page will be 'PASS' on success. | 
|  | 108   // TODO(bashi): Add TestWebSocketServer::GetURL() instead creating url here. | 
|  | 109   std::string url_path = | 
|  | 110       StringPrintf("%s%d%s", "http://localhost:", port, "/ws.html"); | 
|  | 111   ui_test_utils::NavigateToURL(browser(), GURL(url_path)); | 
|  | 112 | 
|  | 113   const string16 result = watcher.WaitAndGetTitle(); | 
|  | 114   EXPECT_TRUE(EqualsASCII(result, "PASS")); | 
|  | 115   EXPECT_TRUE(observer.auth_handled()); | 
|  | 116 } | 
|  | 117 | 
|  | 118 }  // namespace | 
| OLD | NEW | 
|---|