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