Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TT-5790] Update EnsureTransport and related tests #6243

Merged
merged 2 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions gateway/reverse_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +124,27 @@ func urlFromService(spec *APISpec, gw *Gateway) (*apidef.HostList, error) {
// httpScheme matches http://* and https://*, case insensitive
var httpScheme = regexp.MustCompile(`^(?i)https?://`)

// EnsureTransport sanitizes host/protocol pairs and returns a valid URL.
func EnsureTransport(host, protocol string) string {
host = strings.TrimSpace(host)
protocol = strings.TrimSpace(protocol)

// sanitize protocol
if protocol == "" {
protocol = "http"
}

// if host has no protocol, amend it
if !strings.Contains(host, "://") {
host = protocol + "://" + host
}

host = strings.Replace(host, "h2c://", "http://", 1)

u, err := url.Parse(host)
if err != nil {
return host
}
switch u.Scheme {
case "":
if protocol == "" {
protocol = "http"
}
u.Scheme = protocol
case "h2c":
u.Scheme = "http"
}
return u.String()
}

Expand Down
25 changes: 22 additions & 3 deletions gateway/reverse_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1585,6 +1585,23 @@ func TestEnsureTransport(t *testing.T) {
cases := []struct {
host, protocol, expect string
}{
// This section tests EnsureTransport if port + IP is supplied
{"https://192.168.1.1:443 ", "https", "https://192.168.1.1:443"},
{"192.168.1.1:443 ", "https", "https://192.168.1.1:443"},
{"http://192.168.1.1:80 ", "https", "http://192.168.1.1:80"},
{"192.168.1.1:2000 ", "tls", "tls://192.168.1.1:2000"},
{"192.168.1.1:2000 ", "", "http://192.168.1.1:2000"},
// This section tests EnsureTransport if port is supplied
{"https://httpbin.org:443 ", "https", "https://httpbin.org:443"},
{"httpbin.org:443 ", "https", "https://httpbin.org:443"},
{"http://httpbin.org:80 ", "https", "http://httpbin.org:80"},
{"httpbin.org:2000 ", "tls", "tls://httpbin.org:2000"},
{"httpbin.org:2000 ", "", "http://httpbin.org:2000"},
// This is the h2c proto to http conversion
{"http://httpbin.org ", "h2c", "http://httpbin.org"},
{"h2c://httpbin.org ", "h2c", "http://httpbin.org"},
{"httpbin.org ", "h2c", "http://httpbin.org"},
// This is the default parse section
{"https://httpbin.org ", "https", "https://httpbin.org"},
{"httpbin.org ", "https", "https://httpbin.org"},
{"http://httpbin.org ", "https", "http://httpbin.org"},
Expand All @@ -1594,9 +1611,11 @@ func TestEnsureTransport(t *testing.T) {
for i, v := range cases {
t.Run(fmt.Sprintf("case-%d", i), func(t *testing.T) {
g := EnsureTransport(v.host, v.protocol)
if g != v.expect {
t.Errorf("expected %q got %q", v.expect, g)
}

assert.Equal(t, v.expect, g)

_, err := url.Parse(g)
assert.NoError(t, err)
})
}
}
Expand Down
Loading