File: header_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 (405 lines) | stat: -rw-r--r-- 15,026 bytes parent folder | download | duplicates (3)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/***
 * Copyright (C) Microsoft. All rights reserved.
 * Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
 *
 * =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
 *
 * Tests cases for http_headers.
 *
 * =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 ****/

#include "stdafx.h"

#include "cpprest/details/http_helpers.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(outside_tests)
{
    TEST_FIXTURE(uri_address, request_headers)
    {
        test_http_server::scoped_server scoped(m_uri);
        test_http_server* p_server = scoped.server();
        http_client client(m_uri);

        http_request msg(methods::POST);

#ifndef __cplusplus_winrt
        // The WinRT-based HTTP stack does not support headers that have no
        // value, which means that there is no point in making this particular
        // header test, it is an unsupported feature on WinRT.
        msg.headers().add(U("HEHE"), U(""));
#endif

        msg.headers().add(U("MyHeader"), U("hehe;blach"));
        msg.headers().add(U("Yo1"), U("You, Too"));
        msg.headers().add(U("Yo2"), U("You2"));
        msg.headers().add(U("Yo3"), U("You3"));
        msg.headers().add(U("Yo4"), U("You4"));
        msg.headers().add(U("Yo5"), U("You5"));
        msg.headers().add(U("Yo6"), U("You6"));
        msg.headers().add(U("Yo7"), U("You7"));
        msg.headers().add(U("Yo8"), U("You8"));
        msg.headers().add(U("Yo9"), U("You9"));
        msg.headers().add(U("Yo10"), U("You10"));
        msg.headers().add(U("Yo11"), U("You11"));
        msg.headers().add(U("Accept"), U("text/plain"));
        VERIFY_ARE_EQUAL(U("You5"), msg.headers()[U("Yo5")]);
        p_server->next_request().then([&](test_request* p_request) {
            http_asserts::assert_test_request_equals(p_request, methods::POST, U("/"));
            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);
    }

    TEST_FIXTURE(uri_address, field_name_casing)
    {
        test_http_server::scoped_server scoped(m_uri);
        http_client client(m_uri);
        const method mtd = methods::GET;
        const utility::string_t field_name1 = U("CustomHeader");
        const utility::string_t field_name2 = U("CUSTOMHEADER");
        const utility::string_t field_name3 = U("CuSTomHEAdeR");
        const utility::string_t value1 = U("value1");
        const utility::string_t value2 = U("value2");
        const utility::string_t value3 = U("value3");

        http_request msg(mtd);
        msg.headers()[field_name1] = value1;
        msg.headers()[field_name2].append(U(", ") + value2);
        msg.headers()[field_name3].append(U(", ") + value3);
        scoped.server()->next_request().then([&](test_request* p_request) {
            http_asserts::assert_test_request_equals(p_request, mtd, U("/"));
            std::map<utility::string_t, utility::string_t> expected_headers;
            expected_headers[field_name1] = value1 + U(", ") + value2 + U(", ") + value3;
            http_asserts::assert_test_request_contains_headers(p_request, expected_headers);
            p_request->reply(200);
        });
        http_asserts::assert_response_equals(client.request(msg).get(), status_codes::OK);
    }

    TEST_FIXTURE(uri_address, field_name_duplicate)
    {
        test_http_server::scoped_server scoped(m_uri);
        http_client client(m_uri);
        const method mtd = methods::GET;
        const utility::string_t field_name1 = U("CUSTOMHEADER");
        const utility::string_t value1 = U("value1");
        const utility::string_t value2 = U("value2");

        http_request msg(mtd);
        msg.headers().add(field_name1, value1);
        msg.headers().add(field_name1, value2);
        scoped.server()->next_request().then([&](test_request* p_request) {
            http_asserts::assert_test_request_equals(p_request, mtd, U("/"));
            std::map<utility::string_t, utility::string_t> expected_headers;
            expected_headers[field_name1] = value1 + U(", ") + value2;
            http_asserts::assert_test_request_contains_headers(p_request, expected_headers);
            p_request->reply(200);
        });
        http_asserts::assert_response_equals(client.request(msg).get(), status_codes::OK);
    }

    TEST_FIXTURE(uri_address, field_name_no_multivalue_allowed)
    {
        test_http_server::scoped_server scoped(m_uri);
        http_client client(m_uri);
        const method mtd = methods::GET;

        http_request msg(mtd);

        msg.headers().set_content_type(web::http::details::mime_types::text_plain);
        msg.headers().set_content_type(web::http::details::mime_types::application_json);

        scoped.server()->next_request().then([&](test_request* p_request) {
            http_asserts::assert_test_request_equals(p_request, mtd, U("/"));
            std::map<utility::string_t, utility::string_t> expected_headers;
            expected_headers[U("Content-Type")] = web::http::details::mime_types::application_json;
            http_asserts::assert_test_request_contains_headers(p_request, expected_headers);
            p_request->reply(200);
        });
        http_asserts::assert_response_equals(client.request(msg).get(), status_codes::OK);
    }
    TEST_FIXTURE(uri_address, copy_move)
    {
        // copy constructor
        http_headers h1;
        h1.add(U("key1"), U("key2"));
        http_headers h2(h1);
        http_asserts::assert_http_headers_equals(h1, h2);

        // move constructor
        http_headers h3(std::move(h1));
        VERIFY_ARE_EQUAL(1u, h3.size());
        VERIFY_ARE_EQUAL(U("key2"), h3[U("key1")]);

        // assignment operator
        h1 = h3;
        VERIFY_ARE_EQUAL(1u, h1.size());
        VERIFY_ARE_EQUAL(U("key2"), h1[U("key1")]);
        http_asserts::assert_http_headers_equals(h1, h3);

        // move assignment operator
        h1 = http_headers();
        h1 = std::move(h2);
        VERIFY_ARE_EQUAL(1u, h1.size());
        VERIFY_ARE_EQUAL(U("key2"), h1[U("key1")]);
    }

    TEST_FIXTURE(uri_address, match_types)
    {
        // wchar
        http_headers h1;
        h1[U("key1")] = U("string");
        utility::char_t buf[12];
        VERIFY_IS_TRUE(h1.match(U("key1"), buf));
        VERIFY_ARE_EQUAL(U("string"), utility::string_t(buf));

        // utility::string_t
        utility::string_t wstr;
        VERIFY_IS_TRUE(h1.match(U("key1"), wstr));
        VERIFY_ARE_EQUAL(U("string"), wstr);

        // int
        h1[U("key2")] = U("22");
        int i;
        VERIFY_IS_TRUE(h1.match(U("key2"), i));
        VERIFY_ARE_EQUAL(22, i);

        // unsigned long
        unsigned long l;
        VERIFY_IS_TRUE(h1.match(U("key2"), l));
        VERIFY_ARE_EQUAL(22ul, l);
    }

    TEST_FIXTURE(uri_address, match_edge_cases)
    {
        // match with empty string
        http_headers h;
        h[U("here")] = U("");
        utility::string_t value(U("k"));
        VERIFY_IS_TRUE(h.match(U("HeRE"), value));
        VERIFY_ARE_EQUAL(U(""), value);

        // match with string containing spaces
        h.add(U("blah"), U("spaces ss"));
        VERIFY_IS_TRUE(h.match(U("blah"), value));
        VERIFY_ARE_EQUAL(U("spaces ss"), value);

        // match failing
        value = utility::string_t();
        VERIFY_IS_FALSE(h.match(U("hahah"), value));
        VERIFY_ARE_EQUAL(U(""), value);
    }

    TEST_FIXTURE(uri_address, headers_find)
    {
        // Find when empty.
        http_headers h;
        VERIFY_ARE_EQUAL(h.end(), h.find(U("key1")));

        // Find that exists.
        h[U("key1")] = U("yes");
        VERIFY_ARE_EQUAL(U("yes"), h.find(U("key1"))->second);

        // Find that doesn't exist.
        VERIFY_ARE_EQUAL(h.end(), h.find(U("key2")));
    }

    TEST_FIXTURE(uri_address, headers_add)
    {
        // Add multiple
        http_headers h;
        h.add(U("key1"), 22);
        h.add(U("key2"), U("str2"));
        VERIFY_ARE_EQUAL(U("22"), h[U("key1")]);
        VERIFY_ARE_EQUAL(U("str2"), h[U("key2")]);

        // Add one that already exists
        h.add(U("key2"), U("str3"));
        VERIFY_ARE_EQUAL(U("str2, str3"), h[U("key2")]);

        // Add with different case
        h.add(U("KEY2"), U("str4"));
        VERIFY_ARE_EQUAL(U("str2, str3, str4"), h[U("keY2")]);

        // Add with spaces in string
        h.add(U("key3"), U("value with spaces"));
        VERIFY_ARE_EQUAL(U("value with spaces"), h[U("key3")]);
    }

    TEST_FIXTURE(uri_address, headers_iterators)
    {
        // begin when empty
        http_headers h;
        VERIFY_ARE_EQUAL(h.begin(), h.end());

        // with some values.
        h.add(U("key1"), U("value1"));
        h.add(U("key2"), U("value2"));
        h.add(U("key3"), U("value3"));
        http_headers::const_iterator iter = h.begin();
        VERIFY_ARE_EQUAL(U("value1"), iter->second);
        ++iter;
        VERIFY_ARE_EQUAL(U("value2"), iter->second);
        ++iter;
        VERIFY_ARE_EQUAL(U("value3"), iter->second);
        ++iter;
        VERIFY_ARE_EQUAL(h.end(), iter);
    }

    TEST_FIXTURE(uri_address, headers_foreach)
    {
        // begin when empty
        http_headers h;
        VERIFY_ARE_EQUAL(h.begin(), h.end());

        // with some values.
        h.add(U("key1"), U("value"));
        h.add(U("key2"), U("value"));
        h.add(U("key3"), U("value"));

        std::for_each(std::begin(h), std::end(h), [=](http_headers::const_reference kv) {
            VERIFY_ARE_EQUAL(U("value"), kv.second);
        });

        std::for_each(
            std::begin(h), std::end(h), [=](http_headers::reference kv) { VERIFY_ARE_EQUAL(U("value"), kv.second); });
    }

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

        std::map<utility::string_t, utility::string_t> headers;
        headers[U("H1")] = U("");
        headers[U("H2")] = U("hah");
        headers[U("H3")] = U("es");
        headers[U("H4")] = U("es;kjr");
        headers[U("H5")] = U("asb");
        headers[U("H6")] = U("abc");
        headers[U("H7")] = U("eds");
        headers[U("H8")] = U("blue");
        headers[U("H9")] = U("sd");
        headers[U("H10")] = U("res");
        test_server_utilities::verify_request(
            &client, methods::GET, U("/"), scoped.server(), status_codes::OK, headers);
    }

    TEST_FIXTURE(uri_address, cache_control_header)
    {
        http_headers headers;
        VERIFY_ARE_EQUAL(headers.cache_control(), U(""));
        const utility::string_t value(U("custom value"));
        headers.set_cache_control(value);
        VERIFY_ARE_EQUAL(headers.cache_control(), value);
        utility::string_t foundValue;
        VERIFY_IS_TRUE(headers.match(header_names::cache_control, foundValue));
        VERIFY_ARE_EQUAL(value, foundValue);
    }

    TEST_FIXTURE(uri_address, content_length_header)
    {
        http_headers headers;
        VERIFY_ARE_EQUAL(headers.content_length(), 0);
        const size_t value = 44;
        headers.set_content_length(value);
        VERIFY_ARE_EQUAL(headers.content_length(), value);
        size_t foundValue;
        VERIFY_IS_TRUE(headers.match(header_names::content_length, foundValue));
        VERIFY_ARE_EQUAL(value, foundValue);
    }

    TEST_FIXTURE(uri_address, date_header)
    {
        http_headers headers;
        VERIFY_ARE_EQUAL(headers.date(), U(""));
        const utility::datetime value(utility::datetime::utc_now());
        headers.set_date(value);
        VERIFY_ARE_EQUAL(headers.date(), value.to_string());
        utility::string_t foundValue;
        VERIFY_IS_TRUE(headers.match(header_names::date, foundValue));
        VERIFY_ARE_EQUAL(value.to_string(), foundValue);
    }

    TEST_FIXTURE(uri_address, parsing_content_type_redundantsemicolon_json)
    {
        test_http_server::scoped_server scoped(m_uri);
        web::json::value body = web::json::value::string(U("Json body"));

        scoped.server()->next_request().then([&](test_request* p_request) {
            std::map<utility::string_t, utility::string_t> headers;
            headers[header_names::content_type] = U("application/json; charset=utf-8;;;;");
            p_request->reply(200, U("OK"), headers, utility::conversions::to_utf8string(body.serialize()));
        });

        http_client client(m_uri);
        auto resp = client.request(methods::GET).get();
        VERIFY_ARE_EQUAL(resp.extract_json().get().serialize(), body.serialize());
    }

    TEST_FIXTURE(uri_address, parsing_content_type_redundantsemicolon_string)
    {
        test_http_server::scoped_server scoped(m_uri);
        std::string body("Body");
        scoped.server()->next_request().then([&](test_request* p_request) {
            std::map<utility::string_t, utility::string_t> headers;
            headers[header_names::content_type] = U("text/plain; charset  =  UTF-8;;;; ");
            p_request->reply(200, U("OK"), headers, body);
        });

        http_client client(m_uri);
        auto resp = client.request(methods::GET).get();
        VERIFY_ARE_EQUAL(resp.extract_string().get(), utility::conversions::to_string_t(body));
    }

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

        // Test default case of cpprestsdk setting host header as host:port
        auto& host = m_uri.host();
        int port = m_uri.port();
        utility::string_t expected_default_header = host + U(":") + utility::conversions::details::to_string_t(port);
        http_request default_host_headers_request(methods::GET);
        scoped.server()->next_request().then([&](test_request* p_request) {
            auto headers = p_request->m_headers;
            VERIFY_ARE_EQUAL(expected_default_header, headers[header_names::host]);
            p_request->reply(200);
        });

        client.request(default_host_headers_request).get();

#ifndef __cplusplus_winrt
        // Test case where we overwrite the host header
        http_request overwritten_host_headers_request(methods::GET);
        overwritten_host_headers_request.headers().add(U("Host"), host);
        scoped.server()->next_request().then([&](test_request* p_request) {
            auto headers = p_request->m_headers;
            VERIFY_ARE_EQUAL(host, headers[header_names::host]);
            p_request->reply(200);
        });
        client.request(overwritten_host_headers_request).get();
#endif
    }
} // SUITE(header_tests)

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