OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include <string> |
| 6 |
5 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
6 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
7 #include "chrome/browser/ui/browser.h" | 9 #include "chrome/browser/ui/browser.h" |
8 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 10 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
9 #include "chrome/test/base/in_process_browser_test.h" | 11 #include "chrome/test/base/in_process_browser_test.h" |
10 #include "chrome/test/base/ui_test_utils.h" | 12 #include "chrome/test/base/ui_test_utils.h" |
11 #include "content/public/test/browser_test_utils.h" | 13 #include "content/public/test/browser_test_utils.h" |
12 #include "net/base/test_data_directory.h" | 14 #include "net/base/test_data_directory.h" |
13 #include "net/test/spawned_test_server/spawned_test_server.h" | 15 #include "net/test/spawned_test_server/spawned_test_server.h" |
| 16 #include "url/gurl.h" |
14 | 17 |
15 namespace { | 18 namespace { |
16 | 19 |
17 class WebSocketBrowserTest : public InProcessBrowserTest { | 20 class WebSocketBrowserTest : public InProcessBrowserTest { |
18 public: | 21 public: |
19 WebSocketBrowserTest() | 22 WebSocketBrowserTest() |
20 : ws_server_(net::SpawnedTestServer::TYPE_WS, | 23 : ws_server_(net::SpawnedTestServer::TYPE_WS, |
21 net::SpawnedTestServer::kLocalhost, | 24 net::SpawnedTestServer::kLocalhost, |
22 net::GetWebSocketTestDataDirectory()), | 25 net::GetWebSocketTestDataDirectory()), |
23 wss_server_(net::SpawnedTestServer::TYPE_WSS, | 26 wss_server_(net::SpawnedTestServer::TYPE_WSS, |
24 SSLOptions(SSLOptions::CERT_OK), | 27 SSLOptions(SSLOptions::CERT_OK), |
25 net::GetWebSocketTestDataDirectory()) { | 28 net::GetWebSocketTestDataDirectory()) {} |
| 29 |
| 30 protected: |
| 31 void NavigateToHTTP(const std::string& path) { |
| 32 // Visit a HTTP page for testing. |
| 33 std::string scheme("http"); |
| 34 GURL::Replacements replacements; |
| 35 replacements.SetSchemeStr(scheme); |
| 36 ui_test_utils::NavigateToURL( |
| 37 browser(), ws_server_.GetURL(path).ReplaceComponents(replacements)); |
26 } | 38 } |
27 | 39 |
28 protected: | 40 void NavigateToHTTPS(const std::string& path) { |
| 41 // Visit a HTTPS page for testing. |
| 42 std::string scheme("https"); |
| 43 GURL::Replacements replacements; |
| 44 replacements.SetSchemeStr(scheme); |
| 45 ui_test_utils::NavigateToURL( |
| 46 browser(), wss_server_.GetURL(path).ReplaceComponents(replacements)); |
| 47 } |
| 48 |
29 net::SpawnedTestServer ws_server_; | 49 net::SpawnedTestServer ws_server_; |
30 net::SpawnedTestServer wss_server_; | 50 net::SpawnedTestServer wss_server_; |
31 | 51 |
32 private: | 52 private: |
33 typedef net::SpawnedTestServer::SSLOptions SSLOptions; | 53 typedef net::SpawnedTestServer::SSLOptions SSLOptions; |
34 | 54 |
35 DISALLOW_COPY_AND_ASSIGN(WebSocketBrowserTest); | 55 DISALLOW_COPY_AND_ASSIGN(WebSocketBrowserTest); |
36 }; | 56 }; |
37 | 57 |
38 // Test that the browser can handle a WebSocket frame split into multiple TCP | 58 // Test that the browser can handle a WebSocket frame split into multiple TCP |
39 // segments. | 59 // segments. |
40 IN_PROC_BROWSER_TEST_F(WebSocketBrowserTest, WebSocketSplitSegments) { | 60 IN_PROC_BROWSER_TEST_F(WebSocketBrowserTest, WebSocketSplitSegments) { |
41 // Launch a WebSocket server. | 61 // Launch a WebSocket server. |
42 ASSERT_TRUE(ws_server_.Start()); | 62 ASSERT_TRUE(ws_server_.Start()); |
43 | 63 |
44 // Setup page title observer. | 64 // Setup page title observer. |
45 content::WebContents* tab = | 65 content::WebContents* tab = |
46 browser()->tab_strip_model()->GetActiveWebContents(); | 66 browser()->tab_strip_model()->GetActiveWebContents(); |
47 content::TitleWatcher watcher(tab, base::ASCIIToUTF16("PASS")); | 67 content::TitleWatcher watcher(tab, base::ASCIIToUTF16("PASS")); |
48 watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL")); | 68 watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL")); |
49 | 69 |
50 // Visit a HTTP page for testing. | 70 NavigateToHTTP("split_packet_check.html"); |
51 std::string scheme("http"); | |
52 GURL::Replacements replacements; | |
53 replacements.SetSchemeStr(scheme); | |
54 ui_test_utils::NavigateToURL( | |
55 browser(), | |
56 ws_server_.GetURL( | |
57 "split_packet_check.html").ReplaceComponents(replacements)); | |
58 | 71 |
59 const base::string16 result = watcher.WaitAndGetTitle(); | 72 const base::string16 result = watcher.WaitAndGetTitle(); |
60 EXPECT_TRUE(EqualsASCII(result, "PASS")); | 73 EXPECT_EQ(base::ASCIIToUTF16("PASS"), result); |
61 } | 74 } |
62 | 75 |
63 IN_PROC_BROWSER_TEST_F(WebSocketBrowserTest, SecureWebSocketSplitRecords) { | 76 IN_PROC_BROWSER_TEST_F(WebSocketBrowserTest, SecureWebSocketSplitRecords) { |
64 // Launch a secure WebSocket server. | 77 // Launch a secure WebSocket server. |
65 ASSERT_TRUE(wss_server_.Start()); | 78 ASSERT_TRUE(wss_server_.Start()); |
66 | 79 |
67 // Setup page title observer. | 80 // Setup page title observer. |
68 content::WebContents* tab = | 81 content::WebContents* tab = |
69 browser()->tab_strip_model()->GetActiveWebContents(); | 82 browser()->tab_strip_model()->GetActiveWebContents(); |
70 content::TitleWatcher watcher(tab, base::ASCIIToUTF16("PASS")); | 83 content::TitleWatcher watcher(tab, base::ASCIIToUTF16("PASS")); |
71 watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL")); | 84 watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL")); |
72 | 85 |
73 // Visit a HTTPS page for testing. | 86 NavigateToHTTPS("split_packet_check.html"); |
74 std::string scheme("https"); | |
75 GURL::Replacements replacements; | |
76 replacements.SetSchemeStr(scheme); | |
77 ui_test_utils::NavigateToURL( | |
78 browser(), | |
79 wss_server_.GetURL( | |
80 "split_packet_check.html").ReplaceComponents(replacements)); | |
81 | 87 |
82 const base::string16 result = watcher.WaitAndGetTitle(); | 88 const base::string16 result = watcher.WaitAndGetTitle(); |
83 EXPECT_TRUE(EqualsASCII(result, "PASS")); | 89 EXPECT_EQ(base::ASCIIToUTF16("PASS"), result); |
| 90 } |
| 91 |
| 92 IN_PROC_BROWSER_TEST_F(WebSocketBrowserTest, SendCloseFrameWhenTabIsClosed) { |
| 93 // Launch a WebSocket server. |
| 94 ASSERT_TRUE(ws_server_.Start()); |
| 95 |
| 96 content::WebContents* tab = |
| 97 browser()->tab_strip_model()->GetActiveWebContents(); |
| 98 { |
| 99 // Create a new tab, establish a WebSocket connection and close the tab. |
| 100 content::WebContents* new_tab = content::WebContents::Create( |
| 101 content::WebContents::CreateParams(tab->GetBrowserContext())); |
| 102 browser()->tab_strip_model()->AppendWebContents(new_tab, true); |
| 103 ASSERT_EQ(new_tab, browser()->tab_strip_model()->GetWebContentsAt(1)); |
| 104 |
| 105 content::TitleWatcher connected_title_watcher( |
| 106 new_tab, base::ASCIIToUTF16("CONNECTED")); |
| 107 connected_title_watcher.AlsoWaitForTitle(base::ASCIIToUTF16("CLOSED")); |
| 108 NavigateToHTTP("counted_connection.html"); |
| 109 const base::string16 result = connected_title_watcher.WaitAndGetTitle(); |
| 110 EXPECT_TRUE(EqualsASCII(result, "CONNECTED")); |
| 111 |
| 112 content::WebContentsDestroyedWatcher destroyed_watcher(new_tab); |
| 113 browser()->tab_strip_model()->CloseWebContentsAt(1, 0); |
| 114 destroyed_watcher.Wait(); |
| 115 } |
| 116 |
| 117 content::TitleWatcher title_watcher(tab, base::ASCIIToUTF16("PASS")); |
| 118 title_watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL")); |
| 119 |
| 120 NavigateToHTTP("count_connection.html"); |
| 121 const base::string16 result = title_watcher.WaitAndGetTitle(); |
| 122 EXPECT_EQ(base::ASCIIToUTF16("PASS"), result); |
84 } | 123 } |
85 | 124 |
86 } // namespace | 125 } // namespace |
OLD | NEW |