annotate UnitTestsSources/RestApiTests.cpp @ 969:3dce528b0cc2

RestApiHierarchy
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 30 Jun 2014 13:17:49 +0200
parents 8ed284e79850
children 1a3817d84f39
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege,
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
4 * Belgium
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
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 #include "gtest/gtest.h"
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 #include <ctype.h>
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 #include <glog/logging.h>
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
45 #include "../Core/RestApi/RestApiHierarchy.h"
209
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 using namespace Orthanc;
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
94 TEST(RestApi, ParseCookies)
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
95 {
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
96 HttpHandler::Arguments headers;
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
97 HttpHandler::Arguments cookies;
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
98
332
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 330
diff changeset
99 headers["cookie"] = "a=b;c=d;;;e=f;;g=h;";
330
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
100 HttpHandler::ParseCookies(cookies, headers);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
101 ASSERT_EQ(4u, cookies.size());
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
102 ASSERT_EQ("b", cookies["a"]);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
103 ASSERT_EQ("d", cookies["c"]);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
104 ASSERT_EQ("f", cookies["e"]);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
105 ASSERT_EQ("h", cookies["g"]);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
106
332
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 330
diff changeset
107 headers["cookie"] = " name = value ; name2=value2";
330
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
108 HttpHandler::ParseCookies(cookies, headers);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
109 ASSERT_EQ(2u, cookies.size());
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
110 ASSERT_EQ("value", cookies["name"]);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
111 ASSERT_EQ("value2", cookies["name2"]);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
112
332
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 330
diff changeset
113 headers["cookie"] = " ;;; ";
330
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
114 HttpHandler::ParseCookies(cookies, headers);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
115 ASSERT_EQ(0u, cookies.size());
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
116
332
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 330
diff changeset
117 headers["cookie"] = " ; n=v ;; ";
330
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
118 HttpHandler::ParseCookies(cookies, headers);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
119 ASSERT_EQ(1u, cookies.size());
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
120 ASSERT_EQ("v", cookies["n"]);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
121 }
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
122
209
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 TEST(RestApi, RestApiPath)
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 {
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 RestApiPath::Components args;
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 UriComponents trail;
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 {
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 RestApiPath uri("/coucou/{abc}/d/*");
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 ASSERT_TRUE(uri.Match(args, trail, "/coucou/moi/d/e/f/g"));
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 ASSERT_EQ(1u, args.size());
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 ASSERT_EQ(3u, trail.size());
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 ASSERT_EQ("moi", args["abc"]);
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 ASSERT_EQ("e", trail[0]);
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 ASSERT_EQ("f", trail[1]);
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 ASSERT_EQ("g", trail[2]);
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 ASSERT_FALSE(uri.Match(args, trail, "/coucou/moi/f"));
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 ASSERT_TRUE(uri.Match(args, trail, "/coucou/moi/d/"));
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 ASSERT_FALSE(uri.Match(args, trail, "/a/moi/d"));
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 }
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 {
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 RestApiPath uri("/coucou/{abc}/d");
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 ASSERT_FALSE(uri.Match(args, trail, "/coucou/moi/d/e/f/g"));
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 ASSERT_TRUE(uri.Match(args, trail, "/coucou/moi/d"));
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 ASSERT_EQ(1u, args.size());
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 ASSERT_EQ(0u, trail.size());
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 }
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 {
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 RestApiPath uri("/*");
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 ASSERT_TRUE(uri.Match(args, trail, "/a/b/c"));
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 ASSERT_EQ(0u, args.size());
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 ASSERT_EQ(3u, trail.size());
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 ASSERT_EQ("a", trail[0]);
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 ASSERT_EQ("b", trail[1]);
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 }
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
196 static int testValue;
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
197
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
198 template <int value>
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
199 static void SetValue(RestApi::GetCall& get)
966
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
200 {
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
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
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
205 static bool GetDirectory(Json::Value& target,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
206 RestApiHierarchy& hierarchy,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
207 const std::string& uri)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
208 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
209 UriComponents p;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
210 Toolbox::SplitUriComponents(p, uri);
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
211 return hierarchy.GetDirectory(target, p);
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
212 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
213
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
214
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
215 static bool HandleGet(RestApiHierarchy& hierarchy,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
216 const std::string& uri)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
217 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
218 UriComponents p;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
219 Toolbox::SplitUriComponents(p, uri);
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
220 return hierarchy.Handle(*reinterpret_cast<RestApi::GetCall*>(NULL), p);
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
221 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
222
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
223
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
224 TEST(RestApi, RestApiHierarchy)
966
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
225 {
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
226 RestApiHierarchy root;
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
227 root.Register(RestApiPath("/hello/world/test"), SetValue<1>);
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
228 root.Register(RestApiPath("/hello/world/test2"), SetValue<2>);
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
229 root.Register(RestApiPath("/hello/{world}/test3/test4"), SetValue<3>);
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
230 root.Register(RestApiPath("/hello2/*"), SetValue<4>);
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
231
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
232 Json::Value m;
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
233 root.CreateSiteMap(m);
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
234 std::cout << m;
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
235
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
236 Json::Value d;
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
237 ASSERT_FALSE(GetDirectory(d, root, "/hello"));
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
238
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
239 ASSERT_TRUE(GetDirectory(d, root, "/hello/a"));
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
240 ASSERT_EQ(1u, d.size());
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
241 ASSERT_EQ("test3", d[0].asString());
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
242
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
243 ASSERT_TRUE(GetDirectory(d, root, "/hello/world"));
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
244 ASSERT_EQ(2u, d.size());
966
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
245
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
246 ASSERT_TRUE(GetDirectory(d, root, "/hello/a/test3"));
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
247 ASSERT_EQ(1u, d.size());
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
248 ASSERT_EQ("test4", d[0].asString());
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
249
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
250 ASSERT_FALSE(GetDirectory(d, root, "/hello/world/test"));
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
251 ASSERT_FALSE(GetDirectory(d, root, "/hello/world/test2"));
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
252 ASSERT_FALSE(GetDirectory(d, root, "/hello2"));
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
253
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
254 testValue = 0;
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
255 ASSERT_TRUE(HandleGet(root, "/hello/world/test"));
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
256 ASSERT_EQ(testValue, 1);
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
257 ASSERT_TRUE(HandleGet(root, "/hello/world/test2"));
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
258 ASSERT_EQ(testValue, 2);
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
259 ASSERT_TRUE(HandleGet(root, "/hello/b/test3/test4"));
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
260 ASSERT_EQ(testValue, 3);
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
261 ASSERT_FALSE(HandleGet(root, "/hello/b/test3/test"));
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
262 ASSERT_EQ(testValue, 3);
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
263 ASSERT_TRUE(HandleGet(root, "/hello2/a/b"));
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
264 ASSERT_EQ(testValue, 4);
966
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
265 }