Mercurial > hg > orthanc
comparison UnitTestsSources/RestApiTests.cpp @ 967:dfc076546821
add suffix Tests to unit test sources
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 27 Jun 2014 15:36:38 +0200 |
parents | UnitTestsSources/RestApi.cpp@886652370ff2 |
children | 8ed284e79850 |
comparison
equal
deleted
inserted
replaced
966:886652370ff2 | 967:dfc076546821 |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, | |
4 * Belgium | |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
10 * | |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
22 * | |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
33 #include "PrecompiledHeadersUnitTests.h" | |
34 #include "gtest/gtest.h" | |
35 | |
36 #include <ctype.h> | |
37 #include <glog/logging.h> | |
38 | |
39 #include "../Core/ChunkedBuffer.h" | |
40 #include "../Core/HttpClient.h" | |
41 #include "../Core/RestApi/RestApi.h" | |
42 #include "../Core/Uuid.h" | |
43 #include "../Core/OrthancException.h" | |
44 #include "../Core/Compression/ZlibCompressor.h" | |
45 | |
46 using namespace Orthanc; | |
47 | |
48 #if !defined(UNIT_TESTS_WITH_HTTP_CONNEXIONS) | |
49 #error "Please set UNIT_TESTS_WITH_HTTP_CONNEXIONS" | |
50 #endif | |
51 | |
52 TEST(HttpClient, Basic) | |
53 { | |
54 HttpClient c; | |
55 ASSERT_FALSE(c.IsVerbose()); | |
56 c.SetVerbose(true); | |
57 ASSERT_TRUE(c.IsVerbose()); | |
58 c.SetVerbose(false); | |
59 ASSERT_FALSE(c.IsVerbose()); | |
60 | |
61 #if UNIT_TESTS_WITH_HTTP_CONNEXIONS == 1 | |
62 Json::Value v; | |
63 c.SetUrl("http://orthanc.googlecode.com/hg/Resources/Configuration.json"); | |
64 c.Apply(v); | |
65 ASSERT_TRUE(v.isMember("StorageDirectory")); | |
66 //ASSERT_EQ(GetLastStatusText()); | |
67 | |
68 v = Json::nullValue; | |
69 | |
70 HttpClient cc(c); | |
71 cc.SetUrl("https://orthanc.googlecode.com/hg/Resources/Configuration.json"); | |
72 cc.Apply(v); | |
73 ASSERT_TRUE(v.isMember("LuaScripts")); | |
74 #endif | |
75 } | |
76 | |
77 TEST(RestApi, ChunkedBuffer) | |
78 { | |
79 ChunkedBuffer b; | |
80 ASSERT_EQ(0, b.GetNumBytes()); | |
81 | |
82 b.AddChunk("hello", 5); | |
83 ASSERT_EQ(5, b.GetNumBytes()); | |
84 | |
85 b.AddChunk("world", 5); | |
86 ASSERT_EQ(10, b.GetNumBytes()); | |
87 | |
88 std::string s; | |
89 b.Flatten(s); | |
90 ASSERT_EQ("helloworld", s); | |
91 } | |
92 | |
93 TEST(RestApi, ParseCookies) | |
94 { | |
95 HttpHandler::Arguments headers; | |
96 HttpHandler::Arguments cookies; | |
97 | |
98 headers["cookie"] = "a=b;c=d;;;e=f;;g=h;"; | |
99 HttpHandler::ParseCookies(cookies, headers); | |
100 ASSERT_EQ(4u, cookies.size()); | |
101 ASSERT_EQ("b", cookies["a"]); | |
102 ASSERT_EQ("d", cookies["c"]); | |
103 ASSERT_EQ("f", cookies["e"]); | |
104 ASSERT_EQ("h", cookies["g"]); | |
105 | |
106 headers["cookie"] = " name = value ; name2=value2"; | |
107 HttpHandler::ParseCookies(cookies, headers); | |
108 ASSERT_EQ(2u, cookies.size()); | |
109 ASSERT_EQ("value", cookies["name"]); | |
110 ASSERT_EQ("value2", cookies["name2"]); | |
111 | |
112 headers["cookie"] = " ;;; "; | |
113 HttpHandler::ParseCookies(cookies, headers); | |
114 ASSERT_EQ(0u, cookies.size()); | |
115 | |
116 headers["cookie"] = " ; n=v ;; "; | |
117 HttpHandler::ParseCookies(cookies, headers); | |
118 ASSERT_EQ(1u, cookies.size()); | |
119 ASSERT_EQ("v", cookies["n"]); | |
120 } | |
121 | |
122 TEST(RestApi, RestApiPath) | |
123 { | |
124 RestApiPath::Components args; | |
125 UriComponents trail; | |
126 | |
127 { | |
128 RestApiPath uri("/coucou/{abc}/d/*"); | |
129 ASSERT_TRUE(uri.Match(args, trail, "/coucou/moi/d/e/f/g")); | |
130 ASSERT_EQ(1u, args.size()); | |
131 ASSERT_EQ(3u, trail.size()); | |
132 ASSERT_EQ("moi", args["abc"]); | |
133 ASSERT_EQ("e", trail[0]); | |
134 ASSERT_EQ("f", trail[1]); | |
135 ASSERT_EQ("g", trail[2]); | |
136 | |
137 ASSERT_FALSE(uri.Match(args, trail, "/coucou/moi/f")); | |
138 ASSERT_TRUE(uri.Match(args, trail, "/coucou/moi/d/")); | |
139 ASSERT_FALSE(uri.Match(args, trail, "/a/moi/d")); | |
140 ASSERT_FALSE(uri.Match(args, trail, "/coucou/moi")); | |
141 | |
142 ASSERT_EQ(3u, uri.GetLevelCount()); | |
143 ASSERT_TRUE(uri.IsUniversalTrailing()); | |
144 | |
145 ASSERT_EQ("coucou", uri.GetLevelName(0)); | |
146 ASSERT_THROW(uri.GetWildcardName(0), OrthancException); | |
147 | |
148 ASSERT_EQ("abc", uri.GetWildcardName(1)); | |
149 ASSERT_THROW(uri.GetLevelName(1), OrthancException); | |
150 | |
151 ASSERT_EQ("d", uri.GetLevelName(2)); | |
152 ASSERT_THROW(uri.GetWildcardName(2), OrthancException); | |
153 } | |
154 | |
155 { | |
156 RestApiPath uri("/coucou/{abc}/d"); | |
157 ASSERT_FALSE(uri.Match(args, trail, "/coucou/moi/d/e/f/g")); | |
158 ASSERT_TRUE(uri.Match(args, trail, "/coucou/moi/d")); | |
159 ASSERT_EQ(1u, args.size()); | |
160 ASSERT_EQ(0u, trail.size()); | |
161 ASSERT_EQ("moi", args["abc"]); | |
162 | |
163 ASSERT_EQ(3u, uri.GetLevelCount()); | |
164 ASSERT_FALSE(uri.IsUniversalTrailing()); | |
165 | |
166 ASSERT_EQ("coucou", uri.GetLevelName(0)); | |
167 ASSERT_THROW(uri.GetWildcardName(0), OrthancException); | |
168 | |
169 ASSERT_EQ("abc", uri.GetWildcardName(1)); | |
170 ASSERT_THROW(uri.GetLevelName(1), OrthancException); | |
171 | |
172 ASSERT_EQ("d", uri.GetLevelName(2)); | |
173 ASSERT_THROW(uri.GetWildcardName(2), OrthancException); | |
174 } | |
175 | |
176 { | |
177 RestApiPath uri("/*"); | |
178 ASSERT_TRUE(uri.Match(args, trail, "/a/b/c")); | |
179 ASSERT_EQ(0u, args.size()); | |
180 ASSERT_EQ(3u, trail.size()); | |
181 ASSERT_EQ("a", trail[0]); | |
182 ASSERT_EQ("b", trail[1]); | |
183 ASSERT_EQ("c", trail[2]); | |
184 | |
185 ASSERT_EQ(0u, uri.GetLevelCount()); | |
186 ASSERT_TRUE(uri.IsUniversalTrailing()); | |
187 } | |
188 } | |
189 | |
190 | |
191 | |
192 | |
193 | |
194 | |
195 namespace Orthanc | |
196 { | |
197 class RestApiResource | |
198 { | |
199 private: | |
200 struct Handlers | |
201 { | |
202 std::list<RestApi::GetHandler> getHandlers_; | |
203 std::list<RestApi::PutHandler> putHandlers_; | |
204 std::list<RestApi::PostHandler> postHandlers_; | |
205 std::list<RestApi::DeleteHandler> deleteHandlers_; | |
206 | |
207 void Register(RestApi::GetHandler handler) | |
208 { | |
209 getHandlers_.push_back(handler); | |
210 } | |
211 | |
212 void Register(RestApi::PutHandler handler) | |
213 { | |
214 putHandlers_.push_back(handler); | |
215 } | |
216 | |
217 void Register(RestApi::PostHandler handler) | |
218 { | |
219 postHandlers_.push_back(handler); | |
220 } | |
221 | |
222 void Register(RestApi::DeleteHandler handler) | |
223 { | |
224 deleteHandlers_.push_back(handler); | |
225 } | |
226 }; | |
227 | |
228 | |
229 typedef std::map<std::string, RestApiResource*> Children; | |
230 | |
231 Children children_; | |
232 Children wildcardChildren_; | |
233 Handlers handlers_; | |
234 Handlers universalHandlers_; | |
235 | |
236 | |
237 static RestApiResource& AddChild(Children& children, | |
238 const std::string& name) | |
239 { | |
240 Children::iterator it = children.find(name); | |
241 | |
242 if (it == children.end()) | |
243 { | |
244 // Create new child | |
245 RestApiResource *child = new RestApiResource; | |
246 children[name] = child; | |
247 return *child; | |
248 } | |
249 else | |
250 { | |
251 return *it->second; | |
252 } | |
253 } | |
254 | |
255 | |
256 static void DeleteChildren(Children& children) | |
257 { | |
258 for (Children::iterator it = children.begin(); | |
259 it != children.end(); it++) | |
260 { | |
261 delete it->second; | |
262 } | |
263 } | |
264 | |
265 | |
266 | |
267 | |
268 template <typename Handler> | |
269 void RegisterInternal(const RestApiPath& path, | |
270 Handler handler, | |
271 size_t level) | |
272 { | |
273 if (path.GetLevelCount() == level) | |
274 { | |
275 if (path.IsUniversalTrailing()) | |
276 { | |
277 universalHandlers_.Register(handler); | |
278 } | |
279 else | |
280 { | |
281 handlers_.Register(handler); | |
282 } | |
283 } | |
284 else if (path.IsWildcardLevel(level)) | |
285 { | |
286 AddChild(wildcardChildren_, path.GetWildcardName(level)); | |
287 } | |
288 } | |
289 | |
290 | |
291 public: | |
292 ~RestApiResource() | |
293 { | |
294 DeleteChildren(children_); | |
295 DeleteChildren(wildcardChildren_); | |
296 } | |
297 | |
298 void Register(const RestApiPath& path, | |
299 RestApi::GetHandler handler) | |
300 { | |
301 RegisterInternal(path, handler, 0); | |
302 } | |
303 }; | |
304 | |
305 } | |
306 | |
307 | |
308 | |
309 static void Toto(RestApi::GetCall& get) | |
310 { | |
311 } | |
312 | |
313 | |
314 TEST(RestApi, RestApiResource) | |
315 { | |
316 RestApiResource root; | |
317 | |
318 root.Register(RestApiPath("/hello/world/test"), Toto); | |
319 } |