Skip to content

Commit

Permalink
--replace-host: fixed func and command line
Browse files Browse the repository at this point in the history
Replace host is only taking a hostname (no longer a port as in previous versions). This patch fixes some missed cases.
  • Loading branch information
martinrode committed Jul 15, 2024
1 parent 35762bb commit af151cc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 23 deletions.
23 changes: 3 additions & 20 deletions api_testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,8 @@ func NewTestSuite(config TestToolConfig, manifestPath string, manifestDir string
}

// Add external http server url here, as only after this point the http_server.addr may be available
hsru := new(url.URL)
shsu := new(url.URL)
if httpServerReplaceHost != "" {
hsru, err = url.Parse("//" + httpServerReplaceHost)
_, err = url.Parse("//" + httpServerReplaceHost)
if err != nil {
return nil, errors.Wrap(err, "set http_server_host failed (command argument)")
}
Expand All @@ -115,27 +113,12 @@ func NewTestSuite(config TestToolConfig, manifestPath string, manifestDir string
preloadHTTPAddrStr = ":80"
}
// We need to append it as the golang URL parser is not smart enough to differenciate between hostname and protocol
shsu, err = url.Parse("//" + preloadHTTPAddrStr)
_, err = url.Parse("//" + preloadHTTPAddrStr)
if err != nil {
return nil, errors.Wrap(err, "set http_server_host failed (manifesr addr)")
}
}
suitePreload.HTTPServerHost = ""
if hsru.Hostname() != "" {
suitePreload.HTTPServerHost = hsru.Hostname()
} else if shsu.Hostname() != "" {
suitePreload.HTTPServerHost = shsu.Hostname()
} else {
suitePreload.HTTPServerHost = "localhost"
}
if suite.HTTPServerHost == "0.0.0.0" {
suitePreload.HTTPServerHost = "localhost"
}
if hsru.Port() != "" {
suitePreload.HTTPServerHost += ":" + hsru.Port()
} else if shsu.Port() != "" {
suitePreload.HTTPServerHost += ":" + shsu.Port()
}
suitePreload.HTTPServerHost = httpServerReplaceHost

// Here we load the usable manifest, now that we can do all potential replacements
manifest, err = suitePreload.loadManifest()
Expand Down
10 changes: 8 additions & 2 deletions pkg/lib/template/template_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/Masterminds/sprig/v3"
"github.com/pkg/errors"
"github.com/programmfabrik/apitest/pkg/lib/datastore"
"github.com/programmfabrik/golib"
"github.com/sirupsen/logrus"
"golang.org/x/mod/semver"
"golang.org/x/oauth2"
Expand Down Expand Up @@ -518,9 +519,12 @@ func getRowsFromInput(rowsInput any) []map[string]any {
}

// replaceHost uses host of serverHost and replaces it in srcURL
func replaceHost(srcURL, serverHost string) (string, error) {
func replaceHost(srcURL, serverHost string) (s string, err error) {
defer func() {
golib.Pln("replace %q -> %q = %q", srcURL, serverHost, s)
}()
if strings.Contains(serverHost, ":") {
return "", fmt.Errorf("replaceHost: host must not include scheme or port")
return "", fmt.Errorf("replaceHost: host %q must not include scheme or port", serverHost)
}
// Parse source URL or fail
parsedURL, err := url.Parse(srcURL)
Expand All @@ -529,6 +533,8 @@ func replaceHost(srcURL, serverHost string) (string, error) {
}
if parsedURL.Host == "" && parsedURL.Scheme != "" {
parsedURL.Scheme = serverHost
} else if parsedURL.Port() != "" {
parsedURL.Host = serverHost + ":" + parsedURL.Port()
} else {
parsedURL.Host = serverHost
}
Expand Down
10 changes: 9 additions & 1 deletion pkg/lib/template/template_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,11 +478,19 @@ func TestReplaceHost(t *testing.T) {
return
}

h, err = replaceHost("http://localhost:8978/images", "martins.mac")
if !assert.NoError(t, err) {
return
}
if !assert.Equal(t, "http://martins.mac:8978/images", h) {
return
}

h, err = replaceHost("http://localhost:8978", "192.168.122.56")
if !assert.NoError(t, err) {
return
}
if !assert.Equal(t, "http://192.168.122.56", h) {
if !assert.Equal(t, "http://192.168.122.56:8978", h) {
return
}

Expand Down

0 comments on commit af151cc

Please sign in to comment.