Mercurial > hg > orthanc
annotate UnitTestsSources/RestApi.cpp @ 2758:3d65f87f50f1 Orthanc-0.7.6
close old branch
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 17 Jul 2018 09:41:29 +0200 |
parents | 84513f2ee1f3 |
children | 886652370ff2 |
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 | 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" |
209 | 45 |
46 using namespace Orthanc; | |
47 | |
707
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
48 #if !defined(UNIT_TESTS_WITH_HTTP_CONNEXIONS) |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
49 #error "Please set UNIT_TESTS_WITH_HTTP_CONNEXIONS" |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
50 #endif |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
51 |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
52 TEST(HttpClient, Basic) |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
53 { |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
54 HttpClient c; |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
55 ASSERT_FALSE(c.IsVerbose()); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
56 c.SetVerbose(true); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
57 ASSERT_TRUE(c.IsVerbose()); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
58 c.SetVerbose(false); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
59 ASSERT_FALSE(c.IsVerbose()); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
60 |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
61 #if UNIT_TESTS_WITH_HTTP_CONNEXIONS == 1 |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
62 Json::Value v; |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
63 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
|
64 c.Apply(v); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
65 ASSERT_TRUE(v.isMember("StorageDirectory")); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
66 //ASSERT_EQ(GetLastStatusText()); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
67 |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
68 v = Json::nullValue; |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
69 |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
70 HttpClient cc(c); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
71 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
|
72 cc.Apply(v); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
73 ASSERT_TRUE(v.isMember("LuaScripts")); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
74 #endif |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
75 } |
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 TEST(RestApi, ChunkedBuffer) |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
78 { |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
79 ChunkedBuffer b; |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
80 ASSERT_EQ(0, b.GetNumBytes()); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
81 |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
82 b.AddChunk("hello", 5); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
83 ASSERT_EQ(5, b.GetNumBytes()); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
84 |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
85 b.AddChunk("world", 5); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
86 ASSERT_EQ(10, b.GetNumBytes()); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
87 |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
88 std::string s; |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
89 b.Flatten(s); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
90 ASSERT_EQ("helloworld", s); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
91 } |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
92 |
330 | 93 TEST(RestApi, ParseCookies) |
94 { | |
95 HttpHandler::Arguments headers; | |
96 HttpHandler::Arguments cookies; | |
97 | |
332 | 98 headers["cookie"] = "a=b;c=d;;;e=f;;g=h;"; |
330 | 99 HttpHandler::ParseCookies(cookies, headers); |
100 ASSERT_EQ(4u, cookies.size()); | |
101 ASSERT_EQ("b", cookies["a"]); | |
102 ASSERT_EQ("d", cookies["c"]); | |
103 ASSERT_EQ("f", cookies["e"]); | |
104 ASSERT_EQ("h", cookies["g"]); | |
105 | |
332 | 106 headers["cookie"] = " name = value ; name2=value2"; |
330 | 107 HttpHandler::ParseCookies(cookies, headers); |
108 ASSERT_EQ(2u, cookies.size()); | |
109 ASSERT_EQ("value", cookies["name"]); | |
110 ASSERT_EQ("value2", cookies["name2"]); | |
111 | |
332 | 112 headers["cookie"] = " ;;; "; |
330 | 113 HttpHandler::ParseCookies(cookies, headers); |
114 ASSERT_EQ(0u, cookies.size()); | |
115 | |
332 | 116 headers["cookie"] = " ; n=v ;; "; |
330 | 117 HttpHandler::ParseCookies(cookies, headers); |
118 ASSERT_EQ(1u, cookies.size()); | |
119 ASSERT_EQ("v", cookies["n"]); | |
120 } | |
121 | |
209 | 122 TEST(RestApi, RestApiPath) |
123 { | |
124 RestApiPath::Components args; | |
125 UriComponents trail; | |
126 | |
127 { | |
128 RestApiPath uri("/coucou/{abc}/d/*"); | |
129 ASSERT_TRUE(uri.Match(args, trail, "/coucou/moi/d/e/f/g")); | |
130 ASSERT_EQ(1u, args.size()); | |
131 ASSERT_EQ(3u, trail.size()); | |
132 ASSERT_EQ("moi", args["abc"]); | |
133 ASSERT_EQ("e", trail[0]); | |
134 ASSERT_EQ("f", trail[1]); | |
135 ASSERT_EQ("g", trail[2]); | |
136 | |
137 ASSERT_FALSE(uri.Match(args, trail, "/coucou/moi/f")); | |
138 ASSERT_TRUE(uri.Match(args, trail, "/coucou/moi/d/")); | |
139 ASSERT_FALSE(uri.Match(args, trail, "/a/moi/d")); | |
140 ASSERT_FALSE(uri.Match(args, trail, "/coucou/moi")); | |
141 } | |
142 | |
143 { | |
144 RestApiPath uri("/coucou/{abc}/d"); | |
145 ASSERT_FALSE(uri.Match(args, trail, "/coucou/moi/d/e/f/g")); | |
146 ASSERT_TRUE(uri.Match(args, trail, "/coucou/moi/d")); | |
147 ASSERT_EQ(1u, args.size()); | |
148 ASSERT_EQ(0u, trail.size()); | |
149 ASSERT_EQ("moi", args["abc"]); | |
150 } | |
151 | |
152 { | |
153 RestApiPath uri("/*"); | |
154 ASSERT_TRUE(uri.Match(args, trail, "/a/b/c")); | |
155 ASSERT_EQ(0u, args.size()); | |
156 ASSERT_EQ(3u, trail.size()); | |
157 ASSERT_EQ("a", trail[0]); | |
158 ASSERT_EQ("b", trail[1]); | |
159 ASSERT_EQ("c", trail[2]); | |
160 } | |
161 } |