@ -2926,3 +2926,111 @@ func BenchmarkRespond(b *testing.B) {
api . respond ( & testResponseWriter , response , nil )
}
}
func TestGetGlobalURL ( t * testing . T ) {
mustParseURL := func ( t * testing . T , u string ) * url . URL {
parsed , err := url . Parse ( u )
require . NoError ( t , err )
return parsed
}
testcases := [ ] struct {
input * url . URL
opts GlobalURLOptions
expected * url . URL
errorful bool
} {
{
mustParseURL ( t , "http://127.0.0.1:9090" ) ,
GlobalURLOptions {
ListenAddress : "127.0.0.1:9090" ,
Host : "127.0.0.1:9090" ,
Scheme : "http" ,
} ,
mustParseURL ( t , "http://127.0.0.1:9090" ) ,
false ,
} ,
{
mustParseURL ( t , "http://127.0.0.1:9090" ) ,
GlobalURLOptions {
ListenAddress : "127.0.0.1:9090" ,
Host : "prometheus.io" ,
Scheme : "https" ,
} ,
mustParseURL ( t , "https://prometheus.io" ) ,
false ,
} ,
{
mustParseURL ( t , "http://exemple.com" ) ,
GlobalURLOptions {
ListenAddress : "127.0.0.1:9090" ,
Host : "prometheus.io" ,
Scheme : "https" ,
} ,
mustParseURL ( t , "http://exemple.com" ) ,
false ,
} ,
{
mustParseURL ( t , "http://localhost:8080" ) ,
GlobalURLOptions {
ListenAddress : "127.0.0.1:9090" ,
Host : "prometheus.io" ,
Scheme : "https" ,
} ,
mustParseURL ( t , "http://prometheus.io:8080" ) ,
false ,
} ,
{
mustParseURL ( t , "http://[::1]:8080" ) ,
GlobalURLOptions {
ListenAddress : "127.0.0.1:9090" ,
Host : "prometheus.io" ,
Scheme : "https" ,
} ,
mustParseURL ( t , "http://prometheus.io:8080" ) ,
false ,
} ,
{
mustParseURL ( t , "http://localhost" ) ,
GlobalURLOptions {
ListenAddress : "127.0.0.1:9090" ,
Host : "prometheus.io" ,
Scheme : "https" ,
} ,
mustParseURL ( t , "http://prometheus.io" ) ,
false ,
} ,
{
mustParseURL ( t , "http://localhost:9091" ) ,
GlobalURLOptions {
ListenAddress : "[::1]:9090" ,
Host : "[::1]" ,
Scheme : "https" ,
} ,
mustParseURL ( t , "http://[::1]:9091" ) ,
false ,
} ,
{
mustParseURL ( t , "http://localhost:9091" ) ,
GlobalURLOptions {
ListenAddress : "[::1]:9090" ,
Host : "[::1]:9090" ,
Scheme : "https" ,
} ,
mustParseURL ( t , "http://[::1]:9091" ) ,
false ,
} ,
}
for i , tc := range testcases {
t . Run ( fmt . Sprintf ( "Test %d" , i ) , func ( t * testing . T ) {
output , err := getGlobalURL ( tc . input , tc . opts )
if tc . errorful {
require . Error ( t , err )
return
}
require . NoError ( t , err )
require . Equal ( t , tc . expected , output )
} )
}
}