test_http_client.h 5.0 KB

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