test_http_client.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2018 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdlib.h>
  15. #include <stdbool.h>
  16. #include <esp_system.h>
  17. #include <esp_http_client.h>
  18. #include "unity.h"
  19. #include "test_utils.h"
  20. #define HOST "httpbin.org"
  21. #define USERNAME "user"
  22. #define PASSWORD "challenge"
  23. TEST_CASE("Test in common case: Only URL and hostname are specified.", "[ESP HTTP CLIENT]")
  24. {
  25. esp_http_client_config_t config_incorrect = {0};
  26. test_case_uses_tcpip();
  27. esp_http_client_handle_t client = esp_http_client_init(&config_incorrect);
  28. TEST_ASSERT(client == NULL);
  29. esp_http_client_config_t config_with_url = {
  30. .url = "http://httpbin.org/get",
  31. };
  32. client = esp_http_client_init(&config_with_url);
  33. TEST_ASSERT(client != NULL);
  34. TEST_ASSERT(esp_http_client_cleanup(client) == ESP_OK);
  35. esp_http_client_config_t config_with_hostname_path = {
  36. .host = HOST,
  37. .path = "/get",
  38. };
  39. client = esp_http_client_init(&config_with_hostname_path);
  40. TEST_ASSERT(client != NULL);
  41. TEST_ASSERT(esp_http_client_cleanup(client) == ESP_OK);
  42. }
  43. TEST_CASE("Get username and password after initialization.", "[ESP HTTP CLIENT]")
  44. {
  45. esp_http_client_config_t config_with_auth = {
  46. .host = HOST,
  47. .path = "/",
  48. .username = USERNAME,
  49. .password = PASSWORD
  50. };
  51. char *value = NULL;
  52. esp_http_client_handle_t client = esp_http_client_init(&config_with_auth);
  53. TEST_ASSERT_NOT_NULL(client);
  54. // Test with username
  55. esp_err_t r = esp_http_client_get_username(client, &value);
  56. TEST_ASSERT_EQUAL(ESP_OK, r);
  57. TEST_ASSERT_NOT_NULL(value);
  58. TEST_ASSERT_EQUAL_STRING(USERNAME, value);
  59. // Test with password
  60. value = NULL;
  61. r = esp_http_client_get_password(client, &value);
  62. TEST_ASSERT_EQUAL(ESP_OK, r);
  63. TEST_ASSERT_NOT_NULL(value);
  64. TEST_ASSERT_EQUAL_STRING(PASSWORD, value);
  65. esp_http_client_cleanup(client);
  66. }
  67. /**
  68. * Test case to test that, the esp_http_client_set_url won't drop username and password
  69. * when pass a path "/abc" for url.
  70. **/
  71. TEST_CASE("Username is unmodified when we change to new path", "[ESP HTTP CLIENT]")
  72. {
  73. esp_http_client_config_t config_with_auth = {
  74. .host = HOST,
  75. .path = "/",
  76. .username = USERNAME,
  77. .password = PASSWORD
  78. };
  79. char *value = NULL;
  80. esp_http_client_handle_t client = esp_http_client_init(&config_with_auth);
  81. TEST_ASSERT_NOT_NULL(client);
  82. esp_err_t r = esp_http_client_get_username(client, &value);
  83. TEST_ASSERT_EQUAL(ESP_OK, r);
  84. TEST_ASSERT_NOT_NULL(value);
  85. TEST_ASSERT_EQUAL_STRING(USERNAME, value);
  86. esp_http_client_set_url(client, "/something-else/");
  87. r = esp_http_client_get_username(client, &value);
  88. TEST_ASSERT_EQUAL(ESP_OK, r);
  89. TEST_ASSERT_NOT_NULL(value);
  90. TEST_ASSERT_EQUAL_STRING(USERNAME, value);
  91. esp_http_client_cleanup(client);
  92. }
  93. /**
  94. * Test case to test that, the esp_http_client_set_url do not reset the auth credentials
  95. * Explicit APIs esp_http_client_set_username and esp_http_client_set_password are used to change
  96. * the auth credentials
  97. **/
  98. TEST_CASE("Username and password will not reset if new absolute URL doesnot specify auth credentials.", "[ESP HTTP CLIENT]")
  99. {
  100. esp_http_client_config_t config_with_auth = {
  101. .host = HOST,
  102. .path = "/",
  103. .username = USERNAME,
  104. .password = PASSWORD
  105. };
  106. char *value = NULL;
  107. esp_http_client_handle_t client = esp_http_client_init(&config_with_auth);
  108. TEST_ASSERT_NOT_NULL(client);
  109. esp_err_t r = esp_http_client_get_username(client, &value);
  110. TEST_ASSERT_EQUAL(ESP_OK, r);
  111. TEST_ASSERT_NOT_NULL(value);
  112. TEST_ASSERT_EQUAL_STRING(USERNAME, value);
  113. esp_http_client_set_url(client, "http://" HOST "/get");
  114. esp_http_client_set_username(client, value);
  115. // esp_http_client_set_username sets new username and thus invalidates the original one
  116. // which we still reference in the local variable `value` (better forget it)
  117. value = NULL;
  118. esp_http_client_set_password(client, (char *)USERNAME); // Need to cast the string literal (argument is not a const char*)
  119. //checks if username is set or not
  120. r = esp_http_client_get_username(client, &value);
  121. TEST_ASSERT_EQUAL(ESP_OK, r);
  122. //If username is set then value should not be NULL
  123. TEST_ASSERT_NOT_NULL(value);
  124. //checks if password is set or not
  125. r = esp_http_client_get_password(client, &value);
  126. TEST_ASSERT_EQUAL(ESP_OK, r);
  127. //If password is set then value should not be NULL
  128. TEST_ASSERT_NOT_NULL(value);
  129. esp_http_client_cleanup(client);
  130. }