diff --git a/api_testsuite.go b/api_testsuite.go index 003758a..e3c2582 100644 --- a/api_testsuite.go +++ b/api_testsuite.go @@ -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)") } @@ -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() diff --git a/pkg/lib/template/template_loader.go b/pkg/lib/template/template_loader.go index dc23935..4074e35 100644 --- a/pkg/lib/template/template_loader.go +++ b/pkg/lib/template/template_loader.go @@ -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" @@ -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) @@ -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 } diff --git a/pkg/lib/template/template_loader_test.go b/pkg/lib/template/template_loader_test.go index 497a28d..c3b839f 100644 --- a/pkg/lib/template/template_loader_test.go +++ b/pkg/lib/template/template_loader_test.go @@ -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 }