Mercurial > hg > orthanc
annotate UnitTestsSources/RestApi.cpp @ 786:b6d6b65142e8
DicomModification
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 05 May 2014 13:07:10 +0200 |
parents | 203157cb4fde |
children | 3d6f9b7d0add |
rev | line source |
---|---|
209 | 1 #include "gtest/gtest.h" |
2 | |
3 #include <ctype.h> | |
4 #include <glog/logging.h> | |
5 | |
707
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
6 #include "../Core/ChunkedBuffer.h" |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
7 #include "../Core/HttpClient.h" |
209 | 8 #include "../Core/RestApi/RestApi.h" |
221
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
210
diff
changeset
|
9 #include "../Core/Uuid.h" |
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
210
diff
changeset
|
10 #include "../Core/OrthancException.h" |
e7432706b354
accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
210
diff
changeset
|
11 #include "../Core/Compression/ZlibCompressor.h" |
209 | 12 |
13 using namespace Orthanc; | |
14 | |
707
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
15 #if !defined(UNIT_TESTS_WITH_HTTP_CONNEXIONS) |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
16 #error "Please set UNIT_TESTS_WITH_HTTP_CONNEXIONS" |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
17 #endif |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
18 |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
19 TEST(HttpClient, Basic) |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
20 { |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
21 HttpClient c; |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
22 ASSERT_FALSE(c.IsVerbose()); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
23 c.SetVerbose(true); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
24 ASSERT_TRUE(c.IsVerbose()); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
25 c.SetVerbose(false); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
26 ASSERT_FALSE(c.IsVerbose()); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
27 |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
28 #if UNIT_TESTS_WITH_HTTP_CONNEXIONS == 1 |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
29 Json::Value v; |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
30 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
|
31 c.Apply(v); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
32 ASSERT_TRUE(v.isMember("StorageDirectory")); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
33 //ASSERT_EQ(GetLastStatusText()); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
34 |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
35 v = Json::nullValue; |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
36 |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
37 HttpClient cc(c); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
38 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
|
39 cc.Apply(v); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
40 ASSERT_TRUE(v.isMember("LuaScripts")); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
41 #endif |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
42 } |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
43 |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
44 TEST(RestApi, ChunkedBuffer) |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
45 { |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
46 ChunkedBuffer b; |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
47 ASSERT_EQ(0, b.GetNumBytes()); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
48 |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
49 b.AddChunk("hello", 5); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
50 ASSERT_EQ(5, b.GetNumBytes()); |
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 b.AddChunk("world", 5); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
53 ASSERT_EQ(10, b.GetNumBytes()); |
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 std::string s; |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
56 b.Flatten(s); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
57 ASSERT_EQ("helloworld", s); |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
58 } |
203157cb4fde
unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
59 |
330 | 60 TEST(RestApi, ParseCookies) |
61 { | |
62 HttpHandler::Arguments headers; | |
63 HttpHandler::Arguments cookies; | |
64 | |
332 | 65 headers["cookie"] = "a=b;c=d;;;e=f;;g=h;"; |
330 | 66 HttpHandler::ParseCookies(cookies, headers); |
67 ASSERT_EQ(4u, cookies.size()); | |
68 ASSERT_EQ("b", cookies["a"]); | |
69 ASSERT_EQ("d", cookies["c"]); | |
70 ASSERT_EQ("f", cookies["e"]); | |
71 ASSERT_EQ("h", cookies["g"]); | |
72 | |
332 | 73 headers["cookie"] = " name = value ; name2=value2"; |
330 | 74 HttpHandler::ParseCookies(cookies, headers); |
75 ASSERT_EQ(2u, cookies.size()); | |
76 ASSERT_EQ("value", cookies["name"]); | |
77 ASSERT_EQ("value2", cookies["name2"]); | |
78 | |
332 | 79 headers["cookie"] = " ;;; "; |
330 | 80 HttpHandler::ParseCookies(cookies, headers); |
81 ASSERT_EQ(0u, cookies.size()); | |
82 | |
332 | 83 headers["cookie"] = " ; n=v ;; "; |
330 | 84 HttpHandler::ParseCookies(cookies, headers); |
85 ASSERT_EQ(1u, cookies.size()); | |
86 ASSERT_EQ("v", cookies["n"]); | |
87 } | |
88 | |
209 | 89 TEST(RestApi, RestApiPath) |
90 { | |
91 RestApiPath::Components args; | |
92 UriComponents trail; | |
93 | |
94 { | |
95 RestApiPath uri("/coucou/{abc}/d/*"); | |
96 ASSERT_TRUE(uri.Match(args, trail, "/coucou/moi/d/e/f/g")); | |
97 ASSERT_EQ(1u, args.size()); | |
98 ASSERT_EQ(3u, trail.size()); | |
99 ASSERT_EQ("moi", args["abc"]); | |
100 ASSERT_EQ("e", trail[0]); | |
101 ASSERT_EQ("f", trail[1]); | |
102 ASSERT_EQ("g", trail[2]); | |
103 | |
104 ASSERT_FALSE(uri.Match(args, trail, "/coucou/moi/f")); | |
105 ASSERT_TRUE(uri.Match(args, trail, "/coucou/moi/d/")); | |
106 ASSERT_FALSE(uri.Match(args, trail, "/a/moi/d")); | |
107 ASSERT_FALSE(uri.Match(args, trail, "/coucou/moi")); | |
108 } | |
109 | |
110 { | |
111 RestApiPath uri("/coucou/{abc}/d"); | |
112 ASSERT_FALSE(uri.Match(args, trail, "/coucou/moi/d/e/f/g")); | |
113 ASSERT_TRUE(uri.Match(args, trail, "/coucou/moi/d")); | |
114 ASSERT_EQ(1u, args.size()); | |
115 ASSERT_EQ(0u, trail.size()); | |
116 ASSERT_EQ("moi", args["abc"]); | |
117 } | |
118 | |
119 { | |
120 RestApiPath uri("/*"); | |
121 ASSERT_TRUE(uri.Match(args, trail, "/a/b/c")); | |
122 ASSERT_EQ(0u, args.size()); | |
123 ASSERT_EQ(3u, trail.size()); | |
124 ASSERT_EQ("a", trail[0]); | |
125 ASSERT_EQ("b", trail[1]); | |
126 ASSERT_EQ("c", trail[2]); | |
127 } | |
128 } |