test_http_client.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #ifndef TEST_HTTP_CLIENT_H
  31. #define TEST_HTTP_CLIENT_H
  32. #include "core/io/http_client.h"
  33. #include "tests/test_macros.h"
  34. #include "modules/modules_enabled.gen.h"
  35. namespace TestHTTPClient {
  36. TEST_CASE("[HTTPClient] Instantiation") {
  37. Ref<HTTPClient> client = HTTPClient::create();
  38. CHECK_MESSAGE(client.is_valid(), "A HTTP Client created should not be a null pointer");
  39. }
  40. TEST_CASE("[HTTPClient] query_string_from_dict") {
  41. Ref<HTTPClient> client = HTTPClient::create();
  42. Dictionary empty_dict;
  43. String empty_query = client->query_string_from_dict(empty_dict);
  44. CHECK_MESSAGE(empty_query.is_empty(), "A empty dictionary should return a empty string");
  45. Dictionary dict1;
  46. dict1["key"] = "value";
  47. String single_key = client->query_string_from_dict(dict1);
  48. CHECK_MESSAGE(single_key == "key=value", "The query should return key=value for every string in the dictionary");
  49. // Check Dictionary with multiple values of different types.
  50. Dictionary dict2;
  51. dict2["key1"] = "value";
  52. dict2["key2"] = 123;
  53. Array values;
  54. values.push_back(1);
  55. values.push_back(2);
  56. values.push_back(3);
  57. dict2["key3"] = values;
  58. dict2["key4"] = Variant();
  59. String multiple_keys = client->query_string_from_dict(dict2);
  60. CHECK_MESSAGE(multiple_keys == "key1=value&key2=123&key3=1&key3=2&key3=3&key4",
  61. "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");
  62. }
  63. TEST_CASE("[HTTPClient] verify_headers") {
  64. Ref<HTTPClient> client = HTTPClient::create();
  65. Vector<String> headers = { "Accept: text/html", "Content-Type: application/json", "Authorization: Bearer abc123" };
  66. Error err = client->verify_headers(headers);
  67. CHECK_MESSAGE(err == OK, "Expected OK for valid headers");
  68. ERR_PRINT_OFF;
  69. Vector<String> empty_header = { "" };
  70. err = client->verify_headers(empty_header);
  71. CHECK_MESSAGE(err == ERR_INVALID_PARAMETER, "Expected ERR_INVALID_PARAMETER for empty header");
  72. Vector<String> invalid_header = { "InvalidHeader", "Header: " };
  73. err = client->verify_headers(invalid_header);
  74. CHECK_MESSAGE(err == ERR_INVALID_PARAMETER, "Expected ERR_INVALID_PARAMETER for header with no colon");
  75. Vector<String> invalid_header_b = { ":", "Header: " };
  76. err = client->verify_headers(invalid_header_b);
  77. CHECK_MESSAGE(err == ERR_INVALID_PARAMETER, "Expected ERR_INVALID_PARAMETER for header with colon in first position");
  78. ERR_PRINT_ON;
  79. }
  80. #if defined(MODULE_MBEDTLS_ENABLED) || defined(WEB_ENABLED)
  81. TEST_CASE("[HTTPClient] connect_to_host") {
  82. Ref<HTTPClient> client = HTTPClient::create();
  83. String host = "https://www.example.com";
  84. int port = 443;
  85. Ref<TLSOptions> tls_options;
  86. // Connect to host.
  87. Error err = client->connect_to_host(host, port, tls_options);
  88. CHECK_MESSAGE(err == OK, "Expected OK for successful connection");
  89. }
  90. #endif // MODULE_MBEDTLS_ENABLED || WEB_ENABLED
  91. } // namespace TestHTTPClient
  92. #endif // TEST_HTTP_CLIENT_H