blob: 791c16baec1311e84c54758c39fb5476956025df [file] [log] [blame]
[email protected]d46ca7302012-09-08 17:37:241// 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"
[email protected]d46ca7302012-09-08 17:37:2411#include "chrome/browser/ui/login/login_prompt.h"
[email protected]47ae23372013-01-29 01:50:4812#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]d46ca7302012-09-08 17:37:2413#include "chrome/common/chrome_notification_types.h"
14#include "chrome/common/chrome_paths.h"
15#include "chrome/common/chrome_switches.h"
16#include "chrome/common/pref_names.h"
17#include "chrome/test/base/in_process_browser_test.h"
18#include "chrome/test/base/ui_test_utils.h"
19#include "content/public/browser/notification_details.h"
20#include "content/public/browser/notification_source.h"
21#include "content/public/browser/web_contents.h"
22#include "content/public/browser/web_contents_observer.h"
23#include "content/public/test/browser_test_utils.h"
[email protected]4b187da2012-11-06 00:05:2924#include "net/base/test_data_directory.h"
[email protected]d46ca7302012-09-08 17:37:2425#include "net/test/test_server.h"
26
27namespace {
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.
32class 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_;
[email protected]5b7115b32012-12-05 21:38:0954
55 DISALLOW_COPY_AND_ASSIGN(LoginPromptObserver);
[email protected]d46ca7302012-09-08 17:37:2456};
57
58class ProxyBrowserTest : public InProcessBrowserTest {
59 public:
60 ProxyBrowserTest()
61 : proxy_server_(net::TestServer::TYPE_BASIC_AUTH_PROXY,
62 net::TestServer::kLocalhost,
[email protected]650b2d52013-02-10 03:41:4563 base::FilePath()) {
[email protected]d46ca7302012-09-08 17:37:2464 }
65
66 virtual void SetUp() OVERRIDE {
67 ASSERT_TRUE(proxy_server_.Start());
68 InProcessBrowserTest::SetUp();
69 }
70
71 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
72 command_line->AppendSwitchASCII(switches::kProxyServer,
73 proxy_server_.host_port_pair().ToString());
74 }
75
76 protected:
77 net::TestServer proxy_server_;
[email protected]5b7115b32012-12-05 21:38:0978
79 private:
80
81 DISALLOW_COPY_AND_ASSIGN(ProxyBrowserTest);
[email protected]d46ca7302012-09-08 17:37:2482};
83
84#if defined(OS_CHROMEOS)
85// We bypass manually installed proxy for localhost on chromeos.
86#define MAYBE_BasicAuthWSConnect DISABLED_BasicAuthWSConnect
87#else
88#define MAYBE_BasicAuthWSConnect BasicAuthWSConnect
89#endif
90// Test that the browser can establish a WebSocket connection via a proxy
91// that requires basic authentication.
92IN_PROC_BROWSER_TEST_F(ProxyBrowserTest, MAYBE_BasicAuthWSConnect) {
93 // Launch WebSocket server.
[email protected]e0e6f9f2012-10-24 05:35:4494 net::TestServer ws_server(net::TestServer::TYPE_WS,
95 net::TestServer::kLocalhost,
[email protected]4b187da2012-11-06 00:05:2996 net::GetWebSocketTestDataDirectory());
[email protected]e0e6f9f2012-10-24 05:35:4497 ASSERT_TRUE(ws_server.Start());
[email protected]d46ca7302012-09-08 17:37:2498
[email protected]47ae23372013-01-29 01:50:4899 content::WebContents* tab =
100 browser()->tab_strip_model()->GetActiveWebContents();
[email protected]d46ca7302012-09-08 17:37:24101 content::NavigationController* controller = &tab->GetController();
102 content::NotificationRegistrar registrar;
103 // The proxy server will request basic authentication.
104 // |observer| supplies the credential.
105 LoginPromptObserver observer;
106 registrar.Add(&observer, chrome::NOTIFICATION_AUTH_NEEDED,
107 content::Source<content::NavigationController>(controller));
108
109 content::TitleWatcher watcher(tab, ASCIIToUTF16("PASS"));
110 watcher.AlsoWaitForTitle(ASCIIToUTF16("FAIL"));
111
112 // Visit a page that tries to establish WebSocket connection. The title
113 // of the page will be 'PASS' on success.
[email protected]e0e6f9f2012-10-24 05:35:44114 std::string scheme("http");
115 GURL::Replacements replacements;
116 replacements.SetSchemeStr(scheme);
117 ui_test_utils::NavigateToURL(
118 browser(),
119 ws_server.GetURL("connect_check.html").ReplaceComponents(replacements));
[email protected]f20dead2013-03-02 03:01:48120
[email protected]d46ca7302012-09-08 17:37:24121 const string16 result = watcher.WaitAndGetTitle();
122 EXPECT_TRUE(EqualsASCII(result, "PASS"));
123 EXPECT_TRUE(observer.auth_handled());
124}
125
126} // namespace