Branch data Line data Source code
1 : : // Copyright (c) 2012-present The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <rest.h>
6 : : #include <test/util/common.h>
7 : : #include <test/util/setup_common.h>
8 : :
9 : : #include <boost/test/unit_test.hpp>
10 : :
11 : : #include <string>
12 : :
13 : : BOOST_FIXTURE_TEST_SUITE(rest_tests, BasicTestingSetup)
14 : :
15 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(test_query_string)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
16 : : {
17 [ + - ]: 1 : std::string param;
18 : 1 : RESTResponseFormat rf;
19 : : // No query string
20 [ + - + - ]: 1 : rf = ParseDataFormat(param, "/rest/endpoint/someresource.json");
21 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(param, "/rest/endpoint/someresource");
22 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(rf, RESTResponseFormat::JSON);
23 : :
24 : : // Query string with single parameter
25 [ + - + - ]: 1 : rf = ParseDataFormat(param, "/rest/endpoint/someresource.bin?p1=v1");
26 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(param, "/rest/endpoint/someresource");
27 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(rf, RESTResponseFormat::BINARY);
28 : :
29 : : // Query string with multiple parameters
30 [ + - + - ]: 1 : rf = ParseDataFormat(param, "/rest/endpoint/someresource.hex?p1=v1&p2=v2");
31 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(param, "/rest/endpoint/someresource");
32 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(rf, RESTResponseFormat::HEX);
33 : :
34 : : // Incorrectly formed query string will not be handled
35 [ + - + - ]: 1 : rf = ParseDataFormat(param, "/rest/endpoint/someresource.json&p1=v1");
36 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(param, "/rest/endpoint/someresource.json&p1=v1");
37 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(rf, RESTResponseFormat::UNDEF);
38 : :
39 : : // Omitted data format with query string should return UNDEF and hide query string
40 [ + - + - ]: 1 : rf = ParseDataFormat(param, "/rest/endpoint/someresource?p1=v1");
41 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(param, "/rest/endpoint/someresource");
42 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(rf, RESTResponseFormat::UNDEF);
43 : :
44 : : // Data format specified after query string
45 [ + - + - ]: 1 : rf = ParseDataFormat(param, "/rest/endpoint/someresource?p1=v1.json");
46 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(param, "/rest/endpoint/someresource");
47 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(rf, RESTResponseFormat::UNDEF);
48 : 1 : }
49 : : BOOST_AUTO_TEST_SUITE_END()
|