fix(esp_http_client): updated API esp_http_client_get_url to get URL in correct format

This commit updates the API to include the port number in the URL,
which was previously missing.
This commit is contained in:
nilesh.kale 2024-12-17 17:14:27 +05:30
parent c6637ae369
commit 90d2dbad99
2 changed files with 23 additions and 3 deletions

View File

@ -1859,7 +1859,7 @@ esp_err_t esp_http_client_get_url(esp_http_client_handle_t client, char *url, co
return ESP_ERR_INVALID_ARG;
}
if (client->connection_info.host && client->connection_info.scheme && client->connection_info.path) {
snprintf(url, len, "%s://%s%s", client->connection_info.scheme, client->connection_info.host, client->connection_info.path);
snprintf(url, len, "%s://%s:%d%s", client->connection_info.scheme, client->connection_info.host, client->connection_info.port, client->connection_info.path);
return ESP_OK;
} else {
ESP_LOGE(TAG, "Failed to get URL");

View File

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2018-2021 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2018-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -99,7 +99,7 @@ TEST_CASE("Username is unmodified when we change to new path", "[ESP HTTP CLIENT
* Explicit APIs esp_http_client_set_username and esp_http_client_set_password are used to change
* the auth credentials
**/
TEST_CASE("Username and password will not reset if new absolute URL doesnot specify auth credentials.", "[ESP HTTP CLIENT]")
TEST_CASE("Username and password will not reset if new absolute URL does not specify auth credentials.", "[ESP HTTP CLIENT]")
{
esp_http_client_config_t config_with_auth = {
.host = HOST,
@ -146,6 +146,26 @@ TEST_CASE("esp_http_client_init() should return NULL if configured with wrong ur
esp_http_client_cleanup(client);
}
/**
* Test case to verify that esp_http_client_get_url() returns the URL in the correct format.
**/
TEST_CASE("esp_http_client_get_url() should return URL in the correct format", "[ESP HTTP CLIENT]")
{
const char *url = "http://httpbin.org:8080/post";
esp_http_client_config_t config = {
.url = url,
};
esp_http_client_handle_t client = esp_http_client_init(&config);
TEST_ASSERT_NOT_NULL(client);
char client_url[32];
esp_http_client_get_url(client, client_url, sizeof(client_url));
esp_http_client_cleanup(client);
TEST_ASSERT_EQUAL_STRING(url, client_url);
}
void app_main(void)
{
unity_run_menu();