annotate UnitTestsSources/RestApiTests.cpp @ 1492:9f66a12eb8fc

fix warnings for gcc 5
author jodogne
date Tue, 04 Aug 2015 14:47:35 +0200
parents f967bdf8534e
children b5eb5210af91
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
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
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
707
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
38 #include "../Core/ChunkedBuffer.h"
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
39 #include "../Core/HttpClient.h"
1486
f967bdf8534e refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
40 #include "../Core/Logging.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;
1419
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
64 c.SetUrl("http://www.montefiore.ulg.ac.be/~jodogne/Orthanc/Configuration.json");
707
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);
1419
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
72 cc.SetUrl("https://www.montefiore.ulg.ac.be/~jodogne/Orthanc/Configuration.json");
707
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;
1492
9f66a12eb8fc fix warnings for gcc 5
jodogne
parents: 1486
diff changeset
81 ASSERT_EQ(0u, b.GetNumBytes());
707
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);
1492
9f66a12eb8fc fix warnings for gcc 5
jodogne
parents: 1486
diff changeset
84 ASSERT_EQ(5u, b.GetNumBytes());
707
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);
1492
9f66a12eb8fc fix warnings for gcc 5
jodogne
parents: 1486
diff changeset
87 ASSERT_EQ(10u, b.GetNumBytes());
707
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 {
1441
f3672356c121 refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1419
diff changeset
96 IHttpHandler::Arguments headers;
f3672356c121 refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1419
diff changeset
97 IHttpHandler::Arguments cookies;
330
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;";
1441
f3672356c121 refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1419
diff changeset
100 HttpToolbox::ParseCookies(cookies, headers);
330
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";
1441
f3672356c121 refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1419
diff changeset
108 HttpToolbox::ParseCookies(cookies, headers);
330
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"] = " ;;; ";
1441
f3672356c121 refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1419
diff changeset
114 HttpToolbox::ParseCookies(cookies, headers);
330
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 ;; ";
1441
f3672356c121 refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1419
diff changeset
118 HttpToolbox::ParseCookies(cookies, headers);
330
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 {
1441
f3672356c121 refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1419
diff changeset
125 IHttpHandler::Arguments args;
209
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>
974
83622b0f544c refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
199 static void SetValue(RestApiGetCall& 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
978
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
215
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
216 namespace
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
217 {
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
218 class MyVisitor : public RestApiHierarchy::IVisitor
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
219 {
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
220 public:
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
221 virtual bool Visit(const RestApiHierarchy::Resource& resource,
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
222 const UriComponents& uri,
1441
f3672356c121 refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1419
diff changeset
223 const IHttpHandler::Arguments& components,
978
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
224 const UriComponents& trailing)
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
225 {
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
226 return resource.Handle(*reinterpret_cast<RestApiGetCall*>(NULL));
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
227 }
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
228 };
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
229 }
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
230
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
231
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
232 static bool HandleGet(RestApiHierarchy& hierarchy,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
233 const std::string& uri)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
234 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
235 UriComponents p;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
236 Toolbox::SplitUriComponents(p, uri);
978
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
237 MyVisitor visitor;
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
238 return hierarchy.LookupResource(p, visitor);
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
239 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
240
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
241
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
242 TEST(RestApi, RestApiHierarchy)
966
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
243 {
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
244 RestApiHierarchy root;
970
1a3817d84f39 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 969
diff changeset
245 root.Register("/hello/world/test", SetValue<1>);
1a3817d84f39 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 969
diff changeset
246 root.Register("/hello/world/test2", SetValue<2>);
1a3817d84f39 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 969
diff changeset
247 root.Register("/hello/{world}/test3/test4", SetValue<3>);
1a3817d84f39 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 969
diff changeset
248 root.Register("/hello2/*", SetValue<4>);
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
249
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
250 Json::Value m;
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
251 root.CreateSiteMap(m);
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
252 std::cout << m;
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 Json::Value d;
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
255 ASSERT_FALSE(GetDirectory(d, root, "/hello"));
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
256
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
257 ASSERT_TRUE(GetDirectory(d, root, "/hello/a"));
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
258 ASSERT_EQ(1u, d.size());
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
259 ASSERT_EQ("test3", d[0].asString());
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
260
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
261 ASSERT_TRUE(GetDirectory(d, root, "/hello/world"));
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
262 ASSERT_EQ(2u, d.size());
966
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
263
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
264 ASSERT_TRUE(GetDirectory(d, root, "/hello/a/test3"));
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
265 ASSERT_EQ(1u, d.size());
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
266 ASSERT_EQ("test4", d[0].asString());
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
267
1064
cd20e2568fc2 fix tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 978
diff changeset
268 ASSERT_TRUE(GetDirectory(d, root, "/hello/world/test"));
cd20e2568fc2 fix tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 978
diff changeset
269 ASSERT_TRUE(GetDirectory(d, root, "/hello/world/test2"));
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
270 ASSERT_FALSE(GetDirectory(d, root, "/hello2"));
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
271
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
272 testValue = 0;
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
273 ASSERT_TRUE(HandleGet(root, "/hello/world/test"));
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
274 ASSERT_EQ(testValue, 1);
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
275 ASSERT_TRUE(HandleGet(root, "/hello/world/test2"));
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
276 ASSERT_EQ(testValue, 2);
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
277 ASSERT_TRUE(HandleGet(root, "/hello/b/test3/test4"));
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
278 ASSERT_EQ(testValue, 3);
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
279 ASSERT_FALSE(HandleGet(root, "/hello/b/test3/test"));
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
280 ASSERT_EQ(testValue, 3);
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
281 ASSERT_TRUE(HandleGet(root, "/hello2/a/b"));
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
282 ASSERT_EQ(testValue, 4);
966
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
283 }