Add support for parsing endpoints without schema.

Fixes smallstep/ca-component#117
This commit is contained in:
Mariano Cano 2018-11-26 18:29:45 -08:00
parent 3f0a55418c
commit b0a410066b
2 changed files with 69 additions and 4 deletions

View file

@ -8,6 +8,7 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"
"time"
@ -510,3 +511,41 @@ func TestClient_ProvisionerKey(t *testing.T) {
})
}
}
func Test_parseEndpoint(t *testing.T) {
expected1 := &url.URL{Scheme: "https", Host: "ca.smallstep.com"}
expected2 := &url.URL{Scheme: "https", Host: "ca.smallstep.com", Path: "/1.0/sign"}
type args struct {
endpoint string
}
tests := []struct {
name string
args args
want *url.URL
wantErr bool
}{
{"ok", args{"https://ca.smallstep.com"}, expected1, false},
{"ok no scheme", args{"//ca.smallstep.com"}, expected1, false},
{"ok only host", args{"ca.smallstep.com"}, expected1, false},
{"ok no bars", args{"https://ca.smallstep.com"}, expected1, false},
{"ok schema, host and path", args{"https://ca.smallstep.com/1.0/sign"}, expected2, false},
{"ok no bars with path", args{"https://ca.smallstep.com/1.0/sign"}, expected2, false},
{"ok host and path", args{"ca.smallstep.com/1.0/sign"}, expected2, false},
{"ok host and port", args{"ca.smallstep.com:443"}, &url.URL{Scheme: "https", Host: "ca.smallstep.com:443"}, false},
{"ok host, path and port", args{"ca.smallstep.com:443/1.0/sign"}, &url.URL{Scheme: "https", Host: "ca.smallstep.com:443", Path: "/1.0/sign"}, false},
{"fail bad url", args{"://ca.smallstep.com"}, nil, true},
{"fail no host", args{"https://"}, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseEndpoint(tt.args.endpoint)
if (err != nil) != tt.wantErr {
t.Errorf("parseEndpoint() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("parseEndpoint() = %v, want %v", got, tt.want)
}
})
}
}