File: client_construction.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 (227 lines) | stat: -rw-r--r-- 7,240 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/***
 * Copyright (C) Microsoft. All rights reserved.
 * Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
 *
 * =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
 *
 * client_construction.cpp
 *
 * Tests cases for covering creating http_clients.
 *
 * =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 ****/

#include "stdafx.h"

#include <fstream>

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

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

namespace tests
{
namespace functional
{
namespace http
{
namespace client
{
SUITE(client_construction)
{
    // Tests using different types of strings to construct an http_client.
    TEST_FIXTURE(uri_address, string_types)
    {
        // The goal of this test case is to make sure we can compile,
        // if the URI class doesn't have the proper constructors it won't.
        // So we don't need to actually do a request.
        http_client c1(U("http://localhost:4567/"));
        http_client c3(utility::string_t(U("http://localhost:4567/")));
    }

    // Tests different variations on specifying the URI in http_client constructor.
    TEST_FIXTURE(uri_address, different_uris)
    {
        const utility::string_t paths[] = {U(""), U("/"), U("/toplevel/nested"), U("/toplevel/nested/")};
        const utility::string_t expected_paths[] = {U("/"), U("/"), U("/toplevel/nested"), U("/toplevel/nested/")};
        const size_t num_paths = sizeof(paths) / sizeof(paths[0]);
        for (size_t i = 0; i < num_paths; ++i)
        {
            uri address(U("http://localhost:55678") + paths[i]);
            test_http_server::scoped_server scoped(address);
            http_client client(address);
            test_connection(scoped.server(), &client, expected_paths[i]);
        }
    }

    // Helper function verifies that when constructing an http_client with given
    // URI std::invalid_argument is thrown.
    static void verify_client_invalid_argument(const uri& address)
    {
        try
        {
            http_client client(address);
            VERIFY_IS_TRUE(false);
        }
        catch (std::invalid_argument&)
        {
            // expected
        }
    }

    TEST_FIXTURE(uri_address, client_construction_error_cases)
    {
        uri address(U("nothttp://localhost:34567/"));

        // Invalid scheme.
        verify_client_invalid_argument(address);

        // empty host.
        address = uri(U("http://:34567/"));
        verify_client_invalid_argument(address);
    }

    TEST_FIXTURE(uri_address, client_construction_no_scheme)
    {
        uri address(U("//localhost:34568/p/g"));
        test_http_server::scoped_server scoped(m_uri);

        http_client client(address);
        test_connection(scoped.server(), &client, U("/p/g"));
    }

    TEST_FIXTURE(uri_address, copy_assignment)
    {
        test_http_server::scoped_server scoped(m_uri);

        // copy constructor
        http_client original(m_uri);
        http_client new_client(original);
        test_connection(scoped.server(), &new_client, U("/"));
        test_connection(scoped.server(), &original, U("/"));

        // assignment
        http_client new_client2(U("http://bad:-1"));
        new_client2 = original;
        test_connection(scoped.server(), &new_client2, U("/"));
        test_connection(scoped.server(), &original, U("/"));
    }

    TEST_FIXTURE(uri_address, move_not_init)
    {
        test_http_server::scoped_server scoped(m_uri);

        // move constructor
        http_client original(m_uri);
        http_client new_client = std::move(original);
        test_connection(scoped.server(), &new_client, U("/"));

        // move assignment
        original = http_client(m_uri);
        test_connection(scoped.server(), &original, U("/"));
    }

    TEST_FIXTURE(uri_address, move_init)
    {
        test_http_server::scoped_server scoped(m_uri);

        // move constructor
        http_client original(m_uri);
        test_connection(scoped.server(), &original, U("/"));
        http_client new_client = std::move(original);
        test_connection(scoped.server(), &new_client, U("/"));

        // move assignment
        original = http_client(m_uri);
        test_connection(scoped.server(), &original, U("/"));
    }

    // Verify that we can read the config from the http_client
    TEST_FIXTURE(uri_address, get_client_config)
    {
        test_http_server::scoped_server scoped(m_uri);

        http_client_config config;

        VERIFY_ARE_EQUAL(config.chunksize(), 64 * 1024);
        config.set_chunksize(1024);
        VERIFY_ARE_EQUAL(config.chunksize(), 1024);

        utility::seconds timeout(100);
        config.set_timeout(timeout);
        http_client client(m_uri, config);

        const http_client_config& config2 = client.client_config();
        VERIFY_ARE_EQUAL(config2.timeout().count(), timeout.count());
        std::chrono::milliseconds milli_timeout = config2.timeout();
        VERIFY_ARE_EQUAL(milli_timeout.count(), std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count());
        auto micro_timeout = config.timeout<std::chrono::microseconds>();
        VERIFY_ARE_EQUAL(micro_timeout.count(), std::chrono::duration_cast<std::chrono::microseconds>(timeout).count());

        VERIFY_ARE_EQUAL(config2.chunksize(), 1024);
    }

    // Verify that we can get the baseuri from http_client constructors
    TEST_FIXTURE(uri_address, BaseURI_test)
    {
        http_client baseclient1(m_uri);
        VERIFY_ARE_EQUAL(baseclient1.base_uri(), m_uri);

        http_client_config config;
        http_client baseclient2(m_uri, config);
        VERIFY_ARE_EQUAL(baseclient2.base_uri(), m_uri);
    }

#if !defined(_WIN32) && !defined(__cplusplus_winrt) || defined(CPPREST_FORCE_HTTP_CLIENT_ASIO)

    // Verify that the callback of sslcontext is called for HTTPS
    TEST_FIXTURE(uri_address, ssl_context_callback_https)
    {
        http_client_config config;
        bool called = false;

        config.set_ssl_context_callback([&called](boost::asio::ssl::context& ctx) { called = true; });

        http_client client(U("https://www.google.com/"), config);

        try
        {
            client.request(methods::GET, U("/")).get();
        }
        catch (...)
        {
        }

        VERIFY_IS_TRUE(called, "The sslcontext options is not called for HTTPS protocol");
    }

    // Verify that the callback of sslcontext is not called for HTTP
    TEST_FIXTURE(uri_address, ssl_context_callback_http)
    {
        http_client_config config;
        bool called = false;

        config.set_ssl_context_callback([&called](boost::asio::ssl::context& ctx) { called = true; });

        http_client client(U("http://www.google.com/"), config);

        try
        {
            client.request(methods::GET, U("/")).get();
        }
        catch (...)
        {
        }

        VERIFY_IS_FALSE(called, "The sslcontext options is called for HTTP protocol");
    }

#endif

} // SUITE(client_construction)

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