comparison UnitTests/RestApi.cpp @ 209:9960642f0f45

abstraction of RestApi
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 28 Nov 2012 17:22:07 +0100
parents
children 96b7918a6a18
comparison
equal deleted inserted replaced
208:de640de989b8 209:9960642f0f45
1 #include "gtest/gtest.h"
2
3 #include <ctype.h>
4 #include <glog/logging.h>
5
6 #include "../Core/RestApi/RestApi.h"
7
8 using namespace Orthanc;
9
10 TEST(RestApi, RestApiPath)
11 {
12 RestApiPath::Components args;
13 UriComponents trail;
14
15 {
16 RestApiPath uri("/coucou/{abc}/d/*");
17 ASSERT_TRUE(uri.Match(args, trail, "/coucou/moi/d/e/f/g"));
18 ASSERT_EQ(1u, args.size());
19 ASSERT_EQ(3u, trail.size());
20 ASSERT_EQ("moi", args["abc"]);
21 ASSERT_EQ("e", trail[0]);
22 ASSERT_EQ("f", trail[1]);
23 ASSERT_EQ("g", trail[2]);
24
25 ASSERT_FALSE(uri.Match(args, trail, "/coucou/moi/f"));
26 ASSERT_TRUE(uri.Match(args, trail, "/coucou/moi/d/"));
27 ASSERT_FALSE(uri.Match(args, trail, "/a/moi/d"));
28 ASSERT_FALSE(uri.Match(args, trail, "/coucou/moi"));
29 }
30
31 {
32 RestApiPath uri("/coucou/{abc}/d");
33 ASSERT_FALSE(uri.Match(args, trail, "/coucou/moi/d/e/f/g"));
34 ASSERT_TRUE(uri.Match(args, trail, "/coucou/moi/d"));
35 ASSERT_EQ(1u, args.size());
36 ASSERT_EQ(0u, trail.size());
37 ASSERT_EQ("moi", args["abc"]);
38 }
39
40 {
41 RestApiPath uri("/*");
42 ASSERT_TRUE(uri.Match(args, trail, "/a/b/c"));
43 ASSERT_EQ(0u, args.size());
44 ASSERT_EQ(3u, trail.size());
45 ASSERT_EQ("a", trail[0]);
46 ASSERT_EQ("b", trail[1]);
47 ASSERT_EQ("c", trail[2]);
48 }
49 }
50
51
52
53
54 #include "../Core/HttpServer/MongooseServer.h"
55
56 struct Tutu : public IDynamicObject
57 {
58 static void Toto(RestApi::GetCall& call)
59 {
60 printf("DONE\n");
61 Json::Value a = Json::objectValue;
62 a["Tutu"] = "Toto";
63 a["Youpie"] = call.GetArgument("coucou", "nope");
64 a["Toto"] = call.GetUriComponent("test", "nope");
65 call.GetOutput().AnswerJson(a);
66 }
67 };
68
69
70
71 TEST(RestApi, Tutu)
72 {
73 MongooseServer httpServer;
74 httpServer.SetPortNumber(8042);
75 httpServer.Start();
76
77 RestApi* api = new RestApi;
78 httpServer.RegisterHandler(api);
79 api->Register("/coucou/{test}/a/*", Tutu::Toto);
80
81 httpServer.Start();
82 /*LOG(WARNING) << "REST has started";
83 Toolbox::ServerBarrier();*/
84 }