Mercurial > hg > orthanc
annotate UnitTestsSources/RestApiTests.cpp @ 1579:b93c398f934d
check the size of the enumerations in the plugin engine
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 25 Aug 2015 15:45:50 +0200 |
parents | 33d34bc4ac15 |
children | 5ad4e4d92ecb |
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 | |
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 | 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 |
1534
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
53 |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
54 |
707
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
55 TEST(HttpClient, Basic) |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
56 { |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
57 HttpClient c; |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
58 ASSERT_FALSE(c.IsVerbose()); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
59 c.SetVerbose(true); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
60 ASSERT_TRUE(c.IsVerbose()); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
61 c.SetVerbose(false); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
62 ASSERT_FALSE(c.IsVerbose()); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
63 |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
64 #if UNIT_TESTS_WITH_HTTP_CONNEXIONS == 1 |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
65 Json::Value v; |
1419 | 66 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
|
67 c.Apply(v); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
68 ASSERT_TRUE(v.isMember("StorageDirectory")); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
69 #endif |
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 |
1532 | 72 |
1545 | 73 #if UNIT_TESTS_WITH_HTTP_CONNEXIONS == 1 && ORTHANC_SSL_ENABLED == 1 |
1534
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
74 |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
75 /** |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
76 The HTTPS CA certificates for BitBucket were extracted as follows: |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
77 |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
78 (1) We retrieve the certification chain of BitBucket: |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
79 |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
80 # echo | openssl s_client -showcerts -connect www.bitbucket.org:443 |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
81 |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
82 (2) We see that the certification authority (CA) is |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
83 "www.digicert.com", and the root certificate is "DigiCert High |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
84 Assurance EV Root CA". As a consequence, we navigate to DigiCert to |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
85 find the URL to this CA certificate: |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
86 |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
87 firefox https://www.digicert.com/digicert-root-certificates.htm |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
88 |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
89 (3) Once we get the URL to the CA certificate, we convert it to a C |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
90 macro that can be used by libcurl: |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
91 |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
92 # cd UnitTestsSources |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
93 # ../Resources/RetrieveCACertificates.py BITBUCKET_CERTIFICATES https://www.digicert.com/CACerts/DigiCertHighAssuranceEVRootCA.crt > BitbucketCACertificates.h |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
94 **/ |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
95 |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
96 #include "BitbucketCACertificates.h" |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
97 |
1533
0011cc99443c
improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1532
diff
changeset
|
98 TEST(HttpClient, Ssl) |
1532 | 99 { |
1534
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
100 Toolbox::WriteFile(BITBUCKET_CERTIFICATES, "UnitTestsResults/bitbucket.cert"); |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
101 |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
102 /*{ |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
103 std::string s; |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
104 Toolbox::ReadFile(s, "/usr/share/ca-certificates/mozilla/WoSign.crt"); |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
105 Toolbox::WriteFile(s, "UnitTestsResults/bitbucket.cert"); |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
106 }*/ |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
107 |
1532 | 108 HttpClient c; |
1534
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
109 c.SetHttpsVerifyPeers(true); |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
110 c.SetHttpsCACertificates("UnitTestsResults/bitbucket.cert"); |
1533
0011cc99443c
improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1532
diff
changeset
|
111 c.SetUrl("https://bitbucket.org/sjodogne/orthanc/raw/Orthanc-0.9.3/Resources/Configuration.json"); |
1532 | 112 |
1534
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
113 Json::Value v; |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
114 c.Apply(v); |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
115 ASSERT_TRUE(v.isMember("LuaScripts")); |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
116 } |
1533
0011cc99443c
improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1532
diff
changeset
|
117 |
1534
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
118 TEST(HttpClient, SslNoVerification) |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
119 { |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
120 HttpClient c; |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
121 c.SetHttpsVerifyPeers(false); |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
122 c.SetUrl("https://bitbucket.org/sjodogne/orthanc/raw/Orthanc-0.9.3/Resources/Configuration.json"); |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
123 |
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
124 Json::Value v; |
1532 | 125 c.Apply(v); |
1534
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
126 ASSERT_TRUE(v.isMember("LuaScripts")); |
1532 | 127 } |
1534
95b3b0260240
Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1533
diff
changeset
|
128 |
1532 | 129 #endif |
130 | |
131 | |
707
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
132 TEST(RestApi, ChunkedBuffer) |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
133 { |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
134 ChunkedBuffer b; |
1492 | 135 ASSERT_EQ(0u, b.GetNumBytes()); |
707
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
136 |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
137 b.AddChunk("hello", 5); |
1492 | 138 ASSERT_EQ(5u, b.GetNumBytes()); |
707
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
139 |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
140 b.AddChunk("world", 5); |
1492 | 141 ASSERT_EQ(10u, b.GetNumBytes()); |
707
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
142 |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
143 std::string s; |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
144 b.Flatten(s); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
145 ASSERT_EQ("helloworld", s); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
146 } |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
147 |
330 | 148 TEST(RestApi, ParseCookies) |
149 { | |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1419
diff
changeset
|
150 IHttpHandler::Arguments headers; |
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1419
diff
changeset
|
151 IHttpHandler::Arguments cookies; |
330 | 152 |
332 | 153 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
|
154 HttpToolbox::ParseCookies(cookies, headers); |
330 | 155 ASSERT_EQ(4u, cookies.size()); |
156 ASSERT_EQ("b", cookies["a"]); | |
157 ASSERT_EQ("d", cookies["c"]); | |
158 ASSERT_EQ("f", cookies["e"]); | |
159 ASSERT_EQ("h", cookies["g"]); | |
160 | |
332 | 161 headers["cookie"] = " name = value ; name2=value2"; |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1419
diff
changeset
|
162 HttpToolbox::ParseCookies(cookies, headers); |
330 | 163 ASSERT_EQ(2u, cookies.size()); |
164 ASSERT_EQ("value", cookies["name"]); | |
165 ASSERT_EQ("value2", cookies["name2"]); | |
166 | |
332 | 167 headers["cookie"] = " ;;; "; |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1419
diff
changeset
|
168 HttpToolbox::ParseCookies(cookies, headers); |
330 | 169 ASSERT_EQ(0u, cookies.size()); |
170 | |
332 | 171 headers["cookie"] = " ; n=v ;; "; |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1419
diff
changeset
|
172 HttpToolbox::ParseCookies(cookies, headers); |
330 | 173 ASSERT_EQ(1u, cookies.size()); |
174 ASSERT_EQ("v", cookies["n"]); | |
175 } | |
176 | |
209 | 177 TEST(RestApi, RestApiPath) |
178 { | |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1419
diff
changeset
|
179 IHttpHandler::Arguments args; |
209 | 180 UriComponents trail; |
181 | |
182 { | |
183 RestApiPath uri("/coucou/{abc}/d/*"); | |
184 ASSERT_TRUE(uri.Match(args, trail, "/coucou/moi/d/e/f/g")); | |
185 ASSERT_EQ(1u, args.size()); | |
186 ASSERT_EQ(3u, trail.size()); | |
187 ASSERT_EQ("moi", args["abc"]); | |
188 ASSERT_EQ("e", trail[0]); | |
189 ASSERT_EQ("f", trail[1]); | |
190 ASSERT_EQ("g", trail[2]); | |
191 | |
192 ASSERT_FALSE(uri.Match(args, trail, "/coucou/moi/f")); | |
193 ASSERT_TRUE(uri.Match(args, trail, "/coucou/moi/d/")); | |
194 ASSERT_FALSE(uri.Match(args, trail, "/a/moi/d")); | |
195 ASSERT_FALSE(uri.Match(args, trail, "/coucou/moi")); | |
966
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
196 |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
197 ASSERT_EQ(3u, uri.GetLevelCount()); |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
198 ASSERT_TRUE(uri.IsUniversalTrailing()); |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
199 |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
200 ASSERT_EQ("coucou", uri.GetLevelName(0)); |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
201 ASSERT_THROW(uri.GetWildcardName(0), OrthancException); |
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 ASSERT_EQ("abc", uri.GetWildcardName(1)); |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
204 ASSERT_THROW(uri.GetLevelName(1), OrthancException); |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
205 |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
206 ASSERT_EQ("d", uri.GetLevelName(2)); |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
207 ASSERT_THROW(uri.GetWildcardName(2), OrthancException); |
209 | 208 } |
209 | |
210 { | |
211 RestApiPath uri("/coucou/{abc}/d"); | |
212 ASSERT_FALSE(uri.Match(args, trail, "/coucou/moi/d/e/f/g")); | |
213 ASSERT_TRUE(uri.Match(args, trail, "/coucou/moi/d")); | |
214 ASSERT_EQ(1u, args.size()); | |
215 ASSERT_EQ(0u, trail.size()); | |
216 ASSERT_EQ("moi", args["abc"]); | |
966
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
217 |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
218 ASSERT_EQ(3u, uri.GetLevelCount()); |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
219 ASSERT_FALSE(uri.IsUniversalTrailing()); |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
220 |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
221 ASSERT_EQ("coucou", uri.GetLevelName(0)); |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
222 ASSERT_THROW(uri.GetWildcardName(0), OrthancException); |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
223 |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
224 ASSERT_EQ("abc", uri.GetWildcardName(1)); |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
225 ASSERT_THROW(uri.GetLevelName(1), OrthancException); |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
226 |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
227 ASSERT_EQ("d", uri.GetLevelName(2)); |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
228 ASSERT_THROW(uri.GetWildcardName(2), OrthancException); |
209 | 229 } |
230 | |
231 { | |
232 RestApiPath uri("/*"); | |
233 ASSERT_TRUE(uri.Match(args, trail, "/a/b/c")); | |
234 ASSERT_EQ(0u, args.size()); | |
235 ASSERT_EQ(3u, trail.size()); | |
236 ASSERT_EQ("a", trail[0]); | |
237 ASSERT_EQ("b", trail[1]); | |
238 ASSERT_EQ("c", trail[2]); | |
966
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
239 |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
240 ASSERT_EQ(0u, uri.GetLevelCount()); |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
241 ASSERT_TRUE(uri.IsUniversalTrailing()); |
209 | 242 } |
243 } | |
966
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
244 |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
245 |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
246 |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
247 |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
248 |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
249 |
968 | 250 static int testValue; |
251 | |
252 template <int value> | |
974 | 253 static void SetValue(RestApiGetCall& get) |
966
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
254 { |
968 | 255 testValue = value; |
966
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
256 } |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
257 |
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
258 |
969 | 259 static bool GetDirectory(Json::Value& target, |
260 RestApiHierarchy& hierarchy, | |
261 const std::string& uri) | |
262 { | |
263 UriComponents p; | |
264 Toolbox::SplitUriComponents(p, uri); | |
265 return hierarchy.GetDirectory(target, p); | |
266 } | |
267 | |
268 | |
978 | 269 |
270 namespace | |
271 { | |
272 class MyVisitor : public RestApiHierarchy::IVisitor | |
273 { | |
274 public: | |
275 virtual bool Visit(const RestApiHierarchy::Resource& resource, | |
276 const UriComponents& uri, | |
1441
f3672356c121
refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1419
diff
changeset
|
277 const IHttpHandler::Arguments& components, |
978 | 278 const UriComponents& trailing) |
279 { | |
280 return resource.Handle(*reinterpret_cast<RestApiGetCall*>(NULL)); | |
281 } | |
282 }; | |
283 } | |
284 | |
285 | |
969 | 286 static bool HandleGet(RestApiHierarchy& hierarchy, |
287 const std::string& uri) | |
288 { | |
289 UriComponents p; | |
290 Toolbox::SplitUriComponents(p, uri); | |
978 | 291 MyVisitor visitor; |
292 return hierarchy.LookupResource(p, visitor); | |
969 | 293 } |
294 | |
295 | |
968 | 296 TEST(RestApi, RestApiHierarchy) |
966
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
297 { |
968 | 298 RestApiHierarchy root; |
970 | 299 root.Register("/hello/world/test", SetValue<1>); |
300 root.Register("/hello/world/test2", SetValue<2>); | |
301 root.Register("/hello/{world}/test3/test4", SetValue<3>); | |
302 root.Register("/hello2/*", SetValue<4>); | |
968 | 303 |
304 Json::Value m; | |
305 root.CreateSiteMap(m); | |
306 std::cout << m; | |
307 | |
308 Json::Value d; | |
969 | 309 ASSERT_FALSE(GetDirectory(d, root, "/hello")); |
968 | 310 |
969 | 311 ASSERT_TRUE(GetDirectory(d, root, "/hello/a")); |
968 | 312 ASSERT_EQ(1u, d.size()); |
313 ASSERT_EQ("test3", d[0].asString()); | |
314 | |
969 | 315 ASSERT_TRUE(GetDirectory(d, root, "/hello/world")); |
968 | 316 ASSERT_EQ(2u, d.size()); |
966
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
317 |
969 | 318 ASSERT_TRUE(GetDirectory(d, root, "/hello/a/test3")); |
968 | 319 ASSERT_EQ(1u, d.size()); |
320 ASSERT_EQ("test4", d[0].asString()); | |
321 | |
1064 | 322 ASSERT_TRUE(GetDirectory(d, root, "/hello/world/test")); |
323 ASSERT_TRUE(GetDirectory(d, root, "/hello/world/test2")); | |
969 | 324 ASSERT_FALSE(GetDirectory(d, root, "/hello2")); |
968 | 325 |
326 testValue = 0; | |
969 | 327 ASSERT_TRUE(HandleGet(root, "/hello/world/test")); |
968 | 328 ASSERT_EQ(testValue, 1); |
969 | 329 ASSERT_TRUE(HandleGet(root, "/hello/world/test2")); |
968 | 330 ASSERT_EQ(testValue, 2); |
969 | 331 ASSERT_TRUE(HandleGet(root, "/hello/b/test3/test4")); |
968 | 332 ASSERT_EQ(testValue, 3); |
969 | 333 ASSERT_FALSE(HandleGet(root, "/hello/b/test3/test")); |
968 | 334 ASSERT_EQ(testValue, 3); |
969 | 335 ASSERT_TRUE(HandleGet(root, "/hello2/a/b")); |
968 | 336 ASSERT_EQ(testValue, 4); |
966
886652370ff2
accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
337 } |