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" |
(...skipping 19 matching lines...) Expand all Loading... |
30 } | 30 } |
31 | 31 |
32 void OnResponse(net::HttpServerResponseInfo* response_to_set, | 32 void OnResponse(net::HttpServerResponseInfo* response_to_set, |
33 scoped_ptr<net::HttpServerResponseInfo> response) { | 33 scoped_ptr<net::HttpServerResponseInfo> response) { |
34 *response_to_set = *response; | 34 *response_to_set = *response; |
35 } | 35 } |
36 | 36 |
37 } // namespace | 37 } // namespace |
38 | 38 |
39 TEST(HttpHandlerTest, HandleOutsideOfBaseUrl) { | 39 TEST(HttpHandlerTest, HandleOutsideOfBaseUrl) { |
40 Logger log; | 40 HttpHandler handler("base/url/"); |
41 HttpHandler handler(&log, "base/url/"); | |
42 net::HttpServerRequestInfo request; | 41 net::HttpServerRequestInfo request; |
43 request.method = "get"; | 42 request.method = "get"; |
44 request.path = "base/path"; | 43 request.path = "base/path"; |
45 request.data = "body"; | 44 request.data = "body"; |
46 net::HttpServerResponseInfo response; | 45 net::HttpServerResponseInfo response; |
47 handler.Handle(request, base::Bind(&OnResponse, &response)); | 46 handler.Handle(request, base::Bind(&OnResponse, &response)); |
48 ASSERT_EQ(net::HTTP_BAD_REQUEST, response.status_code()); | 47 ASSERT_EQ(net::HTTP_BAD_REQUEST, response.status_code()); |
49 } | 48 } |
50 | 49 |
51 TEST(HttpHandlerTest, HandleUnknownCommand) { | 50 TEST(HttpHandlerTest, HandleUnknownCommand) { |
52 Logger log; | 51 HttpHandler handler("/"); |
53 HttpHandler handler(&log, "/"); | |
54 net::HttpServerRequestInfo request; | 52 net::HttpServerRequestInfo request; |
55 request.method = "get"; | 53 request.method = "get"; |
56 request.path = "/path"; | 54 request.path = "/path"; |
57 net::HttpServerResponseInfo response; | 55 net::HttpServerResponseInfo response; |
58 handler.Handle(request, base::Bind(&OnResponse, &response)); | 56 handler.Handle(request, base::Bind(&OnResponse, &response)); |
59 ASSERT_EQ(net::HTTP_NOT_FOUND, response.status_code()); | 57 ASSERT_EQ(net::HTTP_NOT_FOUND, response.status_code()); |
60 } | 58 } |
61 | 59 |
62 TEST(HttpHandlerTest, HandleNewSession) { | 60 TEST(HttpHandlerTest, HandleNewSession) { |
63 Logger log; | 61 HttpHandler handler("/base/"); |
64 HttpHandler handler(&log, "/base/"); | |
65 handler.command_map_.reset(new HttpHandler::CommandMap()); | 62 handler.command_map_.reset(new HttpHandler::CommandMap()); |
66 handler.command_map_->push_back( | 63 handler.command_map_->push_back( |
67 CommandMapping(kPost, internal::kNewSessionPathPattern, | 64 CommandMapping(kPost, internal::kNewSessionPathPattern, |
68 base::Bind(&DummyCommand, Status(kOk)))); | 65 base::Bind(&DummyCommand, Status(kOk)))); |
69 net::HttpServerRequestInfo request; | 66 net::HttpServerRequestInfo request; |
70 request.method = "post"; | 67 request.method = "post"; |
71 request.path = "/base/session"; | 68 request.path = "/base/session"; |
72 net::HttpServerResponseInfo response; | 69 net::HttpServerResponseInfo response; |
73 handler.Handle(request, base::Bind(&OnResponse, &response)); | 70 handler.Handle(request, base::Bind(&OnResponse, &response)); |
74 ASSERT_EQ(net::HTTP_SEE_OTHER, response.status_code()); | 71 ASSERT_EQ(net::HTTP_SEE_OTHER, response.status_code()); |
75 ASSERT_NE(std::string::npos, | 72 ASSERT_NE(std::string::npos, |
76 response.Serialize().find("Location:/base/session/")) | 73 response.Serialize().find("Location:/base/session/")) |
77 << response.Serialize(); | 74 << response.Serialize(); |
78 } | 75 } |
79 | 76 |
80 TEST(HttpHandlerTest, HandleInvalidPost) { | 77 TEST(HttpHandlerTest, HandleInvalidPost) { |
81 Logger log; | 78 HttpHandler handler("/"); |
82 HttpHandler handler(&log, "/"); | |
83 handler.command_map_->push_back( | 79 handler.command_map_->push_back( |
84 CommandMapping(kPost, "path", base::Bind(&DummyCommand, Status(kOk)))); | 80 CommandMapping(kPost, "path", base::Bind(&DummyCommand, Status(kOk)))); |
85 net::HttpServerRequestInfo request; | 81 net::HttpServerRequestInfo request; |
86 request.method = "post"; | 82 request.method = "post"; |
87 request.path = "/path"; | 83 request.path = "/path"; |
88 request.data = "should be a dictionary"; | 84 request.data = "should be a dictionary"; |
89 net::HttpServerResponseInfo response; | 85 net::HttpServerResponseInfo response; |
90 handler.Handle(request, base::Bind(&OnResponse, &response)); | 86 handler.Handle(request, base::Bind(&OnResponse, &response)); |
91 ASSERT_EQ(net::HTTP_BAD_REQUEST, response.status_code()); | 87 ASSERT_EQ(net::HTTP_BAD_REQUEST, response.status_code()); |
92 } | 88 } |
93 | 89 |
94 TEST(HttpHandlerTest, HandleUnimplementedCommand) { | 90 TEST(HttpHandlerTest, HandleUnimplementedCommand) { |
95 Logger log; | 91 HttpHandler handler("/"); |
96 HttpHandler handler(&log, "/"); | |
97 handler.command_map_->push_back( | 92 handler.command_map_->push_back( |
98 CommandMapping(kPost, "path", | 93 CommandMapping(kPost, "path", |
99 base::Bind(&DummyCommand, Status(kUnknownCommand)))); | 94 base::Bind(&DummyCommand, Status(kUnknownCommand)))); |
100 net::HttpServerRequestInfo request; | 95 net::HttpServerRequestInfo request; |
101 request.method = "post"; | 96 request.method = "post"; |
102 request.path = "/path"; | 97 request.path = "/path"; |
103 net::HttpServerResponseInfo response; | 98 net::HttpServerResponseInfo response; |
104 handler.Handle(request, base::Bind(&OnResponse, &response)); | 99 handler.Handle(request, base::Bind(&OnResponse, &response)); |
105 ASSERT_EQ(net::HTTP_NOT_IMPLEMENTED, response.status_code()); | 100 ASSERT_EQ(net::HTTP_NOT_IMPLEMENTED, response.status_code()); |
106 } | 101 } |
107 | 102 |
108 TEST(HttpHandlerTest, HandleCommand) { | 103 TEST(HttpHandlerTest, HandleCommand) { |
109 Logger log; | 104 HttpHandler handler("/"); |
110 HttpHandler handler(&log, "/"); | |
111 handler.command_map_->push_back( | 105 handler.command_map_->push_back( |
112 CommandMapping(kPost, "path", base::Bind(&DummyCommand, Status(kOk)))); | 106 CommandMapping(kPost, "path", base::Bind(&DummyCommand, Status(kOk)))); |
113 net::HttpServerRequestInfo request; | 107 net::HttpServerRequestInfo request; |
114 request.method = "post"; | 108 request.method = "post"; |
115 request.path = "/path"; | 109 request.path = "/path"; |
116 net::HttpServerResponseInfo response; | 110 net::HttpServerResponseInfo response; |
117 handler.Handle(request, base::Bind(&OnResponse, &response)); | 111 handler.Handle(request, base::Bind(&OnResponse, &response)); |
118 ASSERT_EQ(net::HTTP_OK, response.status_code()); | 112 ASSERT_EQ(net::HTTP_OK, response.status_code()); |
119 base::DictionaryValue body; | 113 base::DictionaryValue body; |
120 body.SetInteger("status", kOk); | 114 body.SetInteger("status", kOk); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 ASSERT_TRUE(internal::MatchesCommand( | 161 ASSERT_TRUE(internal::MatchesCommand( |
168 "post", "path/1/space/2/3", command, &session_id, ¶ms)); | 162 "post", "path/1/space/2/3", command, &session_id, ¶ms)); |
169 ASSERT_EQ("1", session_id); | 163 ASSERT_EQ("1", session_id); |
170 ASSERT_EQ(2u, params.size()); | 164 ASSERT_EQ(2u, params.size()); |
171 std::string param; | 165 std::string param; |
172 ASSERT_TRUE(params.GetString("a", ¶m)); | 166 ASSERT_TRUE(params.GetString("a", ¶m)); |
173 ASSERT_EQ("2", param); | 167 ASSERT_EQ("2", param); |
174 ASSERT_TRUE(params.GetString("b", ¶m)); | 168 ASSERT_TRUE(params.GetString("b", ¶m)); |
175 ASSERT_EQ("3", param); | 169 ASSERT_EQ("3", param); |
176 } | 170 } |
OLD | NEW |