File: to_string_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 (133 lines) | stat: -rw-r--r-- 5,032 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
/***
 * 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 to_string APIs on HTTP request and responses.
 *
 * =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 ****/

#include "stdafx.h"

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

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

namespace tests
{
namespace functional
{
namespace http
{
namespace client
{
SUITE(to_string_tests)
{
    TEST_FIXTURE(uri_address, request_to_string_without_body)
    {
        test_http_server::scoped_server scoped(m_uri);
        http_client client(m_uri);
        const method mtd = methods::GET;
        const utility::string_t path = U("/pathbaby/");
        const utility::string_t content_type = U("text/plain; charset= utf-8");

        // to_string
        http_request msg(mtd);
        msg.set_request_uri(path);
        msg.headers()[U("Content-Type")] = content_type;

        std::map<utility::string_t, utility::string_t> expected_headers;
        expected_headers[U("Content-Type")] = content_type;
        http_asserts::assert_request_string_equals(msg.to_string(), mtd, path, U("HTTP/1.1"), expected_headers, U(""));
    }

    TEST_FIXTURE(uri_address, request_to_string_with_body)
    {
        test_http_server::scoped_server scoped(m_uri);
        http_client client(m_uri);
        const method mtd = methods::POST;
        const utility::string_t path = U("/path baby/");
        const utility::string_t content_type = U("text/plain;charset=utf-8");
        const utility::string_t body = U("YES THIS IS THE MSG BODY!!!!!");

        // to_string
        http_request msg(mtd);
        msg.set_request_uri(uri::encode_uri(path, uri::components::path));
        msg.headers()[U("Content-Type")] = content_type;
        msg.set_body(body);

        std::map<utility::string_t, utility::string_t> expected_headers;
        expected_headers[U("Content-Type")] = content_type;
        expected_headers[U("Content-Length")] = U("29");
        http_asserts::assert_request_string_equals(
            msg.to_string(), mtd, U("/path%20baby/"), U("HTTP/1.1"), expected_headers, body);
    }

    TEST_FIXTURE(uri_address, response_to_string_without_body)
    {
        test_http_server::scoped_server scoped(m_uri);
        http_client client(m_uri);
        const web::http::status_code code = status_codes::OK;
        const utility::string_t reason = U("OK YEAH!");
        const utility::string_t content_type = U("not; charset= utf-8");

        // to_string
        scoped.server()->next_request().then([&](test_request* request) {
            std::map<utility::string_t, utility::string_t> headers;
            headers[U("Content-Type")] = content_type;
            request->reply(code, reason, headers);
        });
        http_response rsp = client.request(methods::GET).get();

        std::map<utility::string_t, utility::string_t> expected_headers;
        expected_headers[U("Content-Length")] = U("0");
        expected_headers[U("Content-Type")] = content_type;
        http_asserts::assert_response_string_equals(
            rsp.to_string(), U("HTTP/1.1"), code, U("OK"), expected_headers, U(""));

#ifdef _WIN32
        // Don't verify the values of each of these headers, but make sure they exist.
        if (!rsp.headers().has(U("Date")) || !rsp.headers().has(U("Cache-Control")) || !rsp.headers().has(U("Server")))
        {
            CHECK(false);
        }
#endif
    }

    TEST_FIXTURE(uri_address, response_to_string_with_body)
    {
        test_http_server::scoped_server scoped(m_uri);
        http_client client(m_uri);
        const ::http::status_code code = status_codes::OK;
        const utility::string_t reason = U("OK YEAH!");
        const std::string data = "HERE IS THE RESPONSE body!";
        const utility::string_t content_type = U("text/yeah;charset=utf-8");

        // to_string
        scoped.server()->next_request().then([&](test_request* request) {
            std::map<utility::string_t, utility::string_t> headers;
            headers[U("Content-Type")] = content_type;
            request->reply(code, reason, headers, data);
        });

        http_response rsp = client.request(methods::GET).get();
        rsp.content_ready().wait();

        std::map<utility::string_t, utility::string_t> expected_headers;
        expected_headers[U("Content-Length")] = U("26");
        expected_headers[U("Content-Type")] = content_type;
        http_asserts::assert_response_string_equals(
            rsp.to_string(), U("HTTP/1.1"), code, U("OK"), expected_headers, ::utility::conversions::to_string_t(data));
    }

} // SUITE(to_string_tests)

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