test_http_client.c
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 15 #include <stdlib.h> 16 #include <stdbool.h> 17 #include <esp_system.h> 18 #include <esp_http_client.h> 19 20 #include "unity.h" 21 #include "test_utils.h" 22 23 #define HOST "httpbin.org" 24 #define USERNAME "user" 25 #define PASSWORD "challenge" 26 27 TEST_CASE("Test in common case: Only URL and hostname are specified.", "[ESP HTTP CLIENT]") 28 { 29 esp_http_client_config_t config_incorrect = {0}; 30 31 test_case_uses_tcpip(); 32 33 esp_http_client_handle_t client = esp_http_client_init(&config_incorrect); 34 TEST_ASSERT(client == NULL); 35 36 esp_http_client_config_t config_with_url = { 37 .url = "http://httpbin.org/get", 38 }; 39 client = esp_http_client_init(&config_with_url); 40 TEST_ASSERT(client != NULL); 41 TEST_ASSERT(esp_http_client_cleanup(client) == ESP_OK); 42 43 44 esp_http_client_config_t config_with_hostname_path = { 45 .host = HOST, 46 .path = "/get", 47 }; 48 client = esp_http_client_init(&config_with_hostname_path); 49 TEST_ASSERT(client != NULL); 50 TEST_ASSERT(esp_http_client_cleanup(client) == ESP_OK); 51 } 52 53 TEST_CASE("Get username and password after initialization.", "[ESP HTTP CLIENT]") 54 { 55 esp_http_client_config_t config_with_auth = { 56 .host = HOST, 57 .path = "/", 58 .username = USERNAME, 59 .password = PASSWORD 60 }; 61 char *value = NULL; 62 esp_http_client_handle_t client = esp_http_client_init(&config_with_auth); 63 TEST_ASSERT_NOT_NULL(client); 64 // Test with username 65 esp_err_t r = esp_http_client_get_username(client, &value); 66 TEST_ASSERT_EQUAL(ESP_OK, r); 67 TEST_ASSERT_NOT_NULL(value); 68 TEST_ASSERT_EQUAL_STRING(USERNAME, value); 69 // Test with password 70 value = NULL; 71 r = esp_http_client_get_password(client, &value); 72 TEST_ASSERT_EQUAL(ESP_OK, r); 73 TEST_ASSERT_NOT_NULL(value); 74 TEST_ASSERT_EQUAL_STRING(PASSWORD, value); 75 esp_http_client_cleanup(client); 76 } 77 78 /** 79 * Test case to test that, the esp_http_client_set_url won't drop username and password 80 * when pass a path "/abc" for url. 81 **/ 82 TEST_CASE("Username is unmodified when we change to new path", "[ESP HTTP CLIENT]") 83 { 84 esp_http_client_config_t config_with_auth = { 85 .host = HOST, 86 .path = "/", 87 .username = USERNAME, 88 .password = PASSWORD 89 }; 90 char *value = NULL; 91 esp_http_client_handle_t client = esp_http_client_init(&config_with_auth); 92 TEST_ASSERT_NOT_NULL(client); 93 esp_err_t r = esp_http_client_get_username(client, &value); 94 TEST_ASSERT_EQUAL(ESP_OK, r); 95 TEST_ASSERT_NOT_NULL(value); 96 TEST_ASSERT_EQUAL_STRING(USERNAME, value); 97 esp_http_client_set_url(client, "/something-else/"); 98 r = esp_http_client_get_username(client, &value); 99 TEST_ASSERT_EQUAL(ESP_OK, r); 100 TEST_ASSERT_NOT_NULL(value); 101 TEST_ASSERT_EQUAL_STRING(USERNAME, value); 102 esp_http_client_cleanup(client); 103 } 104 105 /** 106 * Test case to test that, the esp_http_client_set_url do not reset the auth credentials 107 * Explicit APIs esp_http_client_set_username and esp_http_client_set_password are used to change 108 * the auth credentials 109 **/ 110 TEST_CASE("Username and password will not reset if new absolute URL doesnot specify auth credentials.", "[ESP HTTP CLIENT]") 111 { 112 esp_http_client_config_t config_with_auth = { 113 .host = HOST, 114 .path = "/", 115 .username = USERNAME, 116 .password = PASSWORD 117 }; 118 char *value = NULL; 119 esp_http_client_handle_t client = esp_http_client_init(&config_with_auth); 120 TEST_ASSERT_NOT_NULL(client); 121 esp_err_t r = esp_http_client_get_username(client, &value); 122 TEST_ASSERT_EQUAL(ESP_OK, r); 123 TEST_ASSERT_NOT_NULL(value); 124 TEST_ASSERT_EQUAL_STRING(USERNAME, value); 125 esp_http_client_set_url(client, "http://" HOST "/get"); 126 esp_http_client_set_username(client, value); 127 esp_http_client_set_password(client, value); 128 //checks if username is set or not 129 r = esp_http_client_get_username(client, &value); 130 TEST_ASSERT_EQUAL(ESP_OK, r); 131 //If username is set then value should not be NULL 132 TEST_ASSERT_NOT_NULL(value); 133 //checks if password is set or not 134 r = esp_http_client_get_password(client, &value); 135 TEST_ASSERT_EQUAL(ESP_OK, r); 136 //If password is set then value should not be NULL 137 TEST_ASSERT_NOT_NULL(value); 138 esp_http_client_cleanup(client); 139 }