| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/test/chromedriver/chrome/log.h" | 11 #include "chrome/test/chromedriver/chrome/log.h" |
| 12 #include "chrome/test/chromedriver/chrome/status.h" | 12 #include "chrome/test/chromedriver/chrome/status.h" |
| 13 #include "chrome/test/chromedriver/command.h" |
| 13 #include "chrome/test/chromedriver/server/http_handler.h" | 14 #include "chrome/test/chromedriver/server/http_handler.h" |
| 14 #include "chrome/test/chromedriver/server/http_response.h" | 15 #include "chrome/test/chromedriver/server/http_response.h" |
| 15 #include "net/server/http_server_request_info.h" | 16 #include "net/server/http_server_request_info.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 Status DummyCommand( | 21 void DummyCommand( |
| 21 Status status, | 22 const Status& status, |
| 22 const base::DictionaryValue& params, | 23 const base::DictionaryValue& params, |
| 23 const std::string& session_id, | 24 const std::string& session_id, |
| 24 scoped_ptr<base::Value>* value, | 25 const CommandCallback& callback) { |
| 25 std::string* out_session_id) { | 26 callback.Run(status, |
| 26 value->reset(new base::FundamentalValue(1)); | 27 scoped_ptr<base::Value>(new base::FundamentalValue(1)), |
| 27 *out_session_id = "session_id"; | 28 "session_id"); |
| 28 return status; | 29 } |
| 30 |
| 31 void OnResponse(HttpResponse* response_to_set, |
| 32 scoped_ptr<HttpResponse> response) { |
| 33 *response_to_set = *response; |
| 29 } | 34 } |
| 30 | 35 |
| 31 } // namespace | 36 } // namespace |
| 32 | 37 |
| 33 TEST(HttpHandlerTest, HandleOutsideOfBaseUrl) { | 38 TEST(HttpHandlerTest, HandleOutsideOfBaseUrl) { |
| 34 Logger log; | 39 Logger log; |
| 35 HttpHandler handler(&log, "base/url/"); | 40 HttpHandler handler(&log, "base/url/"); |
| 36 net::HttpServerRequestInfo request; | 41 net::HttpServerRequestInfo request; |
| 37 request.method = "get"; | 42 request.method = "get"; |
| 38 request.path = "base/path"; | 43 request.path = "base/path"; |
| 39 request.data = "body"; | 44 request.data = "body"; |
| 40 HttpResponse response; | 45 HttpResponse response; |
| 41 handler.Handle(request, &response); | 46 handler.Handle(request, base::Bind(&OnResponse, &response)); |
| 42 ASSERT_EQ(HttpResponse::kBadRequest, response.status()); | 47 ASSERT_EQ(HttpResponse::kBadRequest, response.status()); |
| 43 } | 48 } |
| 44 | 49 |
| 45 TEST(HttpHandlerTest, HandleUnknownCommand) { | 50 TEST(HttpHandlerTest, HandleUnknownCommand) { |
| 46 Logger log; | 51 Logger log; |
| 47 HttpHandler handler(&log, "/"); | 52 HttpHandler handler(&log, "/"); |
| 48 net::HttpServerRequestInfo request; | 53 net::HttpServerRequestInfo request; |
| 49 request.method = "get"; | 54 request.method = "get"; |
| 50 request.path = "/path"; | 55 request.path = "/path"; |
| 51 HttpResponse response; | 56 HttpResponse response; |
| 52 handler.Handle(request, &response); | 57 handler.Handle(request, base::Bind(&OnResponse, &response)); |
| 53 ASSERT_EQ(HttpResponse::kNotFound, response.status()); | 58 ASSERT_EQ(HttpResponse::kNotFound, response.status()); |
| 54 } | 59 } |
| 55 | 60 |
| 56 TEST(HttpHandlerTest, HandleNewSession) { | 61 TEST(HttpHandlerTest, HandleNewSession) { |
| 57 Logger log; | 62 Logger log; |
| 58 HttpHandler handler(&log, "/base/"); | 63 HttpHandler handler(&log, "/base/"); |
| 59 handler.command_map_.reset(new HttpHandler::CommandMap()); | 64 handler.command_map_.reset(new HttpHandler::CommandMap()); |
| 60 handler.command_map_->push_back( | 65 handler.command_map_->push_back( |
| 61 CommandMapping(kPost, internal::kNewSessionPathPattern, | 66 CommandMapping(kPost, internal::kNewSessionPathPattern, |
| 62 base::Bind(&DummyCommand, Status(kOk)))); | 67 base::Bind(&DummyCommand, Status(kOk)))); |
| 63 net::HttpServerRequestInfo request; | 68 net::HttpServerRequestInfo request; |
| 64 request.method = "post"; | 69 request.method = "post"; |
| 65 request.path = "/base/session"; | 70 request.path = "/base/session"; |
| 66 HttpResponse response; | 71 HttpResponse response; |
| 67 handler.Handle(request, &response); | 72 handler.Handle(request, base::Bind(&OnResponse, &response)); |
| 68 ASSERT_EQ(HttpResponse::kSeeOther, response.status()); | 73 ASSERT_EQ(HttpResponse::kSeeOther, response.status()); |
| 69 std::string location; | 74 std::string location; |
| 70 ASSERT_TRUE(response.GetHeader("Location", &location)); | 75 ASSERT_TRUE(response.GetHeader("Location", &location)); |
| 71 std::string prefix = "/base/session/"; | 76 std::string prefix = "/base/session/"; |
| 72 ASSERT_EQ(prefix, location.substr(0, prefix.length())); | 77 ASSERT_EQ(prefix, location.substr(0, prefix.length())); |
| 73 } | 78 } |
| 74 | 79 |
| 75 TEST(HttpHandlerTest, HandleInvalidPost) { | 80 TEST(HttpHandlerTest, HandleInvalidPost) { |
| 76 Logger log; | 81 Logger log; |
| 77 HttpHandler handler(&log, "/"); | 82 HttpHandler handler(&log, "/"); |
| 78 handler.command_map_->push_back( | 83 handler.command_map_->push_back( |
| 79 CommandMapping(kPost, "path", base::Bind(&DummyCommand, Status(kOk)))); | 84 CommandMapping(kPost, "path", base::Bind(&DummyCommand, Status(kOk)))); |
| 80 net::HttpServerRequestInfo request; | 85 net::HttpServerRequestInfo request; |
| 81 request.method = "post"; | 86 request.method = "post"; |
| 82 request.path = "/path"; | 87 request.path = "/path"; |
| 83 request.data = "should be a dictionary"; | 88 request.data = "should be a dictionary"; |
| 84 HttpResponse response; | 89 HttpResponse response; |
| 85 handler.Handle(request, &response); | 90 handler.Handle(request, base::Bind(&OnResponse, &response)); |
| 86 ASSERT_EQ(HttpResponse::kBadRequest, response.status()); | 91 ASSERT_EQ(HttpResponse::kBadRequest, response.status()); |
| 87 } | 92 } |
| 88 | 93 |
| 89 TEST(HttpHandlerTest, HandleUnimplementedCommand) { | 94 TEST(HttpHandlerTest, HandleUnimplementedCommand) { |
| 90 Logger log; | 95 Logger log; |
| 91 HttpHandler handler(&log, "/"); | 96 HttpHandler handler(&log, "/"); |
| 92 handler.command_map_->push_back( | 97 handler.command_map_->push_back( |
| 93 CommandMapping(kPost, "path", | 98 CommandMapping(kPost, "path", |
| 94 base::Bind(&DummyCommand, Status(kUnknownCommand)))); | 99 base::Bind(&DummyCommand, Status(kUnknownCommand)))); |
| 95 net::HttpServerRequestInfo request; | 100 net::HttpServerRequestInfo request; |
| 96 request.method = "post"; | 101 request.method = "post"; |
| 97 request.path = "/path"; | 102 request.path = "/path"; |
| 98 HttpResponse response; | 103 HttpResponse response; |
| 99 handler.Handle(request, &response); | 104 handler.Handle(request, base::Bind(&OnResponse, &response)); |
| 100 ASSERT_EQ(HttpResponse::kNotImplemented, response.status()); | 105 ASSERT_EQ(HttpResponse::kNotImplemented, response.status()); |
| 101 } | 106 } |
| 102 | 107 |
| 103 TEST(HttpHandlerTest, HandleCommand) { | 108 TEST(HttpHandlerTest, HandleCommand) { |
| 104 Logger log; | 109 Logger log; |
| 105 HttpHandler handler(&log, "/"); | 110 HttpHandler handler(&log, "/"); |
| 106 handler.command_map_->push_back( | 111 handler.command_map_->push_back( |
| 107 CommandMapping(kPost, "path", base::Bind(&DummyCommand, Status(kOk)))); | 112 CommandMapping(kPost, "path", base::Bind(&DummyCommand, Status(kOk)))); |
| 108 net::HttpServerRequestInfo request; | 113 net::HttpServerRequestInfo request; |
| 109 request.method = "post"; | 114 request.method = "post"; |
| 110 request.path = "/path"; | 115 request.path = "/path"; |
| 111 HttpResponse response; | 116 HttpResponse response; |
| 112 handler.Handle(request, &response); | 117 handler.Handle(request, base::Bind(&OnResponse, &response)); |
| 113 ASSERT_EQ(HttpResponse::kOk, response.status()); | 118 ASSERT_EQ(HttpResponse::kOk, response.status()); |
| 114 std::string mime; | 119 std::string mime; |
| 115 ASSERT_TRUE(response.GetHeader("Content-Type", &mime)); | 120 ASSERT_TRUE(response.GetHeader("Content-Type", &mime)); |
| 116 base::DictionaryValue body; | 121 base::DictionaryValue body; |
| 117 body.SetInteger("status", kOk); | 122 body.SetInteger("status", kOk); |
| 118 body.SetInteger("value", 1); | 123 body.SetInteger("value", 1); |
| 119 body.SetString("sessionId", "session_id"); | 124 body.SetString("sessionId", "session_id"); |
| 120 std::string json; | 125 std::string json; |
| 121 base::JSONWriter::Write(&body, &json); | 126 base::JSONWriter::Write(&body, &json); |
| 122 ASSERT_STREQ(json.c_str(), response.body().c_str()); | 127 ASSERT_STREQ(json.c_str(), response.body().c_str()); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 ASSERT_TRUE(internal::MatchesCommand( | 169 ASSERT_TRUE(internal::MatchesCommand( |
| 165 "post", "path/1/space/2/3", command, &session_id, ¶ms)); | 170 "post", "path/1/space/2/3", command, &session_id, ¶ms)); |
| 166 ASSERT_EQ("1", session_id); | 171 ASSERT_EQ("1", session_id); |
| 167 ASSERT_EQ(2u, params.size()); | 172 ASSERT_EQ(2u, params.size()); |
| 168 std::string param; | 173 std::string param; |
| 169 ASSERT_TRUE(params.GetString("a", ¶m)); | 174 ASSERT_TRUE(params.GetString("a", ¶m)); |
| 170 ASSERT_EQ("2", param); | 175 ASSERT_EQ("2", param); |
| 171 ASSERT_TRUE(params.GetString("b", ¶m)); | 176 ASSERT_TRUE(params.GetString("b", ¶m)); |
| 172 ASSERT_EQ("3", param); | 177 ASSERT_EQ("3", param); |
| 173 } | 178 } |
| OLD | NEW |