File: JsonTest.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 (108 lines) | stat: -rw-r--r-- 3,184 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
#include "json.h"

#include <cppunit/extensions/HelperMacros.h>

#include "RecoverableException.h"
#include "util.h"
#include "array_fun.h"
#include "base64.h"

namespace aria2 {

class JsonTest:public CppUnit::TestFixture {

  CPPUNIT_TEST_SUITE(JsonTest);
  CPPUNIT_TEST(testEncode);
  CPPUNIT_TEST(testDecodeGetParams);
  CPPUNIT_TEST_SUITE_END();
private:

public:
  void testEncode();
  void testDecodeGetParams();
};

CPPUNIT_TEST_SUITE_REGISTRATION( JsonTest );

void JsonTest::testEncode()
{
  {
    auto dict = Dict::g();
    dict->put("name", String::g("aria2"));
    dict->put("loc", Integer::g(80000));
    auto files = List::g();
    files->append(String::g("aria2c"));
    dict->put("files", std::move(files));
    auto attrs = Dict::g();
    attrs->put("license", String::g("GPL"));
    dict->put("attrs", std::move(attrs));

    CPPUNIT_ASSERT_EQUAL(std::string("{\"attrs\":{\"license\":\"GPL\"},"
                                     "\"files\":[\"aria2c\"],"
                                     "\"loc\":80000,"
                                     "\"name\":\"aria2\"}"),
                         json::encode(dict.get()));
  }
  {
    auto list = List::g();
    list->append("\"\\/\b\f\n\r\t");
    CPPUNIT_ASSERT_EQUAL(std::string("[\"\\\"\\\\\\/\\b\\f\\n\\r\\t\"]"),
                         json::encode(list.get()));
  }
  {
    auto list = List::g();
    std::string s;
    s += 0x1Fu;
    list->append(s);
    CPPUNIT_ASSERT_EQUAL(std::string("[\"\\u001F\"]"),
                         json::encode(list.get()));
  }
  {
    auto list = List::g();
    list->append(Bool::gTrue());
    list->append(Bool::gFalse());
    list->append(Null::g());
    CPPUNIT_ASSERT_EQUAL(std::string("[true,false,null]"),
                         json::encode(list.get()));
  }
}

void JsonTest::testDecodeGetParams()
{
  {
    std::string s = "[1,2,3]";
    std::string param = util::percentEncode(base64::encode(s.begin(), s.end()));
    std::string query = "?params=";
    query += param;
    query += '&';
    query += "method=sum&";
    query += "id=300&";
    query += "jsoncallback=cb";
    json::JsonGetParam gparam = json::decodeGetParams(query);
    CPPUNIT_ASSERT_EQUAL(std::string("{\"method\":\"sum\","
                                     "\"id\":\"300\","
                                     "\"params\":[1,2,3]}"),
                         gparam.request);
    CPPUNIT_ASSERT_EQUAL(std::string("cb"), gparam.callback);
  }
  {
    std::string s = "[{}]";
    std::string query = "?params=";
    query += util::percentEncode(base64::encode(s.begin(), s.end()));
    query += '&';
    query += "jsoncallback=cb";
    json::JsonGetParam gparam = json::decodeGetParams(query);
    CPPUNIT_ASSERT_EQUAL(std::string("[{}]"), gparam.request);
    CPPUNIT_ASSERT_EQUAL(std::string("cb"), gparam.callback);
  }
  {
    std::string query = "?method=sum&id=300";
    json::JsonGetParam gparam = json::decodeGetParams(query);
    CPPUNIT_ASSERT_EQUAL(std::string("{\"method\":\"sum\","
                                     "\"id\":\"300\"}"),
                         gparam.request);
    CPPUNIT_ASSERT_EQUAL(std::string(), gparam.callback);
  }
}

} // namespace aria2