| 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, | |
| 61 scoped_ptr<CommandExecutor>(new DummyExecutor()), | |
| 62 scoped_ptr<HttpHandler::CommandMap>(new HttpHandler::CommandMap()), | |
| 63 "/"); | |
| 64 HttpRequest request(kGet, "/path", std::string()); | 44 HttpRequest request(kGet, "/path", std::string()); |
| 65 HttpResponse response; | 45 HttpResponse response; |
| 66 handler.Handle(request, &response); | 46 handler.Handle(request, &response); |
| 67 ASSERT_EQ(HttpResponse::kNotFound, response.status()); | 47 ASSERT_EQ(HttpResponse::kNotFound, response.status()); |
| 68 } | 48 } |
| 69 | 49 |
| 70 TEST(HttpHandlerTest, HandleNewSession) { | 50 TEST(HttpHandlerTest, HandleNewSession) { |
| 71 scoped_ptr<HttpHandler::CommandMap> map(new HttpHandler::CommandMap()); | |
| 72 map->push_back(CommandMapping(kPost, "session", CommandNames::kNewSession)); | |
| 73 Logger log; | 51 Logger log; |
| 74 HttpHandler handler( | 52 HttpHandler handler(&log, "/base/"); |
| 75 &log, | 53 handler.command_map_.reset(new HttpHandler::CommandMap()); |
| 76 scoped_ptr<CommandExecutor>(new DummyExecutor()), | 54 handler.command_map_->push_back( |
| 77 map.Pass(), "/base/"); | 55 CommandMapping(kPost, internal::kNewSessionPathPattern, |
| 56 base::Bind(&DummyCommand, Status(kOk)))); |
| 78 HttpRequest request(kPost, "/base/session", std::string()); | 57 HttpRequest request(kPost, "/base/session", std::string()); |
| 79 HttpResponse response; | 58 HttpResponse response; |
| 80 handler.Handle(request, &response); | 59 handler.Handle(request, &response); |
| 81 ASSERT_EQ(HttpResponse::kSeeOther, response.status()); | 60 ASSERT_EQ(HttpResponse::kSeeOther, response.status()); |
| 82 std::string location; | 61 std::string location; |
| 83 ASSERT_TRUE(response.GetHeader("Location", &location)); | 62 ASSERT_TRUE(response.GetHeader("Location", &location)); |
| 84 std::string prefix = "/base/session/"; | 63 std::string prefix = "/base/session/"; |
| 85 ASSERT_EQ(prefix, location.substr(0, prefix.length())); | 64 ASSERT_EQ(prefix, location.substr(0, prefix.length())); |
| 86 } | 65 } |
| 87 | 66 |
| 88 TEST(HttpHandlerTest, HandleInvalidPost) { | 67 TEST(HttpHandlerTest, HandleInvalidPost) { |
| 89 scoped_ptr<HttpHandler::CommandMap> map(new HttpHandler::CommandMap()); | |
| 90 map->push_back(CommandMapping(kPost, "path", "cmd")); | |
| 91 Logger log; | 68 Logger log; |
| 92 HttpHandler handler( | 69 HttpHandler handler(&log, "/"); |
| 93 &log, | 70 handler.command_map_->push_back( |
| 94 scoped_ptr<CommandExecutor>(new DummyExecutor()), | 71 CommandMapping(kPost, "path", base::Bind(&DummyCommand, Status(kOk)))); |
| 95 map.Pass(), "/"); | |
| 96 HttpRequest request(kPost, "/path", "should be a dictionary"); | 72 HttpRequest request(kPost, "/path", "should be a dictionary"); |
| 97 HttpResponse response; | 73 HttpResponse response; |
| 98 handler.Handle(request, &response); | 74 handler.Handle(request, &response); |
| 99 ASSERT_EQ(HttpResponse::kBadRequest, response.status()); | 75 ASSERT_EQ(HttpResponse::kBadRequest, response.status()); |
| 100 } | 76 } |
| 101 | 77 |
| 102 TEST(HttpHandlerTest, HandleUnimplementedCommand) { | 78 TEST(HttpHandlerTest, HandleUnimplementedCommand) { |
| 103 scoped_ptr<HttpHandler::CommandMap> map(new HttpHandler::CommandMap()); | |
| 104 map->push_back(CommandMapping(kPost, "path", "cmd")); | |
| 105 Logger log; | 79 Logger log; |
| 106 HttpHandler handler( | 80 HttpHandler handler(&log, "/"); |
| 107 &log, | 81 handler.command_map_->push_back( |
| 108 scoped_ptr<CommandExecutor>(new DummyExecutor(kUnknownCommand)), | 82 CommandMapping(kPost, "path", |
| 109 map.Pass(), "/"); | 83 base::Bind(&DummyCommand, Status(kUnknownCommand)))); |
| 110 HttpRequest request(kPost, "/path", std::string()); | 84 HttpRequest request(kPost, "/path", std::string()); |
| 111 HttpResponse response; | 85 HttpResponse response; |
| 112 handler.Handle(request, &response); | 86 handler.Handle(request, &response); |
| 113 ASSERT_EQ(HttpResponse::kNotImplemented, response.status()); | 87 ASSERT_EQ(HttpResponse::kNotImplemented, response.status()); |
| 114 } | 88 } |
| 115 | 89 |
| 116 TEST(HttpHandlerTest, HandleCommand) { | 90 TEST(HttpHandlerTest, HandleCommand) { |
| 117 scoped_ptr<HttpHandler::CommandMap> map(new HttpHandler::CommandMap()); | |
| 118 map->push_back(CommandMapping(kPost, "path", "cmd")); | |
| 119 Logger log; | 91 Logger log; |
| 120 HttpHandler handler( | 92 HttpHandler handler(&log, "/"); |
| 121 &log, | 93 handler.command_map_->push_back( |
| 122 scoped_ptr<CommandExecutor>(new DummyExecutor()), | 94 CommandMapping(kPost, "path", base::Bind(&DummyCommand, Status(kOk)))); |
| 123 map.Pass(), "/"); | |
| 124 HttpRequest request(kPost, "/path", std::string()); | 95 HttpRequest request(kPost, "/path", std::string()); |
| 125 HttpResponse response; | 96 HttpResponse response; |
| 126 handler.Handle(request, &response); | 97 handler.Handle(request, &response); |
| 127 ASSERT_EQ(HttpResponse::kOk, response.status()); | 98 ASSERT_EQ(HttpResponse::kOk, response.status()); |
| 128 std::string mime; | 99 std::string mime; |
| 129 ASSERT_TRUE(response.GetHeader("Content-Type", &mime)); | 100 ASSERT_TRUE(response.GetHeader("Content-Type", &mime)); |
| 130 base::DictionaryValue body; | 101 base::DictionaryValue body; |
| 131 body.SetInteger("status", kOk); | 102 body.SetInteger("status", kOk); |
| 132 body.SetInteger("value", 1); | 103 body.SetInteger("value", 1); |
| 133 body.SetString("sessionId", "session_id"); | 104 body.SetString("sessionId", "session_id"); |
| 134 std::string json; | 105 std::string json; |
| 135 base::JSONWriter::Write(&body, &json); | 106 base::JSONWriter::Write(&body, &json); |
| 136 ASSERT_STREQ(json.c_str(), response.body().c_str()); | 107 ASSERT_STREQ(json.c_str(), response.body().c_str()); |
| 137 } | 108 } |
| 138 | 109 |
| 139 TEST(MatchesCommandTest, DiffMethod) { | 110 TEST(MatchesCommandTest, DiffMethod) { |
| 140 CommandMapping command(kPost, "path", "command"); | 111 CommandMapping command(kPost, "path", base::Bind(&DummyCommand, Status(kOk))); |
| 141 std::string session_id; | 112 std::string session_id; |
| 142 base::DictionaryValue params; | 113 base::DictionaryValue params; |
| 143 ASSERT_FALSE(internal::MatchesCommand( | 114 ASSERT_FALSE(internal::MatchesCommand( |
| 144 kGet, "path", command, &session_id, ¶ms)); | 115 kGet, "path", command, &session_id, ¶ms)); |
| 145 ASSERT_STREQ("", session_id.c_str()); | 116 ASSERT_STREQ("", session_id.c_str()); |
| 146 ASSERT_EQ(0u, params.size()); | 117 ASSERT_EQ(0u, params.size()); |
| 147 } | 118 } |
| 148 | 119 |
| 149 TEST(MatchesCommandTest, DiffPathLength) { | 120 TEST(MatchesCommandTest, DiffPathLength) { |
| 150 CommandMapping command(kPost, "path/path", "command"); | 121 CommandMapping command(kPost, "path/path", |
| 122 base::Bind(&DummyCommand, Status(kOk))); |
| 151 std::string session_id; | 123 std::string session_id; |
| 152 base::DictionaryValue params; | 124 base::DictionaryValue params; |
| 153 ASSERT_FALSE(internal::MatchesCommand( | 125 ASSERT_FALSE(internal::MatchesCommand( |
| 154 kPost, "path", command, &session_id, ¶ms)); | 126 kPost, "path", command, &session_id, ¶ms)); |
| 155 ASSERT_FALSE(internal::MatchesCommand( | 127 ASSERT_FALSE(internal::MatchesCommand( |
| 156 kPost, std::string(), command, &session_id, ¶ms)); | 128 kPost, std::string(), command, &session_id, ¶ms)); |
| 157 ASSERT_FALSE( | 129 ASSERT_FALSE( |
| 158 internal::MatchesCommand(kPost, "/", command, &session_id, ¶ms)); | 130 internal::MatchesCommand(kPost, "/", command, &session_id, ¶ms)); |
| 159 ASSERT_FALSE(internal::MatchesCommand( | 131 ASSERT_FALSE(internal::MatchesCommand( |
| 160 kPost, "path/path/path", command, &session_id, ¶ms)); | 132 kPost, "path/path/path", command, &session_id, ¶ms)); |
| 161 } | 133 } |
| 162 | 134 |
| 163 TEST(MatchesCommandTest, DiffPaths) { | 135 TEST(MatchesCommandTest, DiffPaths) { |
| 164 CommandMapping command(kPost, "path/apath", "command"); | 136 CommandMapping command(kPost, "path/apath", |
| 137 base::Bind(&DummyCommand, Status(kOk))); |
| 165 std::string session_id; | 138 std::string session_id; |
| 166 base::DictionaryValue params; | 139 base::DictionaryValue params; |
| 167 ASSERT_FALSE(internal::MatchesCommand( | 140 ASSERT_FALSE(internal::MatchesCommand( |
| 168 kPost, "path/bpath", command, &session_id, ¶ms)); | 141 kPost, "path/bpath", command, &session_id, ¶ms)); |
| 169 } | 142 } |
| 170 | 143 |
| 171 TEST(MatchesCommandTest, Substitution) { | 144 TEST(MatchesCommandTest, Substitution) { |
| 172 CommandMapping command(kPost, "path/:sessionId/space/:a/:b", "command"); | 145 CommandMapping command(kPost, "path/:sessionId/space/:a/:b", |
| 146 base::Bind(&DummyCommand, Status(kOk))); |
| 173 std::string session_id; | 147 std::string session_id; |
| 174 base::DictionaryValue params; | 148 base::DictionaryValue params; |
| 175 ASSERT_TRUE(internal::MatchesCommand( | 149 ASSERT_TRUE(internal::MatchesCommand( |
| 176 kPost, "path/1/space/2/3", command, &session_id, ¶ms)); | 150 kPost, "path/1/space/2/3", command, &session_id, ¶ms)); |
| 177 ASSERT_EQ("1", session_id); | 151 ASSERT_EQ("1", session_id); |
| 178 ASSERT_EQ(2u, params.size()); | 152 ASSERT_EQ(2u, params.size()); |
| 179 std::string param; | 153 std::string param; |
| 180 ASSERT_TRUE(params.GetString("a", ¶m)); | 154 ASSERT_TRUE(params.GetString("a", ¶m)); |
| 181 ASSERT_EQ("2", param); | 155 ASSERT_EQ("2", param); |
| 182 ASSERT_TRUE(params.GetString("b", ¶m)); | 156 ASSERT_TRUE(params.GetString("b", ¶m)); |
| 183 ASSERT_EQ("3", param); | 157 ASSERT_EQ("3", param); |
| 184 } | 158 } |
| OLD | NEW |