refactor: move base_url to config env

This commit is contained in:
debugtalk
2022-05-24 22:23:02 +08:00
parent 717b08c81c
commit ae36c8fd9a
20 changed files with 55 additions and 29 deletions

View File

@@ -1,6 +1,6 @@
# Release History
## v4.1.0 (2022-05-23)
## v4.1.0 (2022-05-24)
- feat: add `wiki` sub-command to open httprunner website
@@ -8,6 +8,7 @@
- fix #1308: load `.env` file as environment variables
- fix #1309: locate plugin file upward recursively until system root dir
- refactor: move base_url to config env
## v4.1.0-beta (2022-05-21)

View File

@@ -0,0 +1,3 @@
base_url=https://postman-echo.com
USERNAME=debugtalk
PASSWORD=123456

View File

@@ -1,4 +1,3 @@
.env
reports/
*.so
.vscode/

View File

@@ -1,6 +1,6 @@
{
"project_name": "demo-with-go-plugin",
"project_path": "/Users/debugtalk/MyProjects/HttpRunner-dev/httprunner/examples/demo-with-go-plugin",
"create_time": "2022-05-24T11:10:10.127839+08:00",
"create_time": "2022-05-24T22:21:23.330967+08:00",
"hrp_version": "v4.1.0-beta"
}

View File

@@ -5,7 +5,6 @@ config:
foo2: config_bar2
expect_foo1: config_bar1
expect_foo2: config_bar2
base_url: "https://postman-echo.com"
verify: False
export: ["foo3"]
@@ -18,7 +17,7 @@ teststeps:
sum_v: "${sum_two_int(1, 2)}"
request:
method: GET
url: /get
url: $base_url/get
params:
foo1: $foo1
foo2: $foo2
@@ -39,7 +38,7 @@ teststeps:
foo3: "bar32"
request:
method: POST
url: /post
url: $base_url/post
headers:
User-Agent: funplugin/${get_version()}
Content-Type: "text/plain"
@@ -53,7 +52,7 @@ teststeps:
foo2: bar23
request:
method: POST
url: /post
url: $base_url/post
headers:
User-Agent: funplugin/${get_version()}
Content-Type: "application/x-www-form-urlencoded"

View File

@@ -0,0 +1,3 @@
base_url=https://postman-echo.com
USERNAME=debugtalk
PASSWORD=123456

View File

@@ -1,4 +1,3 @@
.env
reports/
*.so
.vscode/

View File

@@ -1,6 +1,6 @@
{
"project_name": "demo-with-py-plugin",
"project_path": "/Users/debugtalk/MyProjects/HttpRunner-dev/httprunner/examples/demo-with-py-plugin",
"create_time": "2022-05-24T11:10:15.544763+08:00",
"create_time": "2022-05-24T22:21:30.722372+08:00",
"hrp_version": "v4.1.0-beta"
}

View File

@@ -5,7 +5,6 @@ config:
foo2: config_bar2
expect_foo1: config_bar1
expect_foo2: config_bar2
base_url: "https://postman-echo.com"
verify: False
export: ["foo3"]
@@ -18,7 +17,7 @@ teststeps:
sum_v: "${sum_two_int(1, 2)}"
request:
method: GET
url: /get
url: $base_url/get
params:
foo1: $foo1
foo2: $foo2
@@ -39,7 +38,7 @@ teststeps:
foo3: "bar32"
request:
method: POST
url: /post
url: $base_url/post
headers:
User-Agent: funplugin/${get_version()}
Content-Type: "text/plain"
@@ -53,7 +52,7 @@ teststeps:
foo2: bar23
request:
method: POST
url: /post
url: $base_url/post
headers:
User-Agent: funplugin/${get_version()}
Content-Type: "application/x-www-form-urlencoded"

View File

@@ -0,0 +1,3 @@
base_url=https://postman-echo.com
USERNAME=debugtalk
PASSWORD=123456

View File

@@ -1,4 +1,3 @@
.env
reports/
*.so
.vscode/

View File

@@ -1,6 +1,6 @@
{
"project_name": "demo-without-plugin",
"project_path": "/Users/debugtalk/MyProjects/HttpRunner-dev/httprunner/examples/demo-without-plugin",
"create_time": "2022-05-24T11:10:16.993462+08:00",
"create_time": "2022-05-24T22:21:31.676833+08:00",
"hrp_version": "v4.1.0-beta"
}

View File

@@ -20,10 +20,10 @@ func NewConfig(name string) *TConfig {
type TConfig struct {
Name string `json:"name" yaml:"name"` // required
Verify bool `json:"verify,omitempty" yaml:"verify,omitempty"`
BaseURL string `json:"base_url,omitempty" yaml:"base_url,omitempty"`
Headers map[string]string `json:"headers,omitempty" yaml:"headers,omitempty"`
Env map[string]string `json:"env,omitempty" yaml:"env,omitempty"`
Variables map[string]interface{} `json:"variables,omitempty" yaml:"variables,omitempty"`
BaseURL string `json:"base_url,omitempty" yaml:"base_url,omitempty"` // deprecated in v4.1, moved to env
Headers map[string]string `json:"headers,omitempty" yaml:"headers,omitempty"` // public request headers
Env map[string]string `json:"env,omitempty" yaml:"env,omitempty"` // environment variables
Variables map[string]interface{} `json:"variables,omitempty" yaml:"variables,omitempty"` // global variables
Parameters map[string]interface{} `json:"parameters,omitempty" yaml:"parameters,omitempty"`
ParametersSetting *TParamsConfig `json:"parameters_setting,omitempty" yaml:"parameters_setting,omitempty"`
ThinkTimeSetting *ThinkTimeConfig `json:"think_time,omitempty" yaml:"think_time,omitempty"`

View File

@@ -314,8 +314,7 @@ func parseEnvContent(file []byte, obj interface{}) error {
var kv []string
if strings.Contains(line, "=") {
kv = strings.SplitN(line, "=", 2)
}
if strings.Contains(line, ":") {
} else if strings.Contains(line, ":") {
kv = strings.SplitN(line, ":", 2)
}
if len(kv) != 2 {

View File

@@ -1,2 +1,3 @@
base_url=https://postman-echo.com
USERNAME=debugtalk
PASSWORD=123456

View File

@@ -1,4 +1,3 @@
.env
reports/
*.so
.vscode/

View File

@@ -5,7 +5,6 @@ config:
foo2: config_bar2
expect_foo1: config_bar1
expect_foo2: config_bar2
base_url: "https://postman-echo.com"
verify: False
export: ["foo3"]
@@ -18,7 +17,7 @@ teststeps:
sum_v: "${sum_two_int(1, 2)}"
request:
method: GET
url: /get
url: $base_url/get
params:
foo1: $foo1
foo2: $foo2
@@ -39,7 +38,7 @@ teststeps:
foo3: "bar32"
request:
method: POST
url: /post
url: $base_url/post
headers:
User-Agent: funplugin/${get_version()}
Content-Type: "text/plain"
@@ -53,7 +52,7 @@ teststeps:
foo2: bar23
request:
method: POST
url: /post
url: $base_url/post
headers:
User-Agent: funplugin/${get_version()}
Content-Type: "application/x-www-form-urlencoded"

View File

@@ -287,6 +287,19 @@ func (r *testCaseRunner) parseConfig() error {
}
r.parsedConfig.BaseURL = convertString(parsedBaseURL)
// merge config environment variables with base_url
// priority: env base_url > base_url
r.parsedConfig.Env = cfg.Env
if value, ok := r.parsedConfig.Env["base_url"]; !ok || value == "" {
r.parsedConfig.Env["base_url"] = r.parsedConfig.BaseURL
}
// merge config variables with environment variables
// priority: env > config variables
for k, v := range r.parsedConfig.Env {
r.parsedConfig.Variables[k] = v
}
// ensure correction of think time config
r.parsedConfig.ThinkTimeSetting.checkThinkTime()

View File

@@ -145,7 +145,8 @@ func (r *requestBuilder) prepareUrlParams(stepVariables map[string]interface{})
log.Error().Err(err).Msg("parse request url failed")
return err
}
rawUrl := buildURL(r.config.BaseURL, convertString(requestUrl))
baseURL := stepVariables["base_url"].(string)
rawUrl := buildURL(baseURL, convertString(requestUrl))
// prepare request params
var queryParams url.Values

View File

@@ -83,11 +83,20 @@ func (path *TestCasePath) ToTestCase() (*TestCase, error) {
// load .env file
dotEnvPath := filepath.Join(projectRootDir, ".env")
if builtin.IsFilePathExists(dotEnvPath) {
testCase.Config.Env = make(map[string]string)
err = builtin.LoadFile(dotEnvPath, testCase.Config.Env)
envVars := make(map[string]string)
err = builtin.LoadFile(dotEnvPath, envVars)
if err != nil {
return nil, errors.Wrap(err, "failed to load .env file")
}
// override testcase config env with variables loaded from .env file
// priority: .env file > testcase config env
if testCase.Config.Env == nil {
testCase.Config.Env = make(map[string]string)
}
for key, value := range envVars {
testCase.Config.Env[key] = value
}
}
for _, step := range tc.TestSteps {