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 class LoginPromptObserver : public content::NotificationObserver { | |
wtc
2012/08/22 19:42:27
Nit: it may be a good idea to add a comment to des
bashi
2012/08/23 09:54:15
Done.
| |
30 public: | |
31 LoginPromptObserver() | |
32 : auth_handled_(false) {} | |
wtc
2012/08/22 19:42:27
Nit: you can define this constructor on a single l
bashi
2012/08/23 09:54:15
Done.
| |
33 | |
34 virtual void Observe(int type, | |
35 const content::NotificationSource& source, | |
36 const content::NotificationDetails& details) OVERRIDE { | |
37 if (type == chrome::NOTIFICATION_AUTH_NEEDED) { | |
38 LoginNotificationDetails* login_details = | |
39 content::Details<LoginNotificationDetails>(details).ptr(); | |
40 login_details->handler()->SetAuth(ASCIIToUTF16("foo"), | |
41 ASCIIToUTF16("bar")); | |
wtc
2012/08/22 19:42:27
The fact that "foo:bar" is the required username:p
bashi
2012/08/23 09:54:15
Done.
| |
42 auth_handled_ = true; | |
43 } | |
44 } | |
45 | |
46 bool AuthHandled() const { return auth_handled_; } | |
wtc
2012/08/22 19:42:27
Nit: our Style Guide recommends naming this getter
bashi
2012/08/23 09:54:15
Done.
| |
47 | |
48 private: | |
49 bool auth_handled_; | |
50 }; | |
51 | |
52 class ProxyBrowserTest : public InProcessBrowserTest { | |
53 public: | |
54 ProxyBrowserTest() | |
55 : proxy_server_(net::TestServer::TYPE_BASIC_AUTH_PROXY, | |
56 net::TestServer::kLocalhost, | |
57 FilePath()) { | |
58 } | |
59 | |
60 virtual void SetUp() OVERRIDE { | |
61 ASSERT_TRUE(proxy_server_.Start()); | |
62 InProcessBrowserTest::SetUp(); | |
63 } | |
64 | |
65 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | |
66 command_line->AppendSwitchASCII(switches::kProxyServer, | |
67 proxy_server_.host_port_pair().ToString()); | |
68 } | |
69 | |
70 protected: | |
71 net::TestServer proxy_server_; | |
72 }; | |
73 | |
74 // Test that the browser can establish a WebSocket connection via a proxy | |
75 // that requires basic authentication. | |
76 IN_PROC_BROWSER_TEST_F(ProxyBrowserTest, BasicAuthWSConnect) { | |
77 // Launch WebSocket server. | |
78 content::TestWebSocketServer ws_server; | |
79 int port = ws_server.UseRandomPort(); | |
80 FilePath ws_root_dir; | |
81 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &ws_root_dir)); | |
82 ASSERT_TRUE(ws_server.Start(ws_root_dir)); | |
83 | |
84 content::WebContents* tab = chrome::GetActiveWebContents(browser()); | |
85 content::NavigationController* controller = &tab->GetController(); | |
86 content::NotificationRegistrar registrar; | |
87 // The proxy server will request basic authentication. | |
88 // |observer| supplies the credential. | |
89 LoginPromptObserver observer; | |
90 registrar.Add(&observer, chrome::NOTIFICATION_AUTH_NEEDED, | |
91 content::Source<content::NavigationController>(controller)); | |
92 | |
93 content::TitleWatcher watcher(tab, ASCIIToUTF16("PASS")); | |
94 watcher.AlsoWaitForTitle(ASCIIToUTF16("FAIL")); | |
95 | |
96 // Visit a page that tries to establish WebSocket connection. The title | |
97 // of the page will be 'PASS' on success. | |
98 std::string url_path = | |
99 StringPrintf("%s%d%s", "http://localhost:", port, "/ws.html"); | |
cbentzel
2012/08/22 20:19:43
Perhaps not in this CL - but it seems like it woul
bashi
2012/08/23 09:54:15
I agree. I'll make another CL for it after this CL
| |
100 ui_test_utils::NavigateToURL(browser(), GURL(url_path)); | |
101 | |
102 const string16 result = watcher.WaitAndGetTitle(); | |
103 EXPECT_TRUE(LowerCaseEqualsASCII(result, "pass")); | |
wtc
2012/08/22 19:42:27
Nit: it seems that we already know the page title
bashi
2012/08/23 09:54:15
Done.
| |
104 EXPECT_TRUE(observer.AuthHandled()); | |
105 } | |
106 | |
107 } // namespace | |
OLD | NEW |