File: request_uri_tests.cpp

package info (click to toggle)
cpprest 2.10.19-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,916 kB
  • sloc: cpp: 71,086; sh: 275; makefile: 170; javascript: 147
file content (176 lines) | stat: -rw-r--r-- 6,910 bytes parent folder | download | duplicates (2)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/***
 * Copyright (C) Microsoft. All rights reserved.
 * Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
 *
 * =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
 *
 * request_uri_tests.cpp
 *
 * Tests cases covering various kinds of request URIs with http_client.
 *
 * =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 ****/

#include "stdafx.h"

using namespace web::http;
using namespace web::http::client;

using namespace tests::functional::http::utilities;

namespace tests
{
namespace functional
{
namespace http
{
namespace client
{
SUITE(request_uri_tests)
{
    // Tests path specified in requests with non-empty base path in client constructor.
    TEST_FIXTURE(uri_address, path_non_empty_ctor)
    {
        uri address(U("http://localhost:45678/base_path/"));

        // Path not starting with '/'.
        {
            test_http_server::scoped_server scoped(address);
            http_client client(address);
            test_connection(scoped.server(), &client, U("next_level"), U("/base_path/next_level"));
        }

        // Path starting with '/'.
        {
            test_http_server::scoped_server scoped(address);
            http_client client(address);
            test_connection(scoped.server(), &client, U("/next_level"), U("/base_path/next_level"));
        }
    }

    // Tests path specified in requests with empty base path in client constructor.
    TEST_FIXTURE(uri_address, path_empty_ctor)
    {
        uri address(U("http://localhost:45678"));

        // NON empty path.
        {
            test_http_server::scoped_server scoped(address);
            http_client client(address);
            test_connection(scoped.server(), &client, U("next_level"), U("/next_level"));
        }

        // Request path of '*'
        {
            test_http_server::scoped_server scoped(address);
            http_client client(address);
            test_connection(scoped.server(), &client, U("*"), U("/*"));
        }

        // Empty base of '/' with request path starting with '/'.
        address = uri(U("http://localhost:45678/"));
        {
            test_http_server::scoped_server scoped(address);
            http_client client(address);
            test_connection(scoped.server(), &client, U("/hehehe"), U("/hehehe"));
        }
    }

    TEST_FIXTURE(uri_address, with_query_fragment)
    {
        test_http_server::scoped_server scoped(m_uri);
        http_client client(m_uri);

        // query
        test_connection(scoped.server(), &client, U("/hehehe?key1=value2&"), U("/hehehe?key1=value2&"));

        // fragment

        // WinRT implementation percent encodes the '#'.
        utility::string_t expected_value = U("/heheh?key1=value2#fragment");
#if defined(__cplusplus_winrt) || defined(CPPREST_FORCE_HTTP_CLIENT_WINHTTPPAL)
        expected_value = percent_encode_pound(expected_value);
#endif

        test_connection(scoped.server(), &client, U("/heheh?key1=value2#fragment"), expected_value);
    }

    TEST_FIXTURE(uri_address, uri_encoding)
    {
        test_http_server::scoped_server scoped(m_uri);
        test_http_server* p_server = scoped.server();
        http_client client(m_uri);

        // try with encoding string.
        http_request msg(methods::GET);
        msg.set_request_uri(U("/path1!!alreadyencoded"));
        p_server->next_request().then([&](test_request* p_request) {
            http_asserts::assert_test_request_equals(p_request, methods::GET, U("/path1!!alreadyencoded"));
            http_asserts::assert_test_request_contains_headers(p_request, msg.headers());
            p_request->reply(200);
        });
        http_asserts::assert_response_equals(client.request(msg).get(), status_codes::OK);

        // verify encoding actual happens with plain.
        msg = http_request(methods::GET);
        msg.set_request_uri(web::http::uri::encode_uri(U("/path1 /encode")));
        VERIFY_ARE_EQUAL(U("/path1%20/encode"), msg.relative_uri().to_string());
        p_server->next_request().then([&](test_request* p_request) {
            http_asserts::assert_test_request_equals(p_request, methods::GET, U("/path1%20/encode"));
            http_asserts::assert_test_request_contains_headers(p_request, msg.headers());
            p_request->reply(200);
        });
        http_asserts::assert_response_equals(client.request(msg).get(), status_codes::OK);
    }

    // Tests combining case URI query/fragments with relative URI query/fragments.
    TEST_FIXTURE(uri_address, append_query_fragment)
    {
        // Try with query.
        const utility::string_t base_uri_with_query =
            web::http::uri_builder(m_uri).append(U("/path1?key1=value1")).to_string();
        {
            test_http_server::scoped_server scoped(m_uri);
            test_http_server* p_server = scoped.server();
            http_client client(base_uri_with_query);

            p_server->next_request().then([&](test_request* p_request) {
                // WinRT implementation percent encodes the '#'.
                utility::string_t expected_value = U("/path1?key1=value1&key2=value2#frag");
#if defined(__cplusplus_winrt) || defined(CPPREST_FORCE_HTTP_CLIENT_WINHTTPPAL)
                expected_value = percent_encode_pound(expected_value);
#endif
                http_asserts::assert_test_request_equals(p_request, methods::GET, expected_value);
                p_request->reply(200);
            });
            http_asserts::assert_response_equals(client.request(methods::GET, U("?key2=value2#frag")).get(),
                                                 status_codes::OK);
        }

        // Try with fragment.
        const utility::string_t base_uri_with_frag(m_uri.to_string() + U("path1#fragment"));
        {
            test_http_server::scoped_server scoped(m_uri);
            test_http_server* p_server = scoped.server();
            http_client client(base_uri_with_frag);

            p_server->next_request().then([&](test_request* p_request) {
                // WinRT implementation percent encodes the '#'.
                utility::string_t expected_value = U("/path1/path2?key2=value2#fragmentfg2");
#if defined(__cplusplus_winrt) || defined(CPPREST_FORCE_HTTP_CLIENT_WINHTTPPAL)
                expected_value = percent_encode_pound(expected_value);
#endif
                http_asserts::assert_test_request_equals(p_request, methods::GET, expected_value);
                p_request->reply(200);
            });
            http_asserts::assert_response_equals(client.request(methods::GET, U("path2?key2=value2#fg2")).get(),
                                                 status_codes::OK);
        }
    }

} // SUITE(request_uri_tests)

} // namespace client
} // namespace http
} // namespace functional
} // namespace tests