test_http_client.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. namespace TestHTTPClient {
  35. TEST_CASE("[HTTPClient] Instantiation") {
  36. Ref<HTTPClient> client = HTTPClient::create();
  37. CHECK_MESSAGE(client != nullptr, "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;
  53. values.push_back(1);
  54. values.push_back(2);
  55. values.push_back(3);
  56. dict2["key3"] = values;
  57. dict2["key4"] = Variant();
  58. String multiple_keys = client->query_string_from_dict(dict2);
  59. CHECK_MESSAGE(multiple_keys == "key1=value&key2=123&key3=1&key3=2&key3=3&key4",
  60. "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");
  61. }
  62. TEST_CASE("[HTTPClient] verify_headers") {
  63. Ref<HTTPClient> client = HTTPClient::create();
  64. Vector<String> headers = { "Accept: text/html", "Content-Type: application/json", "Authorization: Bearer abc123" };
  65. Error err = client->verify_headers(headers);
  66. CHECK_MESSAGE(err == OK, "Expected OK for valid headers");
  67. ERR_PRINT_OFF;
  68. Vector<String> empty_header = { "" };
  69. err = client->verify_headers(empty_header);
  70. CHECK_MESSAGE(err == ERR_INVALID_PARAMETER, "Expected ERR_INVALID_PARAMETER for empty header");
  71. Vector<String> invalid_header = { "InvalidHeader", "Header: " };
  72. err = client->verify_headers(invalid_header);
  73. CHECK_MESSAGE(err == ERR_INVALID_PARAMETER, "Expected ERR_INVALID_PARAMETER for header with no colon");
  74. Vector<String> invalid_header_b = { ":", "Header: " };
  75. err = client->verify_headers(invalid_header_b);
  76. CHECK_MESSAGE(err == ERR_INVALID_PARAMETER, "Expected ERR_INVALID_PARAMETER for header with colon in first position");
  77. ERR_PRINT_ON;
  78. }
  79. TEST_CASE("[HTTPClient] connect_to_host") {
  80. Ref<HTTPClient> client = HTTPClient::create();
  81. String host = "https://www.example.com";
  82. int port = 443;
  83. Ref<TLSOptions> tls_options;
  84. // Connect to host.
  85. Error err = client->connect_to_host(host, port, tls_options);
  86. CHECK_MESSAGE(err == OK, "Expected OK for successful connection");
  87. }
  88. } // namespace TestHTTPClient
  89. #endif // TEST_HTTP_CLIENT_H