| 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 |
#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 |
|
| 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 |
|
| 99 |
Error err = client->connect_to_host(host, port, tls_options); |
| 100 |
CHECK_MESSAGE(err == OK, "Expected OK for successful connection"); |
| 101 |
} |
| 102 |
#endif |
| 103 |
|
| 104 |
} |