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
3 // found in the LICENSE file.
8 #include "base/json/json_writer.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/values.h"
11 #include "chrome/test/chromedriver/chrome/log.h"
12 #include "chrome/test/chromedriver/chrome/status.h"
13 #include "chrome/test/chromedriver/command.h"
14 #include "chrome/test/chromedriver/server/http_handler.h"
15 #include "net/http/http_status_code.h"
16 #include "net/server/http_server_request_info.h"
17 #include "net/server/http_server_response_info.h"
18 #include "testing/gtest/include/gtest/gtest.h"
24 const base::DictionaryValue& params,
25 const std::string& session_id,
26 const CommandCallback& callback) {
28 scoped_ptr<base::Value>(new base::FundamentalValue(1)),
32 void OnResponse(net::HttpServerResponseInfo* response_to_set,
33 scoped_ptr<net::HttpServerResponseInfo> response) {
34 *response_to_set = *response;
39 TEST(HttpHandlerTest, HandleOutsideOfBaseUrl) {
40 HttpHandler handler("base/url/");
41 net::HttpServerRequestInfo request;
42 request.method = "get";
43 request.path = "base/path";
44 request.data = "body";
45 net::HttpServerResponseInfo response;
46 handler.Handle(request, base::Bind(&OnResponse, &response));
47 ASSERT_EQ(net::HTTP_BAD_REQUEST, response.status_code());
50 TEST(HttpHandlerTest, HandleUnknownCommand) {
51 HttpHandler handler("/");
52 net::HttpServerRequestInfo request;
53 request.method = "get";
54 request.path = "/path";
55 net::HttpServerResponseInfo response;
56 handler.Handle(request, base::Bind(&OnResponse, &response));
57 ASSERT_EQ(net::HTTP_NOT_FOUND, response.status_code());
60 TEST(HttpHandlerTest, HandleNewSession) {
61 HttpHandler handler("/base/");
62 handler.command_map_.reset(new HttpHandler::CommandMap());
63 handler.command_map_->push_back(
64 CommandMapping(kPost, internal::kNewSessionPathPattern,
65 base::Bind(&DummyCommand, Status(kOk))));
66 net::HttpServerRequestInfo request;
67 request.method = "post";
68 request.path = "/base/session";
69 net::HttpServerResponseInfo response;
70 handler.Handle(request, base::Bind(&OnResponse, &response));
71 ASSERT_EQ(net::HTTP_SEE_OTHER, response.status_code());
72 ASSERT_NE(std::string::npos,
73 response.Serialize().find("Location:/base/session/"))
74 << response.Serialize();
77 TEST(HttpHandlerTest, HandleInvalidPost) {
78 HttpHandler handler("/");
79 handler.command_map_->push_back(
80 CommandMapping(kPost, "path", base::Bind(&DummyCommand, Status(kOk))));
81 net::HttpServerRequestInfo request;
82 request.method = "post";
83 request.path = "/path";
84 request.data = "should be a dictionary";
85 net::HttpServerResponseInfo response;
86 handler.Handle(request, base::Bind(&OnResponse, &response));
87 ASSERT_EQ(net::HTTP_BAD_REQUEST, response.status_code());
90 TEST(HttpHandlerTest, HandleUnimplementedCommand) {
91 HttpHandler handler("/");
92 handler.command_map_->push_back(
93 CommandMapping(kPost, "path",
94 base::Bind(&DummyCommand, Status(kUnknownCommand))));
95 net::HttpServerRequestInfo request;
96 request.method = "post";
97 request.path = "/path";
98 net::HttpServerResponseInfo response;
99 handler.Handle(request, base::Bind(&OnResponse, &response));
100 ASSERT_EQ(net::HTTP_NOT_IMPLEMENTED, response.status_code());
103 TEST(HttpHandlerTest, HandleCommand) {
104 HttpHandler handler("/");
105 handler.command_map_->push_back(
106 CommandMapping(kPost, "path", base::Bind(&DummyCommand, Status(kOk))));
107 net::HttpServerRequestInfo request;
108 request.method = "post";
109 request.path = "/path";
110 net::HttpServerResponseInfo response;
111 handler.Handle(request, base::Bind(&OnResponse, &response));
112 ASSERT_EQ(net::HTTP_OK, response.status_code());
113 base::DictionaryValue body;
114 body.SetInteger("status", kOk);
115 body.SetInteger("value", 1);
116 body.SetString("sessionId", "session_id");
118 base::JSONWriter::Write(&body, &json);
119 ASSERT_STREQ(json.c_str(), response.body().c_str());
122 TEST(MatchesCommandTest, DiffMethod) {
123 CommandMapping command(kPost, "path", base::Bind(&DummyCommand, Status(kOk)));
124 std::string session_id;
125 base::DictionaryValue params;
126 ASSERT_FALSE(internal::MatchesCommand(
127 "get", "path", command, &session_id, ¶ms));
128 ASSERT_STREQ("", session_id.c_str());
129 ASSERT_EQ(0u, params.size());
132 TEST(MatchesCommandTest, DiffPathLength) {
133 CommandMapping command(kPost, "path/path",
134 base::Bind(&DummyCommand, Status(kOk)));
135 std::string session_id;
136 base::DictionaryValue params;
137 ASSERT_FALSE(internal::MatchesCommand(
138 "post", "path", command, &session_id, ¶ms));
139 ASSERT_FALSE(internal::MatchesCommand(
140 "post", std::string(), command, &session_id, ¶ms));
142 internal::MatchesCommand("post", "/", command, &session_id, ¶ms));
143 ASSERT_FALSE(internal::MatchesCommand(
144 "post", "path/path/path", command, &session_id, ¶ms));
147 TEST(MatchesCommandTest, DiffPaths) {
148 CommandMapping command(kPost, "path/apath",
149 base::Bind(&DummyCommand, Status(kOk)));
150 std::string session_id;
151 base::DictionaryValue params;
152 ASSERT_FALSE(internal::MatchesCommand(
153 "post", "path/bpath", command, &session_id, ¶ms));
156 TEST(MatchesCommandTest, Substitution) {
157 CommandMapping command(kPost, "path/:sessionId/space/:a/:b",
158 base::Bind(&DummyCommand, Status(kOk)));
159 std::string session_id;
160 base::DictionaryValue params;
161 ASSERT_TRUE(internal::MatchesCommand(
162 "post", "path/1/space/2/3", command, &session_id, ¶ms));
163 ASSERT_EQ("1", session_id);
164 ASSERT_EQ(2u, params.size());
166 ASSERT_TRUE(params.GetString("a", ¶m));
167 ASSERT_EQ("2", param);
168 ASSERT_TRUE(params.GetString("b", ¶m));
169 ASSERT_EQ("3", param);