Skip to content

Commit

Permalink
replace_host: removed "port" from new host feature
Browse files Browse the repository at this point in the history
Using "port" in the replace_host func is a tricky thing since the introduction of the SMTP server support.
  • Loading branch information
martinrode committed Jul 12, 2024
1 parent 60ccdb2 commit 35762bb
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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.

Expand Down
26 changes: 19 additions & 7 deletions pkg/lib/template/template_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
29 changes: 29 additions & 0 deletions pkg/lib/template/template_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

}

0 comments on commit 35762bb

Please sign in to comment.