Mercurial > hg > orthanc
annotate UnitTestsSources/RestApiTests.cpp @ 1301:5d56cfa49f68
rename
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 09 Feb 2015 15:31:29 +0100 |
parents | 6e7e5ed91c2d |
children | 20b4521631e1 6784a119484d |
rev | line source |
---|---|
827
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
1 /** |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
2 * Orthanc - A Lightweight, RESTful DICOM Store |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1064
diff
changeset
|
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics |
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1064
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
827
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
5 * |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
6 * This program is free software: you can redistribute it and/or |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
7 * modify it under the terms of the GNU General Public License as |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
8 * published by the Free Software Foundation, either version 3 of the |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
9 * License, or (at your option) any later version. |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
10 * |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
11 * In addition, as a special exception, the copyright holders of this |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
12 * program give permission to link the code of its release with the |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
14 * that use the same license as the "OpenSSL" library), and distribute |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
15 * the linked executables. You must obey the GNU General Public License |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
16 * in all respects for all of the code used other than "OpenSSL". If you |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
17 * modify file(s) with this exception, you may extend this exception to |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
18 * your version of the file(s), but you are not obligated to do so. If |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
19 * you do not wish to do so, delete this exception statement from your |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
20 * version. If you delete this exception statement from all source files |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
21 * in the program, then also delete it here. |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
22 * |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
23 * This program is distributed in the hope that it will be useful, but |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
24 * WITHOUT ANY WARRANTY; without even the implied warranty of |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
26 * General Public License for more details. |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
27 * |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
28 * You should have received a copy of the GNU General Public License |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
30 **/ |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
31 |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
32 |
831
84513f2ee1f3
pch for unit tests and server
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
827
diff
changeset
|
33 #include "PrecompiledHeadersUnitTests.h" |
209 | 34 #include "gtest/gtest.h" |
35 | |
36 #include <ctype.h> | |
37 #include <glog/logging.h> | |
38 | |
707
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
39 #include "../Core/ChunkedBuffer.h" |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
40 #include "../Core/HttpClient.h" |
209 | 41 #include "../Core/RestApi/RestApi.h" |
221
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
210
diff
changeset
|
42 #include "../Core/Uuid.h" |
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
210
diff
changeset
|
43 #include "../Core/OrthancException.h" |
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
210
diff
changeset
|
44 #include "../Core/Compression/ZlibCompressor.h" |
969 | 45 #include "../Core/RestApi/RestApiHierarchy.h" |
209 | 46 |
47 using namespace Orthanc; | |
48 | |
707
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
49 #if !defined(UNIT_TESTS_WITH_HTTP_CONNEXIONS) |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
50 #error "Please set UNIT_TESTS_WITH_HTTP_CONNEXIONS" |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
51 #endif |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
52 |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
53 TEST(HttpClient, Basic) |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
54 { |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
55 HttpClient c; |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
56 ASSERT_FALSE(c.IsVerbose()); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
57 c.SetVerbose(true); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
58 ASSERT_TRUE(c.IsVerbose()); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
59 c.SetVerbose(false); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
60 ASSERT_FALSE(c.IsVerbose()); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
61 |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
62 #if UNIT_TESTS_WITH_HTTP_CONNEXIONS == 1 |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
63 Json::Value v; |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
64 c.SetUrl("http://orthanc.googlecode.com/hg/Resources/Configuration.json"); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
65 c.Apply(v); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
66 ASSERT_TRUE(v.isMember("StorageDirectory")); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
67 //ASSERT_EQ(GetLastStatusText()); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
68 |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
69 v = Json::nullValue; |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
70 |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
71 HttpClient cc(c); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
72 cc.SetUrl("https://orthanc.googlecode.com/hg/Resources/Configuration.json"); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
73 cc.Apply(v); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
74 ASSERT_TRUE(v.isMember("LuaScripts")); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
75 #endif |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
76 } |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
77 |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
78 TEST(RestApi, ChunkedBuffer) |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
79 { |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
80 ChunkedBuffer b; |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
81 ASSERT_EQ(0, b.GetNumBytes()); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
82 |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
83 b.AddChunk("hello", 5); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
84 ASSERT_EQ(5, b.GetNumBytes()); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
85 |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
86 b.AddChunk("world", 5); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
87 ASSERT_EQ(10, b.GetNumBytes()); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
88 |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
89 std::string s; |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
90 b.Flatten(s); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
91 ASSERT_EQ("helloworld", s); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
92 } |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
93 |
330 | 94 TEST(RestApi, ParseCookies) |
95 { | |
96 HttpHandler::Arguments headers; | |
97 HttpHandler::Arguments cookies; | |
98 | |
332 | 99 headers["cookie"] = "a=b;c=d;;;e=f;;g=h;"; |
330 | 100 HttpHandler::ParseCookies(cookies, headers); |
101 ASSERT_EQ(4u, cookies.size()); | |
102 ASSERT_EQ("b", cookies["a"]); | |
103 ASSERT_EQ("d", cookies["c"]); | |
104 ASSERT_EQ("f", cookies["e"]); | |
105 ASSERT_EQ("h", cookies["g"]); | |
106 | |
332 | 107 headers["cookie"] = " name = value ; name2=value2"; |
330 | 108 HttpHandler::ParseCookies(cookies, headers); |
109 ASSERT_EQ(2u, cookies.size()); | |
110 ASSERT_EQ("value", cookies["name"]); | |
111 ASSERT_EQ("value2", cookies["name2"]); | |
112 | |
332 | 113 headers["cookie"] = " ;;; "; |
330 | 114 HttpHandler::ParseCookies(cookies, headers); |
115 ASSERT_EQ(0u, cookies.size()); | |
116 | |
332 | 117 headers["cookie"] = " ; n=v ;; "; |
330 | 118 HttpHandler::ParseCookies(cookies, headers); |
119 ASSERT_EQ(1u, cookies.size()); | |
120 ASSERT_EQ("v", cookies["n"]); | |
121 } | |
122 | |
209 | 123 TEST(RestApi, RestApiPath) |
124 { | |
975 | 125 HttpHandler::Arguments args; |
209 | 126 UriComponents trail; |
127 | |
128 { | |
129 RestApiPath uri("/coucou/{abc}/d/*"); | |
130 ASSERT_TRUE(uri.Match(args, trail, "/coucou/moi/d/e/f/g")); | |
131 ASSERT_EQ(1u, args.size()); | |
132 ASSERT_EQ(3u, trail.size()); | |
133 ASSERT_EQ("moi", args["abc"]); | |
134 ASSERT_EQ("e", trail[0]); | |
135 ASSERT_EQ("f", trail[1]); | |
136 ASSERT_EQ("g", trail[2]); | |
137 | |
138 ASSERT_FALSE(uri.Match(args, trail, "/coucou/moi/f")); | |
139 ASSERT_TRUE(uri.Match(args, trail, "/coucou/moi/d/")); | |
140 ASSERT_FALSE(uri.Match(args, trail, "/a/moi/d")); | |
141 ASSERT_FALSE(uri.Match(args, trail, "/coucou/moi")); | |
966
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
142 |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
143 ASSERT_EQ(3u, uri.GetLevelCount()); |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
144 ASSERT_TRUE(uri.IsUniversalTrailing()); |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
145 |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
146 ASSERT_EQ("coucou", uri.GetLevelName(0)); |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
147 ASSERT_THROW(uri.GetWildcardName(0), OrthancException); |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
148 |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
149 ASSERT_EQ("abc", uri.GetWildcardName(1)); |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
150 ASSERT_THROW(uri.GetLevelName(1), OrthancException); |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
151 |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
152 ASSERT_EQ("d", uri.GetLevelName(2)); |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
153 ASSERT_THROW(uri.GetWildcardName(2), OrthancException); |
209 | 154 } |
155 | |
156 { | |
157 RestApiPath uri("/coucou/{abc}/d"); | |
158 ASSERT_FALSE(uri.Match(args, trail, "/coucou/moi/d/e/f/g")); | |
159 ASSERT_TRUE(uri.Match(args, trail, "/coucou/moi/d")); | |
160 ASSERT_EQ(1u, args.size()); | |
161 ASSERT_EQ(0u, trail.size()); | |
162 ASSERT_EQ("moi", args["abc"]); | |
966
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
163 |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
164 ASSERT_EQ(3u, uri.GetLevelCount()); |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
165 ASSERT_FALSE(uri.IsUniversalTrailing()); |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
166 |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
167 ASSERT_EQ("coucou", uri.GetLevelName(0)); |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
168 ASSERT_THROW(uri.GetWildcardName(0), OrthancException); |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
169 |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
170 ASSERT_EQ("abc", uri.GetWildcardName(1)); |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
171 ASSERT_THROW(uri.GetLevelName(1), OrthancException); |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
172 |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
173 ASSERT_EQ("d", uri.GetLevelName(2)); |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
174 ASSERT_THROW(uri.GetWildcardName(2), OrthancException); |
209 | 175 } |
176 | |
177 { | |
178 RestApiPath uri("/*"); | |
179 ASSERT_TRUE(uri.Match(args, trail, "/a/b/c")); | |
180 ASSERT_EQ(0u, args.size()); | |
181 ASSERT_EQ(3u, trail.size()); | |
182 ASSERT_EQ("a", trail[0]); | |
183 ASSERT_EQ("b", trail[1]); | |
184 ASSERT_EQ("c", trail[2]); | |
966
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
185 |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
186 ASSERT_EQ(0u, uri.GetLevelCount()); |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
187 ASSERT_TRUE(uri.IsUniversalTrailing()); |
209 | 188 } |
189 } | |
966
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
190 |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
191 |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
192 |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
193 |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
194 |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
195 |
968 | 196 static int testValue; |
197 | |
198 template <int value> | |
974 | 199 static void SetValue(RestApiGetCall& get) |
966
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
200 { |
968 | 201 testValue = value; |
966
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
202 } |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
203 |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
204 |
969 | 205 static bool GetDirectory(Json::Value& target, |
206 RestApiHierarchy& hierarchy, | |
207 const std::string& uri) | |
208 { | |
209 UriComponents p; | |
210 Toolbox::SplitUriComponents(p, uri); | |
211 return hierarchy.GetDirectory(target, p); | |
212 } | |
213 | |
214 | |
978 | 215 |
216 namespace | |
217 { | |
218 class MyVisitor : public RestApiHierarchy::IVisitor | |
219 { | |
220 public: | |
221 virtual bool Visit(const RestApiHierarchy::Resource& resource, | |
222 const UriComponents& uri, | |
223 const HttpHandler::Arguments& components, | |
224 const UriComponents& trailing) | |
225 { | |
226 return resource.Handle(*reinterpret_cast<RestApiGetCall*>(NULL)); | |
227 } | |
228 }; | |
229 } | |
230 | |
231 | |
969 | 232 static bool HandleGet(RestApiHierarchy& hierarchy, |
233 const std::string& uri) | |
234 { | |
235 UriComponents p; | |
236 Toolbox::SplitUriComponents(p, uri); | |
978 | 237 MyVisitor visitor; |
238 return hierarchy.LookupResource(p, visitor); | |
969 | 239 } |
240 | |
241 | |
968 | 242 TEST(RestApi, RestApiHierarchy) |
966
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
243 { |
968 | 244 RestApiHierarchy root; |
970 | 245 root.Register("/hello/world/test", SetValue<1>); |
246 root.Register("/hello/world/test2", SetValue<2>); | |
247 root.Register("/hello/{world}/test3/test4", SetValue<3>); | |
248 root.Register("/hello2/*", SetValue<4>); | |
968 | 249 |
250 Json::Value m; | |
251 root.CreateSiteMap(m); | |
252 std::cout << m; | |
253 | |
254 Json::Value d; | |
969 | 255 ASSERT_FALSE(GetDirectory(d, root, "/hello")); |
968 | 256 |
969 | 257 ASSERT_TRUE(GetDirectory(d, root, "/hello/a")); |
968 | 258 ASSERT_EQ(1u, d.size()); |
259 ASSERT_EQ("test3", d[0].asString()); | |
260 | |
969 | 261 ASSERT_TRUE(GetDirectory(d, root, "/hello/world")); |
968 | 262 ASSERT_EQ(2u, d.size()); |
966
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
263 |
969 | 264 ASSERT_TRUE(GetDirectory(d, root, "/hello/a/test3")); |
968 | 265 ASSERT_EQ(1u, d.size()); |
266 ASSERT_EQ("test4", d[0].asString()); | |
267 | |
1064 | 268 ASSERT_TRUE(GetDirectory(d, root, "/hello/world/test")); |
269 ASSERT_TRUE(GetDirectory(d, root, "/hello/world/test2")); | |
969 | 270 ASSERT_FALSE(GetDirectory(d, root, "/hello2")); |
968 | 271 |
272 testValue = 0; | |
969 | 273 ASSERT_TRUE(HandleGet(root, "/hello/world/test")); |
968 | 274 ASSERT_EQ(testValue, 1); |
969 | 275 ASSERT_TRUE(HandleGet(root, "/hello/world/test2")); |
968 | 276 ASSERT_EQ(testValue, 2); |
969 | 277 ASSERT_TRUE(HandleGet(root, "/hello/b/test3/test4")); |
968 | 278 ASSERT_EQ(testValue, 3); |
969 | 279 ASSERT_FALSE(HandleGet(root, "/hello/b/test3/test")); |
968 | 280 ASSERT_EQ(testValue, 3); |
969 | 281 ASSERT_TRUE(HandleGet(root, "/hello2/a/b")); |
968 | 282 ASSERT_EQ(testValue, 4); |
966
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
283 } |