Debian Codegraph @

tests/core/io/test_http_client.h

raw symbols SCIP
1 /**************************************************************************/
2 /* test_http_client.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #pragma once
32
33 #include "core/io/http_client.h"
34
35 #include "tests/test_macros.h"
36
37 #include "modules/modules_enabled.gen.h"
38
39 namespace TestHTTPClient {
40
41 TEST_CASE("[HTTPClient] Instantiation") {
42 Ref<HTTPClient> client = HTTPClient::create();
43 CHECK_MESSAGE(client.is_valid(), "A HTTP Client created should not be a null pointer");
44 }
45
46 TEST_CASE("[HTTPClient] query_string_from_dict") {
47 Ref<HTTPClient> client = HTTPClient::create();
48 Dictionary empty_dict;
49 String empty_query = client->query_string_from_dict(empty_dict);
50 CHECK_MESSAGE(empty_query.is_empty(), "A empty dictionary should return a empty string");
51
52 Dictionary dict1;
53 dict1["key"] = "value";
54 String single_key = client->query_string_from_dict(dict1);
55 CHECK_MESSAGE(single_key == "key=value", "The query should return key=value for every string in the dictionary");
56
57 // Check Dictionary with multiple values of different types.
58 Dictionary dict2;
59 dict2["key1"] = "value";
60 dict2["key2"] = 123;
61 Array values = { 1, 2, 3 };
62 dict2["key3"] = values;
63 dict2["key4"] = Variant();
64 String multiple_keys = client->query_string_from_dict(dict2);
65 CHECK_MESSAGE(multiple_keys == "key1=value&key2=123&key3=1&key3=2&key3=3&key4",
66 "The query should return key=value for every string in the dictionary. Pairs should be separated by &, arrays should be have a query for every element, and variants should have empty values");
67 }
68
69 TEST_CASE("[HTTPClient] verify_headers") {
70 Ref<HTTPClient> client = HTTPClient::create();
71 Vector<String> headers = { "Accept: text/html", "Content-Type: application/json", "Authorization: Bearer abc123" };
72
73 Error err = client->verify_headers(headers);
74 CHECK_MESSAGE(err == OK, "Expected OK for valid headers");
75
76 ERR_PRINT_OFF;
77 Vector<String> empty_header = { "" };
78 err = client->verify_headers(empty_header);
79 CHECK_MESSAGE(err == ERR_INVALID_PARAMETER, "Expected ERR_INVALID_PARAMETER for empty header");
80
81 Vector<String> invalid_header = { "InvalidHeader", "Header: " };
82 err = client->verify_headers(invalid_header);
83 CHECK_MESSAGE(err == ERR_INVALID_PARAMETER, "Expected ERR_INVALID_PARAMETER for header with no colon");
84
85 Vector<String> invalid_header_b = { ":", "Header: " };
86 err = client->verify_headers(invalid_header_b);
87 CHECK_MESSAGE(err == ERR_INVALID_PARAMETER, "Expected ERR_INVALID_PARAMETER for header with colon in first position");
88 ERR_PRINT_ON;
89 }
90
91 #if defined(MODULE_MBEDTLS_ENABLED) || defined(WEB_ENABLED)
92 TEST_CASE("[HTTPClient] connect_to_host") {
93 Ref<HTTPClient> client = HTTPClient::create();
94 String host = "https://www.example.com";
95 int port = 443;
96 Ref<TLSOptions> tls_options;
97
98 // Connect to host.
99 Error err = client->connect_to_host(host, port, tls_options);
100 CHECK_MESSAGE(err == OK, "Expected OK for successful connection");
101 }
102 #endif // MODULE_MBEDTLS_ENABLED || WEB_ENABLED
103
104 } // namespace TestHTTPClient