mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-07 06:32:43 +08:00
76 lines
2.7 KiB
Go
76 lines
2.7 KiB
Go
package tests
|
|
|
|
import (
|
|
"testing"
|
|
|
|
hrp "github.com/httprunner/httprunner/v5"
|
|
)
|
|
|
|
func TestCaseBasicRequest(t *testing.T) {
|
|
testcase := &hrp.TestCase{
|
|
Config: hrp.NewConfig("request methods testcase in hardcode").
|
|
SetBaseURL("https://postman-echo.com").
|
|
SetVerifySSL(false),
|
|
TestSteps: []hrp.IStep{
|
|
hrp.NewStep("get with params").
|
|
WithVariables(map[string]interface{}{"headers_key": "\"Content-Type\"", "body_key": "foo1"}).
|
|
GET("/get").
|
|
WithParams(map[string]interface{}{"foo1": "bar1", "foo2": "bar2"}).
|
|
WithHeaders(map[string]string{
|
|
"User-Agent": "HttpRunnerPlus",
|
|
}).
|
|
Validate().
|
|
AssertEqual("status_code", 200, "check status code").
|
|
AssertEqual("headers.$headers_key", "application/json; charset=utf-8", "check header Content-Type").
|
|
AssertEqual("body.args.$body_key", "bar1", "check args foo1").
|
|
AssertEqual("body.args.foo2", "bar2", "check args foo2"),
|
|
hrp.NewStep("post raw text").
|
|
POST("/post").
|
|
WithHeaders(map[string]string{
|
|
"User-Agent": "HttpRunnerPlus",
|
|
"Content-Type": "text/plain",
|
|
}).
|
|
WithBody("This is expected to be sent back as part of response body.").
|
|
Validate().
|
|
AssertEqual("status_code", 200, "check status code").
|
|
AssertEqual("body.data", "This is expected to be sent back as part of response body.", "check data"),
|
|
hrp.NewStep("post form data").
|
|
POST("/post").
|
|
WithHeaders(map[string]string{
|
|
"User-Agent": "HttpRunnerPlus",
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
}).
|
|
WithBody(map[string]interface{}{"foo1": "bar1", "foo2": "bar2"}).
|
|
Validate().
|
|
AssertEqual("status_code", 200, "check status code").
|
|
AssertEqual("body.form.foo1", "bar1", "check form foo1").
|
|
AssertEqual("body.form.foo2", "bar2", "check form foo2"),
|
|
hrp.NewStep("post json data").
|
|
POST("/post").
|
|
WithHeaders(map[string]string{
|
|
"User-Agent": "HttpRunnerPlus",
|
|
}).
|
|
WithBody(map[string]interface{}{"foo1": "bar1", "foo2": "bar2"}).
|
|
Validate().
|
|
AssertEqual("status_code", 200, "check status code").
|
|
AssertEqual("body.json.foo1", "bar1", "check json foo1").
|
|
AssertEqual("body.json.foo2", "bar2", "check json foo2"),
|
|
hrp.NewStep("put request").
|
|
PUT("/put").
|
|
WithHeaders(map[string]string{
|
|
"User-Agent": "HttpRunnerPlus",
|
|
"Content-Type": "text/plain",
|
|
}).
|
|
WithBody("This is expected to be sent back as part of response body.").
|
|
Validate().
|
|
AssertEqual("status_code", 200, "check status code").
|
|
AssertEqual("body.data", "This is expected to be sent back as part of response body.", "check data"),
|
|
},
|
|
}
|
|
|
|
err := hrp.NewRunner(t).Run(testcase)
|
|
if err != nil {
|
|
t.Fatalf("run testcase error: %v", err)
|
|
}
|
|
}
|