#pragma once
#include "core/io/http_client.h"
#include "tests/test_macros.h"
#include "modules/modules_enabled.gen.h"
namespace TestHTTPClient {
TEST_CASE("[HTTPClient] Instantiation") {
Ref<HTTPClient> client = HTTPClient::create();
CHECK_MESSAGE(client.is_valid(), "A HTTP Client created should not be a null pointer");
}
TEST_CASE("[HTTPClient] query_string_from_dict") {
Ref<HTTPClient> client = HTTPClient::create();
Dictionary empty_dict;
String empty_query = client->query_string_from_dict(empty_dict);
CHECK_MESSAGE(empty_query.is_empty(), "A empty dictionary should return a empty string");
Dictionary dict1;
dict1["key"] = "value";
String single_key = client->query_string_from_dict(dict1);
CHECK_MESSAGE(single_key == "key=value", "The query should return key=value for every string in the dictionary");
Dictionary dict2;
dict2["key1"] = "value";
dict2["key2"] = 123;
Array values = { 1, 2, 3 };
dict2["key3"] = values;
dict2["key4"] = Variant();
String multiple_keys = client->query_string_from_dict(dict2);
CHECK_MESSAGE(multiple_keys == "key1=value&key2=123&key3=1&key3=2&key3=3&key4",
"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");
}
TEST_CASE("[HTTPClient] verify_headers") {
Ref<HTTPClient> client = HTTPClient::create();
Vector<String> headers = { "Accept: text/html", "Content-Type: application/json", "Authorization: Bearer abc123" };
Error err = client->verify_headers(headers);
CHECK_MESSAGE(err == OK, "Expected OK for valid headers");
ERR_PRINT_OFF;
Vector<String> empty_header = { "" };
err = client->verify_headers(empty_header);
CHECK_MESSAGE(err == ERR_INVALID_PARAMETER, "Expected ERR_INVALID_PARAMETER for empty header");
Vector<String> invalid_header = { "InvalidHeader", "Header: " };
err = client->verify_headers(invalid_header);
CHECK_MESSAGE(err == ERR_INVALID_PARAMETER, "Expected ERR_INVALID_PARAMETER for header with no colon");
Vector<String> invalid_header_b = { ":", "Header: " };
err = client->verify_headers(invalid_header_b);
CHECK_MESSAGE(err == ERR_INVALID_PARAMETER, "Expected ERR_INVALID_PARAMETER for header with colon in first position");
ERR_PRINT_ON;
}
#if defined(MODULE_MBEDTLS_ENABLED) || defined(WEB_ENABLED)
TEST_CASE("[HTTPClient] connect_to_host") {
Ref<HTTPClient> client = HTTPClient::create();
String host = "https://www.example.com";
int port = 443;
Ref<TLSOptions> tls_options;
Error err = client->connect_to_host(host, port, tls_options);
CHECK_MESSAGE(err == OK, "Expected OK for successful connection");
}
#endif
}