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 "chrome/browser/extensions/extension_apitest.h" | 5 #include "chrome/browser/extensions/extension_apitest.h" |
6 | 6 |
7 #include "base/strings/string_split.h" | |
7 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
8 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
9 #include "chrome/browser/extensions/api/test/test_api.h" | 10 #include "chrome/browser/extensions/api/test/test_api.h" |
10 #include "chrome/browser/extensions/extension_service.h" | 11 #include "chrome/browser/extensions/extension_service.h" |
11 #include "chrome/browser/extensions/extension_system.h" | 12 #include "chrome/browser/extensions/extension_system.h" |
12 #include "chrome/browser/extensions/unpacked_installer.h" | 13 #include "chrome/browser/extensions/unpacked_installer.h" |
13 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
15 #include "chrome/browser/ui/extensions/application_launch.h" | 16 #include "chrome/browser/ui/extensions/application_launch.h" |
16 #include "chrome/common/chrome_notification_types.h" | 17 #include "chrome/common/chrome_notification_types.h" |
17 #include "chrome/common/extensions/extension.h" | 18 #include "chrome/common/extensions/extension.h" |
18 #include "chrome/test/base/ui_test_utils.h" | 19 #include "chrome/test/base/ui_test_utils.h" |
19 #include "content/public/browser/notification_registrar.h" | 20 #include "content/public/browser/notification_registrar.h" |
20 #include "content/public/browser/notification_service.h" | 21 #include "content/public/browser/notification_service.h" |
22 #include "net/base/escape.h" | |
21 #include "net/base/net_util.h" | 23 #include "net/base/net_util.h" |
24 #include "net/test/embedded_test_server/embedded_test_server.h" | |
25 #include "net/test/embedded_test_server/http_response.h" | |
26 #include "net/test/embedded_test_server/http_request.h" | |
22 #include "net/test/spawned_test_server/spawned_test_server.h" | 27 #include "net/test/spawned_test_server/spawned_test_server.h" |
23 | 28 |
24 namespace { | 29 namespace { |
25 | 30 |
26 const char kTestCustomArg[] = "customArg"; | 31 const char kTestCustomArg[] = "customArg"; |
27 const char kTestServerPort[] = "testServer.port"; | 32 const char kTestServerPort[] = "testServer.port"; |
28 const char kTestDataDirectory[] = "testDataDirectory"; | 33 const char kTestDataDirectory[] = "testDataDirectory"; |
29 const char kTestWebSocketPort[] = "testWebSocketPort"; | 34 const char kTestWebSocketPort[] = "testWebSocketPort"; |
30 | 35 |
36 scoped_ptr<net::test_server::HttpResponse> HandleServerRedirectRequest( | |
37 const net::test_server::HttpRequest& request) { | |
38 if (!StartsWithASCII(request.relative_url, "/server-redirect?", true)) | |
39 return scoped_ptr<net::test_server::HttpResponse>(NULL); | |
40 | |
41 size_t query_string_pos = request.relative_url.find('?'); | |
42 std::string redirect_target = | |
43 request.relative_url.substr(query_string_pos + 1); | |
44 | |
45 scoped_ptr<net::test_server::BasicHttpResponse> http_response( | |
46 new net::test_server::BasicHttpResponse); | |
47 http_response->set_code(net::HTTP_MOVED_PERMANENTLY); | |
48 http_response->AddCustomHeader("Location", redirect_target); | |
49 return http_response.PassAs<net::test_server::HttpResponse>(); | |
50 } | |
51 | |
52 scoped_ptr<net::test_server::HttpResponse> HandleEchoHeaderRequest( | |
53 const net::test_server::HttpRequest& request) { | |
54 if (!StartsWithASCII(request.relative_url, "/echoheader?", true)) | |
55 return scoped_ptr<net::test_server::HttpResponse>(NULL); | |
56 | |
57 size_t query_string_pos = request.relative_url.find('?'); | |
58 std::string header_name = | |
59 request.relative_url.substr(query_string_pos + 1); | |
60 | |
61 std::string header_value; | |
62 std::map<std::string, std::string>::const_iterator it = request.headers.find( | |
63 header_name); | |
64 if (it != request.headers.end()) | |
65 header_value = it->second; | |
66 | |
67 scoped_ptr<net::test_server::BasicHttpResponse> http_response( | |
68 new net::test_server::BasicHttpResponse); | |
69 http_response->set_code(net::HTTP_OK); | |
70 http_response->set_content(header_value); | |
71 return http_response.PassAs<net::test_server::HttpResponse>(); | |
72 } | |
73 | |
74 scoped_ptr<net::test_server::HttpResponse> HandleSetCookieRequest( | |
75 const net::test_server::HttpRequest& request) { | |
76 if (!StartsWithASCII(request.relative_url, "/set-cookie?", true)) | |
77 return scoped_ptr<net::test_server::HttpResponse>(NULL); | |
78 | |
79 scoped_ptr<net::test_server::BasicHttpResponse> http_response( | |
80 new net::test_server::BasicHttpResponse); | |
81 http_response->set_code(net::HTTP_OK); | |
82 | |
83 size_t query_string_pos = request.relative_url.find('?'); | |
84 std::string cookie_value = | |
85 request.relative_url.substr(query_string_pos + 1); | |
86 | |
87 std::vector<std::string> cookies; | |
88 base::SplitString(cookie_value, '&', &cookies); | |
89 | |
90 for (size_t i = 0; i < cookies.size(); i++) | |
91 http_response->AddCustomHeader("Set-Cookie", cookies[i]); | |
92 | |
93 return http_response.PassAs<net::test_server::HttpResponse>(); | |
94 } | |
95 | |
96 scoped_ptr<net::test_server::HttpResponse> HandleSetHeaderRequest( | |
97 const net::test_server::HttpRequest& request) { | |
98 if (!StartsWithASCII(request.relative_url, "/set-header?", true)) | |
99 return scoped_ptr<net::test_server::HttpResponse>(NULL); | |
100 | |
101 size_t query_string_pos = request.relative_url.find('?'); | |
102 std::string escaped_header = | |
103 request.relative_url.substr(query_string_pos + 1); | |
104 | |
105 std::string header = | |
106 net::UnescapeURLComponent(escaped_header, | |
107 net::UnescapeRule::NORMAL | | |
108 net::UnescapeRule::SPACES | | |
109 net::UnescapeRule::URL_SPECIAL_CHARS); | |
110 | |
111 size_t colon_pos = header.find(':'); | |
112 if (colon_pos == std::string::npos) | |
113 return scoped_ptr<net::test_server::HttpResponse>(NULL); | |
114 | |
115 std::string header_name = header.substr(0, colon_pos); | |
116 // Skip space after colon. | |
117 std::string header_value = header.substr(colon_pos + 2); | |
118 | |
119 scoped_ptr<net::test_server::BasicHttpResponse> http_response( | |
120 new net::test_server::BasicHttpResponse); | |
121 http_response->set_code(net::HTTP_OK); | |
122 http_response->AddCustomHeader(header_name, header_value); | |
123 return http_response.PassAs<net::test_server::HttpResponse>(); | |
124 } | |
125 | |
asargent_no_longer_on_chrome
2013/06/21 23:08:14
observation: This whole block of new functions see
Paweł Hajdan Jr.
2013/06/22 00:16:02
I was thinking about it. For now I prefer it to be
| |
31 }; // namespace | 126 }; // namespace |
32 | 127 |
33 ExtensionApiTest::ExtensionApiTest() {} | 128 ExtensionApiTest::ExtensionApiTest() { |
129 embedded_test_server()->RegisterRequestHandler( | |
130 base::Bind(&HandleServerRedirectRequest)); | |
131 embedded_test_server()->RegisterRequestHandler( | |
132 base::Bind(&HandleEchoHeaderRequest)); | |
133 embedded_test_server()->RegisterRequestHandler( | |
134 base::Bind(&HandleSetCookieRequest)); | |
135 embedded_test_server()->RegisterRequestHandler( | |
136 base::Bind(&HandleSetHeaderRequest)); | |
137 } | |
34 | 138 |
35 ExtensionApiTest::~ExtensionApiTest() {} | 139 ExtensionApiTest::~ExtensionApiTest() {} |
36 | 140 |
37 ExtensionApiTest::ResultCatcher::ResultCatcher() | 141 ExtensionApiTest::ResultCatcher::ResultCatcher() |
38 : profile_restriction_(NULL), | 142 : profile_restriction_(NULL), |
39 waiting_(false) { | 143 waiting_(false) { |
40 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_TEST_PASSED, | 144 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_TEST_PASSED, |
41 content::NotificationService::AllSources()); | 145 content::NotificationService::AllSources()); |
42 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_TEST_FAILED, | 146 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_TEST_FAILED, |
43 content::NotificationService::AllSources()); | 147 content::NotificationService::AllSources()); |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
299 } | 403 } |
300 | 404 |
301 if (!extension) { | 405 if (!extension) { |
302 message_ = "extension pointer is NULL."; | 406 message_ = "extension pointer is NULL."; |
303 return NULL; | 407 return NULL; |
304 } | 408 } |
305 return extension; | 409 return extension; |
306 } | 410 } |
307 | 411 |
308 bool ExtensionApiTest::StartTestServer() { | 412 bool ExtensionApiTest::StartTestServer() { |
309 if (!test_server()->Start()) | 413 if (!embedded_test_server()->InitializeAndWaitUntilReady()) |
asargent_no_longer_on_chrome
2013/06/21 20:02:11
It seems kind of strange to have a method called S
Paweł Hajdan Jr.
2013/06/21 20:37:31
There are two test servers here: test_server() - o
asargent_no_longer_on_chrome
2013/06/21 23:08:14
Ok, I see. Can we rename one of the two then? I do
Paweł Hajdan Jr.
2013/06/22 00:16:02
Done.
| |
310 return false; | 414 return false; |
311 | 415 |
312 // Build a dictionary of values that tests can use to build URLs that | 416 // Build a dictionary of values that tests can use to build URLs that |
313 // access the test server and local file system. Tests can see these values | 417 // access the test server and local file system. Tests can see these values |
314 // using the extension API function chrome.test.getConfig(). | 418 // using the extension API function chrome.test.getConfig(). |
315 test_config_->SetInteger(kTestServerPort, | 419 test_config_->SetInteger(kTestServerPort, |
316 test_server()->host_port_pair().port()); | 420 embedded_test_server()->port()); |
317 | 421 |
318 return true; | 422 return true; |
319 } | 423 } |
320 | 424 |
321 bool ExtensionApiTest::StartWebSocketServer( | 425 bool ExtensionApiTest::StartWebSocketServer( |
322 const base::FilePath& root_directory) { | 426 const base::FilePath& root_directory) { |
323 websocket_server_.reset(new net::SpawnedTestServer( | 427 websocket_server_.reset(new net::SpawnedTestServer( |
324 net::SpawnedTestServer::TYPE_WS, | 428 net::SpawnedTestServer::TYPE_WS, |
325 net::SpawnedTestServer::kLocalhost, | 429 net::SpawnedTestServer::kLocalhost, |
326 root_directory)); | 430 root_directory)); |
327 | 431 |
328 if (!websocket_server_->Start()) | 432 if (!websocket_server_->Start()) |
329 return false; | 433 return false; |
330 | 434 |
331 test_config_->SetInteger(kTestWebSocketPort, | 435 test_config_->SetInteger(kTestWebSocketPort, |
332 websocket_server_->host_port_pair().port()); | 436 websocket_server_->host_port_pair().port()); |
333 | 437 |
334 return true; | 438 return true; |
335 } | 439 } |
336 | 440 |
337 void ExtensionApiTest::SetUpCommandLine(CommandLine* command_line) { | 441 void ExtensionApiTest::SetUpCommandLine(CommandLine* command_line) { |
338 ExtensionBrowserTest::SetUpCommandLine(command_line); | 442 ExtensionBrowserTest::SetUpCommandLine(command_line); |
339 test_data_dir_ = test_data_dir_.AppendASCII("api_test"); | 443 test_data_dir_ = test_data_dir_.AppendASCII("api_test"); |
340 } | 444 } |
OLD | NEW |