| 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/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 8 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/values.h" | 10 #include "base/values.h" |
| 10 #include "chrome/test/chromedriver/chrome/log.h" | 11 #include "chrome/test/chromedriver/chrome/log.h" |
| 11 #include "chrome/test/chromedriver/chrome/status.h" | 12 #include "chrome/test/chromedriver/chrome/status.h" |
| 12 #include "chrome/test/chromedriver/command_executor.h" | |
| 13 #include "chrome/test/chromedriver/command_names.h" | |
| 14 #include "chrome/test/chromedriver/server/http_handler.h" | 13 #include "chrome/test/chromedriver/server/http_handler.h" |
| 15 #include "chrome/test/chromedriver/server/http_response.h" | 14 #include "chrome/test/chromedriver/server/http_response.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 16 |
| 18 namespace { | 17 namespace { |
| 19 | 18 |
| 20 class DummyExecutor : public CommandExecutor { | 19 Status DummyCommand( |
| 21 public: | 20 Status status, |
| 22 DummyExecutor() : status_(kOk) {} | 21 const base::DictionaryValue& params, |
| 23 explicit DummyExecutor(StatusCode status) : status_(status) {} | 22 const std::string& session_id, |
| 24 virtual ~DummyExecutor() {} | 23 scoped_ptr<base::Value>* value, |
| 25 | 24 std::string* out_session_id) { |
| 26 virtual void Init() OVERRIDE {} | 25 value->reset(new base::FundamentalValue(1)); |
| 27 virtual void ExecuteCommand(const std::string& name, | 26 *out_session_id = "session_id"; |
| 28 const base::DictionaryValue& params, | 27 return status; |
| 29 const std::string& session_id, | 28 } |
| 30 StatusCode* status, | |
| 31 scoped_ptr<base::Value>* value, | |
| 32 std::string* out_session_id) OVERRIDE { | |
| 33 *status = status_; | |
| 34 value->reset(new base::FundamentalValue(1)); | |
| 35 *out_session_id = "session_id"; | |
| 36 } | |
| 37 | |
| 38 private: | |
| 39 StatusCode status_; | |
| 40 }; | |
| 41 | 29 |
| 42 } // namespace | 30 } // namespace |
| 43 | 31 |
| 44 TEST(HttpHandlerTest, HandleOutsideOfBaseUrl) { | 32 TEST(HttpHandlerTest, HandleOutsideOfBaseUrl) { |
| 45 Logger log; | 33 Logger log; |
| 46 HttpHandler handler( | 34 HttpHandler handler(&log, "base/url/"); |
| 47 &log, | |
| 48 scoped_ptr<CommandExecutor>(new DummyExecutor()), | |
| 49 scoped_ptr<HttpHandler::CommandMap>(new HttpHandler::CommandMap()), | |
| 50 "base/url/"); | |
| 51 HttpRequest request(kGet, "base/path", "body"); | 35 HttpRequest request(kGet, "base/path", "body"); |
| 52 HttpResponse response; | 36 HttpResponse response; |
| 53 handler.Handle(request, &response); | 37 handler.Handle(request, &response); |
| 54 ASSERT_EQ(HttpResponse::kBadRequest, response.status()); | 38 ASSERT_EQ(HttpResponse::kBadRequest, response.status()); |
| 55 } | 39 } |
| 56 | 40 |
| 57 TEST(HttpHandlerTest, HandleUnknownCommand) { | 41 TEST(HttpHandlerTest, HandleUnknownCommand) { |
| 58 Logger log; | 42 Logger log; |
| 59 HttpHandler handler( | 43 HttpHandler handler(&log, "/"); |
| 60 &log, | 44 handler.command_map_.reset(new HttpHandler::CommandMap()); |
| 61 scoped_ptr<CommandExecutor>(new DummyExecutor()), | |
| 62 scoped_ptr<HttpHandler::CommandMap>(new HttpHandler::CommandMap()), | |
| 63 "/"); | |
| 64 HttpRequest request(kGet, "/path", std::string()); | 45 HttpRequest request(kGet, "/path", std::string()); |
| 65 HttpResponse response; | 46 HttpResponse response; |
| 66 handler.Handle(request, &response); | 47 handler.Handle(request, &response); |
| 67 ASSERT_EQ(HttpResponse::kNotFound, response.status()); | 48 ASSERT_EQ(HttpResponse::kNotFound, response.status()); |
| 68 } | 49 } |
| 69 | 50 |
| 70 TEST(HttpHandlerTest, HandleNewSession) { | 51 TEST(HttpHandlerTest, HandleNewSession) { |
| 71 scoped_ptr<HttpHandler::CommandMap> map(new HttpHandler::CommandMap()); | |
| 72 map->push_back(CommandMapping(kPost, "session", CommandNames::kNewSession)); | |
| 73 Logger log; | 52 Logger log; |
| 74 HttpHandler handler( | 53 HttpHandler handler(&log, "/base/"); |
| 75 &log, | 54 handler.command_map_.reset(new HttpHandler::CommandMap()); |
| 76 scoped_ptr<CommandExecutor>(new DummyExecutor()), | 55 handler.command_map_->push_back( |
| 77 map.Pass(), "/base/"); | 56 CommandMapping(kPost, internal::kNewSessionPathPattern, |
| 57 base::Bind(&DummyCommand, Status(kOk)))); |
| 78 HttpRequest request(kPost, "/base/session", std::string()); | 58 HttpRequest request(kPost, "/base/session", std::string()); |
| 79 HttpResponse response; | 59 HttpResponse response; |
| 80 handler.Handle(request, &response); | 60 handler.Handle(request, &response); |
| 81 ASSERT_EQ(HttpResponse::kSeeOther, response.status()); | 61 ASSERT_EQ(HttpResponse::kSeeOther, response.status()); |
| 82 std::string location; | 62 std::string location; |
| 83 ASSERT_TRUE(response.GetHeader("Location", &location)); | 63 ASSERT_TRUE(response.GetHeader("Location", &location)); |
| 84 std::string prefix = "/base/session/"; | 64 std::string prefix = "/base/session/"; |
| 85 ASSERT_EQ(prefix, location.substr(0, prefix.length())); | 65 ASSERT_EQ(prefix, location.substr(0, prefix.length())); |
| 86 } | 66 } |
| 87 | 67 |
| 88 TEST(HttpHandlerTest, HandleInvalidPost) { | 68 TEST(HttpHandlerTest, HandleInvalidPost) { |
| 89 scoped_ptr<HttpHandler::CommandMap> map(new HttpHandler::CommandMap()); | |
| 90 map->push_back(CommandMapping(kPost, "path", "cmd")); | |
| 91 Logger log; | 69 Logger log; |
| 92 HttpHandler handler( | 70 HttpHandler handler(&log, "/"); |
| 93 &log, | 71 handler.command_map_.reset(new HttpHandler::CommandMap()); |
| 94 scoped_ptr<CommandExecutor>(new DummyExecutor()), | 72 handler.command_map_->push_back( |
| 95 map.Pass(), "/"); | 73 CommandMapping(kPost, "path", base::Bind(&DummyCommand, Status(kOk)))); |
| 96 HttpRequest request(kPost, "/path", "should be a dictionary"); | 74 HttpRequest request(kPost, "/path", "should be a dictionary"); |
| 97 HttpResponse response; | 75 HttpResponse response; |
| 98 handler.Handle(request, &response); | 76 handler.Handle(request, &response); |
| 99 ASSERT_EQ(HttpResponse::kBadRequest, response.status()); | 77 ASSERT_EQ(HttpResponse::kBadRequest, response.status()); |
| 100 } | 78 } |
| 101 | 79 |
| 102 TEST(HttpHandlerTest, HandleUnimplementedCommand) { | 80 TEST(HttpHandlerTest, HandleUnimplementedCommand) { |
| 103 scoped_ptr<HttpHandler::CommandMap> map(new HttpHandler::CommandMap()); | |
| 104 map->push_back(CommandMapping(kPost, "path", "cmd")); | |
| 105 Logger log; | 81 Logger log; |
| 106 HttpHandler handler( | 82 HttpHandler handler(&log, "/"); |
| 107 &log, | 83 handler.command_map_.reset(new HttpHandler::CommandMap()); |
| 108 scoped_ptr<CommandExecutor>(new DummyExecutor(kUnknownCommand)), | 84 handler.command_map_->push_back( |
| 109 map.Pass(), "/"); | 85 CommandMapping(kPost, "path", |
| 86 base::Bind(&DummyCommand, Status(kUnknownCommand)))); |
| 110 HttpRequest request(kPost, "/path", std::string()); | 87 HttpRequest request(kPost, "/path", std::string()); |
| 111 HttpResponse response; | 88 HttpResponse response; |
| 112 handler.Handle(request, &response); | 89 handler.Handle(request, &response); |
| 113 ASSERT_EQ(HttpResponse::kNotImplemented, response.status()); | 90 ASSERT_EQ(HttpResponse::kNotImplemented, response.status()); |
| 114 } | 91 } |
| 115 | 92 |
| 116 TEST(HttpHandlerTest, HandleCommand) { | 93 TEST(HttpHandlerTest, HandleCommand) { |
| 117 scoped_ptr<HttpHandler::CommandMap> map(new HttpHandler::CommandMap()); | |
| 118 map->push_back(CommandMapping(kPost, "path", "cmd")); | |
| 119 Logger log; | 94 Logger log; |
| 120 HttpHandler handler( | 95 HttpHandler handler(&log, "/"); |
| 121 &log, | 96 handler.command_map_.reset(new HttpHandler::CommandMap()); |
| 122 scoped_ptr<CommandExecutor>(new DummyExecutor()), | 97 handler.command_map_->push_back( |
| 123 map.Pass(), "/"); | 98 CommandMapping(kPost, "path", base::Bind(&DummyCommand, Status(kOk)))); |
| 124 HttpRequest request(kPost, "/path", std::string()); | 99 HttpRequest request(kPost, "/path", std::string()); |
| 125 HttpResponse response; | 100 HttpResponse response; |
| 126 handler.Handle(request, &response); | 101 handler.Handle(request, &response); |
| 127 ASSERT_EQ(HttpResponse::kOk, response.status()); | 102 ASSERT_EQ(HttpResponse::kOk, response.status()); |
| 128 std::string mime; | 103 std::string mime; |
| 129 ASSERT_TRUE(response.GetHeader("Content-Type", &mime)); | 104 ASSERT_TRUE(response.GetHeader("Content-Type", &mime)); |
| 130 base::DictionaryValue body; | 105 base::DictionaryValue body; |
| 131 body.SetInteger("status", kOk); | 106 body.SetInteger("status", kOk); |
| 132 body.SetInteger("value", 1); | 107 body.SetInteger("value", 1); |
| 133 body.SetString("sessionId", "session_id"); | 108 body.SetString("sessionId", "session_id"); |
| 134 std::string json; | 109 std::string json; |
| 135 base::JSONWriter::Write(&body, &json); | 110 base::JSONWriter::Write(&body, &json); |
| 136 ASSERT_STREQ(json.c_str(), response.body().c_str()); | 111 ASSERT_STREQ(json.c_str(), response.body().c_str()); |
| 137 } | 112 } |
| 138 | 113 |
| 139 TEST(MatchesCommandTest, DiffMethod) { | 114 TEST(MatchesCommandTest, DiffMethod) { |
| 140 CommandMapping command(kPost, "path", "command"); | 115 CommandMapping command(kPost, "path", base::Bind(&DummyCommand, Status(kOk))); |
| 141 std::string session_id; | 116 std::string session_id; |
| 142 base::DictionaryValue params; | 117 base::DictionaryValue params; |
| 143 ASSERT_FALSE(internal::MatchesCommand( | 118 ASSERT_FALSE(internal::MatchesCommand( |
| 144 kGet, "path", command, &session_id, ¶ms)); | 119 kGet, "path", command, &session_id, ¶ms)); |
| 145 ASSERT_STREQ("", session_id.c_str()); | 120 ASSERT_STREQ("", session_id.c_str()); |
| 146 ASSERT_EQ(0u, params.size()); | 121 ASSERT_EQ(0u, params.size()); |
| 147 } | 122 } |
| 148 | 123 |
| 149 TEST(MatchesCommandTest, DiffPathLength) { | 124 TEST(MatchesCommandTest, DiffPathLength) { |
| 150 CommandMapping command(kPost, "path/path", "command"); | 125 CommandMapping command(kPost, "path/path", |
| 126 base::Bind(&DummyCommand, Status(kOk))); |
| 151 std::string session_id; | 127 std::string session_id; |
| 152 base::DictionaryValue params; | 128 base::DictionaryValue params; |
| 153 ASSERT_FALSE(internal::MatchesCommand( | 129 ASSERT_FALSE(internal::MatchesCommand( |
| 154 kPost, "path", command, &session_id, ¶ms)); | 130 kPost, "path", command, &session_id, ¶ms)); |
| 155 ASSERT_FALSE(internal::MatchesCommand( | 131 ASSERT_FALSE(internal::MatchesCommand( |
| 156 kPost, std::string(), command, &session_id, ¶ms)); | 132 kPost, std::string(), command, &session_id, ¶ms)); |
| 157 ASSERT_FALSE( | 133 ASSERT_FALSE( |
| 158 internal::MatchesCommand(kPost, "/", command, &session_id, ¶ms)); | 134 internal::MatchesCommand(kPost, "/", command, &session_id, ¶ms)); |
| 159 ASSERT_FALSE(internal::MatchesCommand( | 135 ASSERT_FALSE(internal::MatchesCommand( |
| 160 kPost, "path/path/path", command, &session_id, ¶ms)); | 136 kPost, "path/path/path", command, &session_id, ¶ms)); |
| 161 } | 137 } |
| 162 | 138 |
| 163 TEST(MatchesCommandTest, DiffPaths) { | 139 TEST(MatchesCommandTest, DiffPaths) { |
| 164 CommandMapping command(kPost, "path/apath", "command"); | 140 CommandMapping command(kPost, "path/apath", |
| 141 base::Bind(&DummyCommand, Status(kOk))); |
| 165 std::string session_id; | 142 std::string session_id; |
| 166 base::DictionaryValue params; | 143 base::DictionaryValue params; |
| 167 ASSERT_FALSE(internal::MatchesCommand( | 144 ASSERT_FALSE(internal::MatchesCommand( |
| 168 kPost, "path/bpath", command, &session_id, ¶ms)); | 145 kPost, "path/bpath", command, &session_id, ¶ms)); |
| 169 } | 146 } |
| 170 | 147 |
| 171 TEST(MatchesCommandTest, Substitution) { | 148 TEST(MatchesCommandTest, Substitution) { |
| 172 CommandMapping command(kPost, "path/:sessionId/space/:a/:b", "command"); | 149 CommandMapping command(kPost, "path/:sessionId/space/:a/:b", |
| 150 base::Bind(&DummyCommand, Status(kOk))); |
| 173 std::string session_id; | 151 std::string session_id; |
| 174 base::DictionaryValue params; | 152 base::DictionaryValue params; |
| 175 ASSERT_TRUE(internal::MatchesCommand( | 153 ASSERT_TRUE(internal::MatchesCommand( |
| 176 kPost, "path/1/space/2/3", command, &session_id, ¶ms)); | 154 kPost, "path/1/space/2/3", command, &session_id, ¶ms)); |
| 177 ASSERT_EQ("1", session_id); | 155 ASSERT_EQ("1", session_id); |
| 178 ASSERT_EQ(2u, params.size()); | 156 ASSERT_EQ(2u, params.size()); |
| 179 std::string param; | 157 std::string param; |
| 180 ASSERT_TRUE(params.GetString("a", ¶m)); | 158 ASSERT_TRUE(params.GetString("a", ¶m)); |
| 181 ASSERT_EQ("2", param); | 159 ASSERT_EQ("2", param); |
| 182 ASSERT_TRUE(params.GetString("b", ¶m)); | 160 ASSERT_TRUE(params.GetString("b", ¶m)); |
| 183 ASSERT_EQ("3", param); | 161 ASSERT_EQ("3", param); |
| 184 } | 162 } |
| OLD | NEW |