Added host URL normalization
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -55,7 +56,7 @@ func NewApiClient(endpoints []string, tokenId string, secret string, checkInterv
|
||||
// Prepare API endpoints.
|
||||
for _, endpoint := range endpoints {
|
||||
apiEndpoint := ApiEndpoint{
|
||||
host: endpoint,
|
||||
host: normalizeApiHost(endpoint),
|
||||
alive: false,
|
||||
}
|
||||
instance.endpoints = append(instance.endpoints, &apiEndpoint)
|
||||
@@ -86,6 +87,11 @@ func NewApiClient(endpoints []string, tokenId string, secret string, checkInterv
|
||||
return &instance
|
||||
}
|
||||
|
||||
// normalizeApiHost ensures API paths can be appended to either IP- or DNS-based hosts.
|
||||
func normalizeApiHost(host string) string {
|
||||
return strings.TrimRight(strings.TrimSpace(host), "/") + "/"
|
||||
}
|
||||
|
||||
// Check endpoint liveness state.
|
||||
func (instance *ApiClient) checkEndpointsLiveness() {
|
||||
// We want to make sure other routines won't make any requests until we have checked for alive connections.
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package proxmox
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestNewApiClientNormalizesEndpointHosts(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
host string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "IP without trailing slash",
|
||||
host: "https://192.168.0.10:8006",
|
||||
want: "https://192.168.0.10:8006/",
|
||||
},
|
||||
{
|
||||
name: "IP with trailing slash",
|
||||
host: "https://192.168.0.10:8006/",
|
||||
want: "https://192.168.0.10:8006/",
|
||||
},
|
||||
{
|
||||
name: "DNS without trailing slash",
|
||||
host: "https://pve.example.com:8006",
|
||||
want: "https://pve.example.com:8006/",
|
||||
},
|
||||
{
|
||||
name: "DNS with trailing slash",
|
||||
host: "https://pve.example.com:8006/",
|
||||
want: "https://pve.example.com:8006/",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
client := NewApiClient([]string{tt.host}, "token", "secret", time.Second)
|
||||
defer client.httpClient.CloseIdleConnections()
|
||||
|
||||
if got := client.endpoints[0].host; got != tt.want {
|
||||
t.Fatalf("normalized endpoint host = %q, want %q", got, tt.want)
|
||||
}
|
||||
if got := client.endpoints[0].host + "api2/json/"; got != tt.want+"api2/json/" {
|
||||
t.Fatalf("liveness URL = %q, want %q", got, tt.want+"api2/json/")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user