File: encoding_tests.cpp

package info (click to toggle)
cpprest 2.10.18-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 6,896 kB
  • sloc: cpp: 70,952; sh: 275; makefile: 170; javascript: 147
file content (134 lines) | stat: -rw-r--r-- 4,233 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
/***
 * Copyright (C) Microsoft. All rights reserved.
 * Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
 *
 * =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
 *
 * Tests for encoding features of the uri class.
 *
 * =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 ****/

#include "stdafx.h"

using namespace web;
using namespace utility;

namespace tests
{
namespace functional
{
namespace uri_tests
{
SUITE(encoding_tests)
{
#ifdef _WIN32
#pragma warning(push)
#pragma warning(disable : 4428)
    TEST(encode_string)
    {
        utility::string_t result = uri::encode_uri(L"first%second\u4e2d\u56fd");
        VERIFY_ARE_EQUAL(U("first%25second%E4%B8%AD%E5%9B%BD"), result);

        result = uri::encode_uri(U("first%second"));
        VERIFY_ARE_EQUAL(U("first%25second"), result);
    }

    TEST(decode_string)
    {
        utility::string_t result = uri::decode(U("first%25second%E4%B8%AD%E5%9B%BD"));
        VERIFY_ARE_EQUAL(L"first%second\u4e2d\u56fd", result);

        result = uri::decode(U("first%25second"));
        VERIFY_ARE_EQUAL(U("first%second"), result);
    }
#pragma warning(pop)
#endif

    TEST(encode_characters_in_resource)
    {
        utility::string_t result = uri::encode_uri(U("http://path%name/%#!%"));
        VERIFY_ARE_EQUAL(U("http://path%25name/%25#!%25"), result);
    }

    // Tests trying to encode empty strings.
    TEST(encode_decode_empty_strings)
    {
        // utility::string_t
        utility::string_t result = uri::encode_uri(U(""));
        VERIFY_ARE_EQUAL(U(""), result);
        utility::string_t str = uri::decode(result);
        VERIFY_ARE_EQUAL(U(""), str);

        // std::wstring
        result = uri::encode_uri(U(""));
        VERIFY_ARE_EQUAL(U(""), result);
        auto wstr = uri::decode(result);
        VERIFY_ARE_EQUAL(U(""), wstr);
    }

    // Tests encoding in various components of the URI.
    TEST(encode_uri_multiple_components)
    {
        // only encodes characters that aren't in the unreserved and reserved set.

        // utility::string_t
        utility::string_t str(U("htt p://^localhost:80/path ?^one=two# frag"));
        utility::string_t result = uri::encode_uri(str);
        VERIFY_ARE_EQUAL(U("htt%20p://%5Elocalhost:80/path%20?%5Eone=two#%20frag"), result);
        VERIFY_ARE_EQUAL(str, uri::decode(result));
    }

    // Tests encoding individual components of a URI.
    TEST(encode_uri_component)
    {
        // encodes all characters not in the unreserved set.

        // utility::string_t
        utility::string_t str(U("path with^spaced"));
        utility::string_t result = uri::encode_uri(str);
        VERIFY_ARE_EQUAL(U("path%20with%5Espaced"), result);
        VERIFY_ARE_EQUAL(str, uri::decode(result));
    }

    // Tests trying to decode a string that doesn't have 2 hex digits after %
    TEST(decode_invalid_hex)
    {
        VERIFY_THROWS(uri::decode(U("hehe%")), uri_exception);
        VERIFY_THROWS(uri::decode(U("hehe%2")), uri_exception);
        VERIFY_THROWS(uri::decode(U("hehe%4H")), uri_exception);
        VERIFY_THROWS(uri::decode(U("he%kkhe")), uri_exception);
    }

    // Tests making sure '+' is encoded even though nonstandard, so it doesn't
    // get mistaken later by some implementations as a space.
    TEST(encode_plus_char)
    {
        const utility::string_t encodedPlus(U("%2B"));

        uri_builder builder;
        builder.set_user_info(U("+"), true);
        builder.set_path(U("+"), true);
        builder.set_query(U("+"), true);
        builder.set_fragment(U("+"), true);

        VERIFY_ARE_EQUAL(builder.user_info(), encodedPlus);
        VERIFY_ARE_EQUAL(builder.path(), encodedPlus);
        VERIFY_ARE_EQUAL(builder.query(), encodedPlus);
        VERIFY_ARE_EQUAL(builder.fragment(), encodedPlus);
    }

    TEST(bug_417601)
    {
        utility::ostringstream_t ss1;
        auto enc1 = uri::encode_data_string(U("!"));
        ss1 << enc1;

        VERIFY_ARE_EQUAL(U("%21"), ss1.str());
    }

} // SUITE(encoding_tests)

} // namespace uri_tests
} // namespace functional
} // namespace tests