File: RpcResponseTest.cc

package info (click to toggle)
aria2 1.18.8-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 19,392 kB
  • ctags: 16,036
  • sloc: cpp: 115,823; sh: 12,015; ansic: 7,394; makefile: 1,445; ruby: 462; python: 216; xml: 176; asm: 58; sed: 16
file content (132 lines) | stat: -rw-r--r-- 4,626 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "RpcResponse.h"

#include <cppunit/extensions/HelperMacros.h>

namespace aria2 {

namespace rpc {

class RpcResponseTest:public CppUnit::TestFixture {
  CPPUNIT_TEST_SUITE(RpcResponseTest);
  CPPUNIT_TEST(testToJson);
#ifdef ENABLE_XML_RPC
  CPPUNIT_TEST(testToXml);
#endif // ENABLE_XML_RPC
  CPPUNIT_TEST_SUITE_END();
public:
  void testToJson();
#ifdef ENABLE_XML_RPC
  void testToXml();
#endif // ENABLE_XML_RPC
};

CPPUNIT_TEST_SUITE_REGISTRATION(RpcResponseTest);

void RpcResponseTest::testToJson()
{
  std::vector<RpcResponse> results;
  {
    auto param = List::g();
    param->append(Integer::g(1));
    RpcResponse res(0, RpcResponse::AUTHORIZED, std::move(param),
                    String::g("9"));
    results.push_back(std::move(res));
    std::string s = toJson(results.back(), "", false);
    CPPUNIT_ASSERT_EQUAL(std::string("{\"id\":\"9\","
                                     "\"jsonrpc\":\"2.0\","
                                     "\"result\":[1]}"),
                         s);
    // with callback
    s = toJson(results.back(), "cb", false);
    CPPUNIT_ASSERT_EQUAL(std::string("cb({\"id\":\"9\","
                                     "\"jsonrpc\":\"2.0\","
                                     "\"result\":[1]})"),
                         s);
  }
  {
    // error response
    auto param = Dict::g();
    param->put("code", Integer::g(1));
    param->put("message", "HELLO ERROR");
    RpcResponse res(1, RpcResponse::AUTHORIZED, std::move(param), Null::g());
    results.push_back(std::move(res));
    std::string s = toJson(results.back(), "", false);
    CPPUNIT_ASSERT_EQUAL(std::string("{\"id\":null,"
                                     "\"jsonrpc\":\"2.0\","
                                     "\"error\":{\"code\":1,"
                                     "\"message\":\"HELLO ERROR\"}"
                                     "}"),
                         s);
    // with callback
    s = toJson(results.back(), "cb", false);
    CPPUNIT_ASSERT_EQUAL(std::string("cb({\"id\":null,"
                                     "\"jsonrpc\":\"2.0\","
                                     "\"error\":{\"code\":1,"
                                     "\"message\":\"HELLO ERROR\"}"
                                     "})"),
                         s);
  }
  {
    // batch response
    std::string s = toJsonBatch(results, "", false);
    CPPUNIT_ASSERT_EQUAL(std::string("["
                                     "{\"id\":\"9\","
                                     "\"jsonrpc\":\"2.0\","
                                     "\"result\":[1]},"
                                     "{\"id\":null,"
                                     "\"jsonrpc\":\"2.0\","
                                     "\"error\":{\"code\":1,"
                                     "\"message\":\"HELLO ERROR\"}"
                                     "}"
                                     "]"),
                         s);
    // with callback
    s = toJsonBatch(results, "cb", false);
    CPPUNIT_ASSERT_EQUAL(std::string("cb(["
                                     "{\"id\":\"9\","
                                     "\"jsonrpc\":\"2.0\","
                                     "\"result\":[1]},"
                                     "{\"id\":null,"
                                     "\"jsonrpc\":\"2.0\","
                                     "\"error\":{\"code\":1,"
                                     "\"message\":\"HELLO ERROR\"}"
                                     "}"
                                     "])"),
                         s);
  }
}

#ifdef ENABLE_XML_RPC
void RpcResponseTest::testToXml()
{
  auto param = Dict::g();
  param->put("faultCode", Integer::g(1));
  param->put("faultString", "No such method: make.hamburger");
  RpcResponse res(1, RpcResponse::AUTHORIZED, std::move(param), Null::g());
  std::string s = toXml(res, false);
  CPPUNIT_ASSERT_EQUAL
    (std::string("<?xml version=\"1.0\"?>"
                 "<methodResponse>"
                 "<fault>"
                 "<value>"
                 "<struct>"
                 "<member>"
                 "<name>faultCode</name><value><int>1</int></value>"
                 "</member>"
                 "<member>"
                 "<name>faultString</name>"
                 "<value>"
                 "<string>No such method: make.hamburger</string>"
                 "</value>"
                 "</member>"
                 "</struct>"
                 "</value>"
                 "</fault>"
                 "</methodResponse>"),
     s);
}
#endif // ENABLE_XML_RPC

} // namespace rpc

} // namespace aria2