| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/renderer/chrome_content_renderer_client.h" | 5 #include "chrome/renderer/chrome_content_renderer_client.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 void MonitorRequestHandler(const net::test_server::HttpRequest& request) { | 85 void MonitorRequestHandler(const net::test_server::HttpRequest& request) { |
| 86 // We're only interested in YouTube video embeds | 86 // We're only interested in YouTube video embeds |
| 87 if (request.headers.at("Host").find("youtube.com") == std::string::npos) | 87 if (request.headers.at("Host").find("youtube.com") == std::string::npos) |
| 88 return; | 88 return; |
| 89 | 89 |
| 90 if (request.relative_url.find("/embed") != 0 && | 90 if (request.relative_url.find("/embed") != 0 && |
| 91 request.relative_url.find("/v") != 0) | 91 request.relative_url.find("/v") != 0) |
| 92 return; | 92 return; |
| 93 | 93 |
| 94 auto type = request.headers.find("Accept"); | 94 auto type = request.headers.find("Accept"); |
| 95 EXPECT_NE(std::string::npos, type->second.find("text/html")) | 95 EXPECT_NE(std::string::npos, type->second.find("text/html")); |
| 96 << "Type is not text/html for test " << test_name_; | |
| 97 | 96 |
| 98 EXPECT_EQ(request.relative_url, expected_url_) | 97 EXPECT_EQ(request.relative_url, expected_url_); |
| 99 << "URL is wrong for test " << test_name_; | |
| 100 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 98 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 101 message_runner_->QuitClosure()); | 99 message_runner_->QuitClosure()); |
| 102 } | 100 } |
| 103 | 101 |
| 104 void WaitForYouTubeRequest() { | 102 void WaitForYouTubeRequest() { |
| 105 message_runner_ = new content::MessageLoopRunner(); | 103 message_runner_ = new content::MessageLoopRunner(); |
| 106 message_runner_->Run(); | 104 message_runner_->Run(); |
| 107 } | 105 } |
| 108 | 106 |
| 109 void set_expected_url(const std::string& expected_url) { | 107 void set_expected_url(std::string given) { expected_url_ = given; } |
| 110 expected_url_ = expected_url; | |
| 111 } | |
| 112 | |
| 113 void set_test_name(const std::string& test_name) { | |
| 114 test_name_ = test_name; | |
| 115 } | |
| 116 | 108 |
| 117 private: | 109 private: |
| 118 std::string test_name_; | |
| 119 std::string expected_url_; | 110 std::string expected_url_; |
| 120 scoped_refptr<content::MessageLoopRunner> message_runner_; | 111 scoped_refptr<content::MessageLoopRunner> message_runner_; |
| 121 }; | 112 }; |
| 122 | 113 |
| 123 // These tests examine Youtube requests that use the Flash API and ensure that | 114 // These tests examine Youtube requests that use the Flash API and ensure that |
| 124 // the requests have been modified to instead use HTML5. The tests also check | 115 // the requests have been modified to instead use HTML5. The tests also check |
| 125 // the MIME type of the request to ensure that it is "text/html". | 116 // the MIME type of the request to ensure that it is "text/html". |
| 126 IN_PROC_BROWSER_TEST_F(ChromeContentRendererClientBrowserTest, | 117 IN_PROC_BROWSER_TEST_F(ChromeContentRendererClientBrowserTest, |
| 127 RewriteYouTubeFlashEmbed) { | 118 RewriteYouTubeFlashEmbed) { |
| 128 struct TestData { | |
| 129 std::string name; | |
| 130 std::string host; | |
| 131 std::string path; | |
| 132 std::string type; | |
| 133 std::string expected_url; | |
| 134 } test_data[] = { | |
| 135 { | |
| 136 "Valid URL, no parameters", | |
| 137 "www.youtube.com", | |
| 138 "/v/deadbeef", | |
| 139 "application/x-shockwave-flash", | |
| 140 "/embed/deadbeef" | |
| 141 }, | |
| 142 { | |
| 143 "Valid URL, no parameters, subdomain", | |
| 144 "www.foo.youtube.com", | |
| 145 "/v/deadbeef", | |
| 146 "application/x-shockwave-flash", | |
| 147 "/embed/deadbeef" | |
| 148 }, | |
| 149 { | |
| 150 "Valid URL, many parameters", | |
| 151 "www.youtube.com", | |
| 152 "/v/deadbeef?start=4&fs=1", | |
| 153 "application/x-shockwave-flash", | |
| 154 "/embed/deadbeef?start=4&fs=1" | |
| 155 }, | |
| 156 { | |
| 157 "Invalid parameter construct, many parameters", | |
| 158 "www.youtube.com", | |
| 159 "/v/deadbeef&bar=4&foo=6", | |
| 160 "application/x-shockwave-flash", | |
| 161 "/embed/deadbeef?bar=4&foo=6" | |
| 162 }, | |
| 163 { | |
| 164 "Valid URL, enablejsapi=1", | |
| 165 "www.youtube.com", | |
| 166 "/v/deadbeef?enablejsapi=1", | |
| 167 "", | |
| 168 "/v/deadbeef?enablejsapi=1" | |
| 169 } | |
| 170 }; | |
| 171 | |
| 172 ASSERT_TRUE(embedded_test_server()->Start()); | 119 ASSERT_TRUE(embedded_test_server()->Start()); |
| 173 | 120 |
| 174 host_resolver()->AddRule("*", "127.0.0.1"); | 121 host_resolver()->AddRule("*", "127.0.0.1"); |
| 175 | 122 |
| 176 embedded_test_server()->ServeFilesFromSourceDirectory( | 123 embedded_test_server()->ServeFilesFromSourceDirectory( |
| 177 base::FilePath(kDocRoot)); | 124 base::FilePath(kDocRoot)); |
| 178 embedded_test_server()->RegisterRequestMonitor( | 125 embedded_test_server()->RegisterRequestMonitor( |
| 179 base::Bind(&ChromeContentRendererClientBrowserTest::MonitorRequestHandler, | 126 base::Bind(&ChromeContentRendererClientBrowserTest::MonitorRequestHandler, |
| 180 base::Unretained(this))); | 127 base::Unretained(this))); |
| 181 | 128 |
| 182 GURL url(embedded_test_server()->GetURL("/flash_embeds.html")); | 129 GURL url(embedded_test_server()->GetURL("/flash_embeds.html")); |
| 183 | 130 |
| 184 ui_test_utils::NavigateToURL(browser(), url); | 131 ui_test_utils::NavigateToURL(browser(), url); |
| 185 | 132 |
| 186 content::WebContents* web_contents = | 133 content::WebContents* web_contents = |
| 187 browser()->tab_strip_model()->GetActiveWebContents(); | 134 browser()->tab_strip_model()->GetActiveWebContents(); |
| 188 std::string port = std::to_string(embedded_test_server()->port()); | 135 std::string port = std::to_string(embedded_test_server()->port()); |
| 189 | 136 |
| 190 for (auto data : test_data) { | 137 // Valid URL, no parameters |
| 191 std::string video_url = "http://" + data.host + ":" + port + data.path; | 138 std::string video_url = "http://www.youtube.com:" + port + "/v/deadbeef"; |
| 192 set_expected_url(data.expected_url); | 139 std::string mime_type = "application/x-shockwave-flash"; |
| 193 set_test_name("<embed src= > " + data.name); | 140 set_expected_url("/embed/deadbeef"); |
| 194 EXPECT_TRUE(ExecuteScript(web_contents, | 141 EXPECT_TRUE(ExecuteScript( |
| 195 "appendEmbedToDOM('" + video_url + "','" + data.type + "');")); | 142 web_contents, "appendToDOM('" + video_url + "','" + mime_type + "');")); |
| 196 WaitForYouTubeRequest(); | 143 WaitForYouTubeRequest(); |
| 197 | 144 |
| 198 set_test_name("<embed data= > " + data.name); | 145 // Valid URL, no parameters, subdomain |
| 199 EXPECT_TRUE(ExecuteScript(web_contents, | 146 video_url = "http://www.foo.youtube.com:" + port + "/v/deadbeef"; |
| 200 "appendDataEmbedToDOM('" + video_url + "','" + data.type + "');")); | 147 set_expected_url("/embed/deadbeef"); |
| 201 WaitForYouTubeRequest(); | 148 EXPECT_TRUE(ExecuteScript( |
| 202 } | 149 web_contents, "appendToDOM('" + video_url + "','" + mime_type + "');")); |
| 150 WaitForYouTubeRequest(); |
| 151 |
| 152 // Valid URL, many parameters |
| 153 video_url = "http://www.youtube.com:" + port + "/v/deadbeef?start=4&fs=1"; |
| 154 set_expected_url("/embed/deadbeef?start=4&fs=1"); |
| 155 EXPECT_TRUE(ExecuteScript( |
| 156 web_contents, "appendToDOM('" + video_url + "','" + mime_type + "');")); |
| 157 WaitForYouTubeRequest(); |
| 158 |
| 159 // Invalid parameter construct, many parameters |
| 160 video_url = "http://www.youtube.com:" + port + "/v/deadbeef&bar=4&foo=6"; |
| 161 set_expected_url("/embed/deadbeef?bar=4&foo=6"); |
| 162 EXPECT_TRUE(ExecuteScript( |
| 163 web_contents, "appendToDOM('" + video_url + "','" + mime_type + "');")); |
| 164 WaitForYouTubeRequest(); |
| 165 |
| 166 // Valid URL, enablejsapi=1 |
| 167 video_url = "http://www.youtube.com:" + port + "/v/deadbeef?enablejsapi=1"; |
| 168 mime_type = ""; |
| 169 set_expected_url("/v/deadbeef?enablejsapi=1"); |
| 170 EXPECT_TRUE(ExecuteScript( |
| 171 web_contents, "appendToDOM('" + video_url + "','" + mime_type + "');")); |
| 172 WaitForYouTubeRequest(); |
| 203 } | 173 } |
| OLD | NEW |