Skip to content

Commit 530933c

Browse files
authored
fix: cli client on default scheme (#362)
1 parent 6ae250b commit 530933c

2 files changed

Lines changed: 66 additions & 1 deletion

File tree

internal/api/client.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"fmt"
1212
"io"
1313
"net/http"
14+
"strings"
1415
"syscall"
1516
"time"
1617

@@ -36,11 +37,22 @@ func NewClient(cfg pkgmodel.APIConfig, auth http.Header, net *http.Client) *Clie
3637
}
3738

3839
return &Client{
39-
endpoint: fmt.Sprintf("%s:%d", cfg.URL, cfg.Port),
40+
endpoint: formatEndpoint(cfg.URL, cfg.Port),
4041
resty: client,
4142
}
4243
}
4344

45+
// formatEndpoint builds the API endpoint string. It omits the port when it
46+
// matches the scheme default (443 for HTTPS, 80 for HTTP) so that Go's HTTP
47+
// client does not include a redundant port in the Host header.
48+
func formatEndpoint(url string, port int) string {
49+
if (port == 443 && strings.HasPrefix(url, "https://")) ||
50+
(port == 80 && strings.HasPrefix(url, "http://")) {
51+
return url
52+
}
53+
return fmt.Sprintf("%s:%d", url, port)
54+
}
55+
4456
func (c *Client) Stats() (*apimodel.Stats, error) {
4557
resp, err := c.resty.R().
4658
Get(c.endpoint + "/api/v1/stats")

internal/api/client_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// © 2025 Platform Engineering Labs Inc.
2+
//
3+
// SPDX-License-Identifier: FSL-1.1-ALv2
4+
5+
package api
6+
7+
import (
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestFormatEndpointStandardPort(t *testing.T) {
14+
want := "http://localhost:49684"
15+
got := formatEndpoint("http://localhost", 49684)
16+
17+
assert.Equal(t, want, got)
18+
}
19+
20+
func TestFormatEndpointHTTPSDefault(t *testing.T) {
21+
want := "https://example.awsapprunner.com"
22+
got := formatEndpoint("https://example.awsapprunner.com", 443)
23+
24+
assert.Equal(t, want, got)
25+
}
26+
27+
func TestFormatEndpointHTTPDefault(t *testing.T) {
28+
want := "http://example.com"
29+
got := formatEndpoint("http://example.com", 80)
30+
31+
assert.Equal(t, want, got)
32+
}
33+
34+
func TestFormatEndpointHTTPSNonDefault(t *testing.T) {
35+
want := "https://example.com:8443"
36+
got := formatEndpoint("https://example.com", 8443)
37+
38+
assert.Equal(t, want, got)
39+
}
40+
41+
func TestFormatEndpointHTTPWith443(t *testing.T) {
42+
want := "http://example.com:443"
43+
got := formatEndpoint("http://example.com", 443)
44+
45+
assert.Equal(t, want, got)
46+
}
47+
48+
func TestFormatEndpointHTTPSWith80(t *testing.T) {
49+
want := "https://example.com:80"
50+
got := formatEndpoint("https://example.com", 80)
51+
52+
assert.Equal(t, want, got)
53+
}

0 commit comments

Comments
 (0)