diff --git a/components/esp_http_client/esp_http_client.c b/components/esp_http_client/esp_http_client.c index c7d2c28ffa..275c39fd8b 100644 --- a/components/esp_http_client/esp_http_client.c +++ b/components/esp_http_client/esp_http_client.c @@ -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"); diff --git a/components/esp_http_client/test_apps/main/test_http_client.c b/components/esp_http_client/test_apps/main/test_http_client.c index f4b1e081f8..f066c2d52b 100644 --- a/components/esp_http_client/test_apps/main/test_http_client.c +++ b/components/esp_http_client/test_apps/main/test_http_client.c @@ -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();