annotate UnitTestsSources/RestApi.cpp @ 802:82b07ab959f4

fix
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 06 May 2014 17:09:32 +0200
parents 203157cb4fde
children 3d6f9b7d0add
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
209
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 #include "gtest/gtest.h"
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 #include <ctype.h>
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 #include <glog/logging.h>
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 using namespace Orthanc;
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
60 TEST(RestApi, ParseCookies)
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
61 {
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
62 HttpHandler::Arguments headers;
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
63 HttpHandler::Arguments cookies;
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
64
332
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 330
diff changeset
65 headers["cookie"] = "a=b;c=d;;;e=f;;g=h;";
330
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
66 HttpHandler::ParseCookies(cookies, headers);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
67 ASSERT_EQ(4u, cookies.size());
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
68 ASSERT_EQ("b", cookies["a"]);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
69 ASSERT_EQ("d", cookies["c"]);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
70 ASSERT_EQ("f", cookies["e"]);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
71 ASSERT_EQ("h", cookies["g"]);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
72
332
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 330
diff changeset
73 headers["cookie"] = " name = value ; name2=value2";
330
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
74 HttpHandler::ParseCookies(cookies, headers);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
75 ASSERT_EQ(2u, cookies.size());
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
76 ASSERT_EQ("value", cookies["name"]);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
77 ASSERT_EQ("value2", cookies["name2"]);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
78
332
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 330
diff changeset
79 headers["cookie"] = " ;;; ";
330
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
80 HttpHandler::ParseCookies(cookies, headers);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
81 ASSERT_EQ(0u, cookies.size());
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
82
332
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 330
diff changeset
83 headers["cookie"] = " ; n=v ;; ";
330
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
84 HttpHandler::ParseCookies(cookies, headers);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
85 ASSERT_EQ(1u, cookies.size());
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
86 ASSERT_EQ("v", cookies["n"]);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
87 }
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
88
209
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 TEST(RestApi, RestApiPath)
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 {
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 RestApiPath::Components args;
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 UriComponents trail;
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 {
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 RestApiPath uri("/coucou/{abc}/d/*");
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 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
97 ASSERT_EQ(1u, args.size());
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 ASSERT_EQ(3u, trail.size());
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 ASSERT_EQ("moi", args["abc"]);
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 ASSERT_EQ("e", trail[0]);
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 ASSERT_EQ("f", trail[1]);
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 ASSERT_EQ("g", trail[2]);
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 ASSERT_FALSE(uri.Match(args, trail, "/coucou/moi/f"));
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 ASSERT_TRUE(uri.Match(args, trail, "/coucou/moi/d/"));
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 ASSERT_FALSE(uri.Match(args, trail, "/a/moi/d"));
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 ASSERT_FALSE(uri.Match(args, trail, "/coucou/moi"));
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 }
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 {
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 RestApiPath uri("/coucou/{abc}/d");
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 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
113 ASSERT_TRUE(uri.Match(args, trail, "/coucou/moi/d"));
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 ASSERT_EQ(1u, args.size());
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 ASSERT_EQ(0u, trail.size());
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 ASSERT_EQ("moi", args["abc"]);
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 }
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 {
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 RestApiPath uri("/*");
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 ASSERT_TRUE(uri.Match(args, trail, "/a/b/c"));
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 ASSERT_EQ(0u, args.size());
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 ASSERT_EQ(3u, trail.size());
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 ASSERT_EQ("a", trail[0]);
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 ASSERT_EQ("b", trail[1]);
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 ASSERT_EQ("c", trail[2]);
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 }