diff --git a/README.md b/README.md index 9041165..0593a88 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ You can also set the log verbosity per single testcase. The greater verbosity wi | `--server URL` | | Overwrites base url to the api | | `--report-file newReportFile` | | Overwrites the report file name from the `apitest.yml` config with `newReportFile` | | `--report-format junit` | | Overwrites the report format from the `apitest.yml` config with `junit` | -| `--replace-host [host][:port]` | | Overwrites built-in server host in template function `replace_host` | +| `--replace-host host` | | Overwrites built-in server host in template function `replace_host` | ### Additional parameters @@ -120,7 +120,7 @@ You can also set the log verbosity per single testcase. The greater verbosity wi - Run all tests in the directory **apitests** with **http server host replacement** for those templates using **replace_host** template function ```bash -./apitest -d apitests --replace-host my.fancy.host:8989 +./apitest -d apitests --replace-host my.fancy.host ``` # Manifest @@ -2455,7 +2455,7 @@ Example how to range over 100 objects ## `replace_host [url]` -**replace_host** replaces the host and port in the given `url` with the actual address of the built-in HTTP server (see below). This address, taken from the `manifest.json` can be overwritten with the command line parameter `--replace-host`. +**replace_host** replaces the host in the given `url` with the actual address of the built-in HTTP server (see below). This address, taken from the `manifest.json` can be overwritten with the command line parameter `--replace-host`. As an example, the URL `http://localhost/myimage.jpg` would be changed into `http://localhost:8788/myimage.jpg` following the example below. diff --git a/pkg/lib/template/template_loader.go b/pkg/lib/template/template_loader.go index 522eef5..dc23935 100644 --- a/pkg/lib/template/template_loader.go +++ b/pkg/lib/template/template_loader.go @@ -349,13 +349,7 @@ func (loader *Loader) Render( if loader.HTTPServerHost == "" { return srcURL, nil } - // Parse source URL or fail - parsedURL, err := url.Parse(srcURL) - if err != nil { - return "", err - } - parsedURL.Host = loader.HTTPServerHost - return parsedURL.String(), nil + return replaceHost(srcURL, loader.HTTPServerHost) }, "server_url": func() *url.URL { u := new(url.URL) @@ -522,3 +516,21 @@ func getRowsFromInput(rowsInput any) []map[string]any { } return rows } + +// replaceHost uses host of serverHost and replaces it in srcURL +func replaceHost(srcURL, serverHost string) (string, error) { + if strings.Contains(serverHost, ":") { + return "", fmt.Errorf("replaceHost: host must not include scheme or port") + } + // Parse source URL or fail + parsedURL, err := url.Parse(srcURL) + if err != nil { + return "", err + } + if parsedURL.Host == "" && parsedURL.Scheme != "" { + parsedURL.Scheme = serverHost + } else { + parsedURL.Host = serverHost + } + return parsedURL.String(), nil +} diff --git a/pkg/lib/template/template_loader_test.go b/pkg/lib/template/template_loader_test.go index 7d0c15a..497a28d 100644 --- a/pkg/lib/template/template_loader_test.go +++ b/pkg/lib/template/template_loader_test.go @@ -7,6 +7,7 @@ import ( "github.com/programmfabrik/apitest/pkg/lib/datastore" "github.com/programmfabrik/apitest/pkg/lib/test_utils" + "github.com/stretchr/testify/assert" "github.com/programmfabrik/apitest/pkg/lib/api" "github.com/programmfabrik/apitest/pkg/lib/filesystem" @@ -456,5 +457,33 @@ func Test_DataStore_QJson(t *testing.T) { test_utils.AssertJsonStringEquals(t, string(res), testCase.expected) }) } +} + +func TestReplaceHost(t *testing.T) { + var ( + h string + err error + ) + + h, err = replaceHost("localhost:9925", "192.168.122.56:8978") + if !assert.Error(t, err) { + return + } + + h, err = replaceHost("localhost:9925", "192.168.122.56") + if !assert.NoError(t, err) { + return + } + if !assert.Equal(t, "192.168.122.56:9925", 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) { + return + } }