mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-11 18:11:21 +08:00
refactor: move hrp/ to root folder
This commit is contained in:
1
tests/demo_file_load_ws_message.txt
Normal file
1
tests/demo_file_load_ws_message.txt
Normal file
@@ -0,0 +1 @@
|
||||
demo file used for testing load_ws_message function
|
||||
85
tests/extract_test.go
Normal file
85
tests/extract_test.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
hrp "github.com/httprunner/httprunner/v5"
|
||||
)
|
||||
|
||||
// reference extracted variables for validation in the same step
|
||||
func TestCaseExtractStepSingle(t *testing.T) {
|
||||
testcase := &hrp.TestCase{
|
||||
Config: hrp.NewConfig("run request with variables").
|
||||
SetBaseURL("https://postman-echo.com").
|
||||
SetVerifySSL(false),
|
||||
TestSteps: []hrp.IStep{
|
||||
hrp.NewStep("get with params").
|
||||
WithVariables(map[string]interface{}{
|
||||
"var1": "bar1",
|
||||
"agent": "HttpRunnerPlus",
|
||||
"expectedStatusCode": 200,
|
||||
"jmespathFoo1": "body.args.foo1",
|
||||
}).
|
||||
GET("/get").
|
||||
WithParams(map[string]interface{}{"foo1": "$var1", "foo2": "bar2"}).
|
||||
WithHeaders(map[string]string{"User-Agent": "$agent"}).
|
||||
Extract().
|
||||
WithJmesPath("status_code", "statusCode").
|
||||
WithJmesPath("headers.\"Content-Type\"", "contentType").
|
||||
WithJmesPath("$jmespathFoo1", "varFoo1").
|
||||
Validate().
|
||||
AssertEqual("$statusCode", "$expectedStatusCode", "check status code"). // assert with extracted variable from current step
|
||||
AssertEqual("$contentType", "application/json; charset=utf-8", "check header Content-Type"). // assert with extracted variable from current step
|
||||
AssertEqual("$varFoo1", "bar1", "check args foo1"). // assert with extracted variable from current step
|
||||
AssertEqual("body.args.foo2", "bar2", "check args foo2").
|
||||
AssertEqual("body.headers.\"user-agent\"", "HttpRunnerPlus", "check header user agent"),
|
||||
},
|
||||
}
|
||||
|
||||
err := hrp.NewRunner(t).Run(testcase)
|
||||
if err != nil {
|
||||
t.Fatalf("run testcase error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// reference extracted variables from previous step
|
||||
func TestCaseExtractStepAssociation(t *testing.T) {
|
||||
testcase := &hrp.TestCase{
|
||||
Config: hrp.NewConfig("run request with variables").
|
||||
SetBaseURL("https://postman-echo.com").
|
||||
SetVerifySSL(false),
|
||||
TestSteps: []hrp.IStep{
|
||||
hrp.NewStep("get with params").
|
||||
WithVariables(map[string]interface{}{
|
||||
"var1": "bar1",
|
||||
"agent": "HttpRunnerPlus",
|
||||
}).
|
||||
GET("/get").
|
||||
WithParams(map[string]interface{}{"foo1": "$var1", "foo2": "bar2"}).
|
||||
WithHeaders(map[string]string{"User-Agent": "$agent"}).
|
||||
Extract().
|
||||
WithJmesPath("status_code", "statusCode").
|
||||
WithJmesPath("headers.\"Content-Type\"", "contentType").
|
||||
WithJmesPath("body.args.foo1", "varFoo1").
|
||||
Validate().
|
||||
AssertEqual("$statusCode", 200, "check status code").
|
||||
AssertEqual("$contentType", "application/json; charset=utf-8", "check header Content-Type").
|
||||
AssertEqual("$varFoo1", "bar1", "check args foo1").
|
||||
AssertEqual("body.args.foo2", "bar2", "check args foo2").
|
||||
AssertEqual("body.headers.\"user-agent\"", "HttpRunnerPlus", "check header user agent"),
|
||||
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", "$statusCode", "check status code"). // assert with extracted variable from previous step
|
||||
AssertEqual("$varFoo1", "bar1", "check json foo1"). // assert with extracted variable from previous step
|
||||
AssertEqual("body.json.foo2", "bar2", "check json foo2"),
|
||||
},
|
||||
}
|
||||
|
||||
err := hrp.NewRunner(t).Run(testcase)
|
||||
if err != nil {
|
||||
t.Fatalf("run testcase error: %v", err)
|
||||
}
|
||||
}
|
||||
49
tests/function_test.go
Normal file
49
tests/function_test.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
hrp "github.com/httprunner/httprunner/v5"
|
||||
)
|
||||
|
||||
func TestCaseCallFunction(t *testing.T) {
|
||||
testcase := &hrp.TestCase{
|
||||
Config: hrp.NewConfig("run request with functions").
|
||||
SetBaseURL("https://postman-echo.com").
|
||||
WithVariables(map[string]interface{}{
|
||||
"n": 5,
|
||||
"a": 12.3,
|
||||
"b": 3.45,
|
||||
}).
|
||||
SetVerifySSL(false),
|
||||
TestSteps: []hrp.IStep{
|
||||
hrp.NewStep("get with params").
|
||||
GET("/get").
|
||||
WithParams(map[string]interface{}{"foo1": "${gen_random_string($n)}", "foo2": "${max($a, $b)}", "foo3": "Foo3"}).
|
||||
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
|
||||
Extract().
|
||||
WithJmesPath("body.args.foo1", "varFoo1").
|
||||
Validate().
|
||||
AssertEqual("status_code", 200, "check status code").
|
||||
AssertLengthEqual("body.args.foo1", 5, "check args foo1").
|
||||
AssertEqual("body.args.foo2", "12.3", "check args foo2").
|
||||
AssertTypeMatch("body.args.foo3", "str", "check args foo3 is type string").
|
||||
AssertEqualFold("body.args.foo3", "foo3", "check args foo3 case-insensitivity").
|
||||
AssertContains("body.args.foo3", "Foo", "check contains ").
|
||||
AssertContainedBy("body.args.foo3", "this is Foo3 test", "check contained by"), // notice: request params value will be converted to string
|
||||
hrp.NewStep("post json data with functions").
|
||||
POST("/post").
|
||||
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
|
||||
WithBody(map[string]interface{}{"foo1": "${gen_random_string($n)}", "foo2": "${max($a, $b)}"}).
|
||||
Validate().
|
||||
AssertEqual("status_code", 200, "check status code").
|
||||
AssertLengthEqual("body.json.foo1", 5, "check args foo1").
|
||||
AssertEqual("body.json.foo2", 12.3, "check args foo2"),
|
||||
},
|
||||
}
|
||||
|
||||
err := hrp.NewRunner(t).Run(testcase)
|
||||
if err != nil {
|
||||
t.Fatalf("run testcase error: %v", err)
|
||||
}
|
||||
}
|
||||
266
tests/protocol_test.go
Normal file
266
tests/protocol_test.go
Normal file
@@ -0,0 +1,266 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
hrp "github.com/httprunner/httprunner/v5"
|
||||
)
|
||||
|
||||
func TestHTTPProtocol(t *testing.T) {
|
||||
testcase := &hrp.TestCase{
|
||||
Config: hrp.NewConfig("run request with HTTP/1.1 and HTTP/2").
|
||||
SetBaseURL("https://postman-echo.com"),
|
||||
TestSteps: []hrp.IStep{
|
||||
hrp.NewStep("HTTP/1.1 get").
|
||||
GET("/get").
|
||||
WithParams(map[string]interface{}{"foo1": "foo1", "foo2": "foo2"}).
|
||||
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
|
||||
Validate().
|
||||
AssertEqual("status_code", 200, "check status code").
|
||||
AssertEqual("proto", "HTTP/1.1", "check protocol type").
|
||||
AssertLengthEqual("body.args.foo1", 4, "check param foo1"),
|
||||
hrp.NewStep("HTTP/1.1 post").
|
||||
POST("/post").
|
||||
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
|
||||
WithBody(map[string]interface{}{"foo1": "foo1", "foo2": "foo2"}).
|
||||
Validate().
|
||||
AssertEqual("status_code", 200, "check status code").
|
||||
AssertEqual("proto", "HTTP/1.1", "check protocol type").
|
||||
AssertLengthEqual("body.json.foo1", 4, "check body foo1"),
|
||||
hrp.NewStep("HTTP/2 get").
|
||||
HTTP2().
|
||||
GET("/get").
|
||||
WithParams(map[string]interface{}{"foo1": "foo1", "foo2": "foo2"}).
|
||||
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
|
||||
Validate().
|
||||
AssertEqual("status_code", 200, "check status code").
|
||||
AssertEqual("proto", "HTTP/2.0", "check protocol type").
|
||||
AssertLengthEqual("body.args.foo1", 4, "check param foo1"),
|
||||
hrp.NewStep("HTTP/2 post").
|
||||
HTTP2().
|
||||
POST("/post").
|
||||
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
|
||||
WithBody(map[string]interface{}{"foo1": "foo1", "foo2": "foo2"}).
|
||||
Validate().
|
||||
AssertEqual("status_code", 200, "check status code").
|
||||
AssertEqual("proto", "HTTP/2.0", "check protocol type").
|
||||
AssertLengthEqual("body.json.foo1", 4, "check body foo1"),
|
||||
},
|
||||
}
|
||||
err := hrp.NewRunner(t).Run(testcase)
|
||||
if err != nil {
|
||||
t.Fatalf("run testcase error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebSocketProtocol(t *testing.T) {
|
||||
testcase := &hrp.TestCase{
|
||||
Config: hrp.NewConfig("run request with WebSocket protocol").
|
||||
WithVariables(map[string]interface{}{
|
||||
"n": 5,
|
||||
"a": 12.3,
|
||||
"b": 3.45,
|
||||
"file": "./demo_file_load_ws_message.txt",
|
||||
"wsEchoURL": "ws://echo.websocket.events",
|
||||
"wsPostmanURL": "wss://ws.postman-echo.com/raw",
|
||||
}),
|
||||
TestSteps: []hrp.IStep{
|
||||
hrp.NewStep("open connection").
|
||||
WebSocket().
|
||||
OpenConnection("$wsEchoURL"). // absolute url specified, disable base url anyway
|
||||
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
|
||||
Validate().
|
||||
AssertEqual("status_code", 101, "check open status code").
|
||||
AssertEqual("headers.Connection", "Upgrade", "check headers"),
|
||||
hrp.NewStep("ping pong test").
|
||||
WebSocket().
|
||||
PingPong("$wsEchoURL").
|
||||
WithTimeout(5000),
|
||||
hrp.NewStep("read sponsor info").
|
||||
WebSocket().
|
||||
Read("$wsEchoURL").
|
||||
WithTimeout(5000).
|
||||
Validate().
|
||||
AssertContains("body", "Lob.com", "check sponsor message"),
|
||||
hrp.NewStep("write json").
|
||||
WebSocket().
|
||||
Write("$wsEchoURL").
|
||||
WithTextMessage(map[string]interface{}{"foo1": "${gen_random_string($n)}", "foo2": "${max($a, $b)}"}),
|
||||
hrp.NewStep("read json").
|
||||
WebSocket().
|
||||
Read("$wsEchoURL").
|
||||
Extract().
|
||||
WithJmesPath("body.foo1", "varFoo1").
|
||||
Validate().
|
||||
AssertLengthEqual("body.foo1", 5, "check json foo1").
|
||||
AssertEqual("body.foo2", 12.3, "check json foo2"),
|
||||
hrp.NewStep("write and read text").
|
||||
WebSocket().
|
||||
WriteAndRead("$wsEchoURL").
|
||||
WithTextMessage("$varFoo1").
|
||||
Validate().
|
||||
AssertLengthEqual("body", 5, "check length equal"),
|
||||
hrp.NewStep("write and read binary file").
|
||||
WebSocket().
|
||||
WriteAndRead("$wsEchoURL").
|
||||
WithBinaryMessage("${load_ws_message($file)}"),
|
||||
hrp.NewStep("write something redundant").
|
||||
WebSocket().
|
||||
Write("$wsEchoURL").
|
||||
WithTextMessage("have a nice day!"),
|
||||
hrp.NewStep("write something redundant").
|
||||
WebSocket().
|
||||
Write("$wsEchoURL").
|
||||
WithTextMessage("balabala ..."),
|
||||
hrp.NewStep("close connection").
|
||||
WebSocket().
|
||||
CloseConnection("$wsEchoURL").
|
||||
WithTimeout(30000).
|
||||
WithCloseStatus(1000).
|
||||
Validate().
|
||||
AssertEqual("status_code", 1000, "check close status code"),
|
||||
hrp.NewStep("[postman-echo] open connection").
|
||||
WebSocket().
|
||||
OpenConnection("$wsPostmanURL").
|
||||
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
|
||||
Validate().
|
||||
AssertEqual("status_code", 101, "check open status code").
|
||||
AssertEqualFold("headers.Connection", "Upgrade", "check headers").
|
||||
AssertEqualFold("headers.Server", "nginx", "check server").
|
||||
AssertEqualFold("headers.Upgrade", "websocket", "checkout upgrade"),
|
||||
hrp.NewStep("[postman-echo] write json").
|
||||
WebSocket().
|
||||
Write("$wsPostmanURL").
|
||||
WithTextMessage(map[string]interface{}{"foo1": "${gen_random_string($n)}", "foo2": "${max($a, $b)}"}),
|
||||
hrp.NewStep("[postman-echo] read json").
|
||||
WebSocket().
|
||||
Read("$wsPostmanURL").
|
||||
Validate().
|
||||
AssertLengthEqual("body.foo1", 5, "check json foo1").
|
||||
AssertEqual("body.foo2", 12.3, "check json foo2"),
|
||||
hrp.NewStep("[postman-echo] write and read text").
|
||||
WebSocket().
|
||||
WriteAndRead("$wsPostmanURL").
|
||||
WithTextMessage("$varFoo1").
|
||||
Validate().
|
||||
AssertLengthEqual("body", 5, "check length equal"),
|
||||
hrp.NewStep("[postman-echo] close connection").
|
||||
WebSocket().
|
||||
CloseConnection("$wsPostmanURL").
|
||||
WithTimeout(30000).
|
||||
WithCloseStatus(1000).
|
||||
Validate().
|
||||
AssertEqual("status_code", 1000, "check close status code"),
|
||||
},
|
||||
}
|
||||
err := hrp.NewRunner(t).Run(testcase)
|
||||
if err != nil {
|
||||
t.Fatalf("run testcase error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebSocketProtocolUsingRelativeURL(t *testing.T) {
|
||||
testcase := &hrp.TestCase{
|
||||
Config: hrp.NewConfig("run request with WebSocket protocol").
|
||||
SetBaseURL("wss://ws.postman-echo.com").
|
||||
WithVariables(map[string]interface{}{
|
||||
"n": 5,
|
||||
"a": 12.3,
|
||||
"b": 3.45,
|
||||
"file": "./demo_file_load_ws_message.txt",
|
||||
}),
|
||||
TestSteps: []hrp.IStep{
|
||||
hrp.NewStep("open connection").
|
||||
WebSocket().
|
||||
OpenConnection("/raw"). // relative url specified ==> wss://ws.postman-echo.com/raw
|
||||
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
|
||||
Validate().
|
||||
AssertEqual("status_code", 101, "check open status code").
|
||||
AssertEqualFold("headers.Connection", "Upgrade", "check headers").
|
||||
AssertEqualFold("headers.Server", "nginx", "check server").
|
||||
AssertEqualFold("headers.Upgrade", "websocket", "checkout upgrade"),
|
||||
hrp.NewStep("write json").
|
||||
WebSocket().
|
||||
Write("/raw").
|
||||
WithTextMessage(map[string]interface{}{"foo1": "${gen_random_string($n)}", "foo2": "${max($a, $b)}"}),
|
||||
hrp.NewStep("read json").
|
||||
WebSocket().
|
||||
Read("/raw").
|
||||
Extract().
|
||||
WithJmesPath("body.foo1", "varFoo1").
|
||||
Validate().
|
||||
AssertLengthEqual("body.foo1", 5, "check json foo1").
|
||||
AssertEqual("body.foo2", 12.3, "check json foo2"),
|
||||
hrp.NewStep("write and read text").
|
||||
WebSocket().
|
||||
WriteAndRead("/raw").
|
||||
WithTextMessage("$varFoo1").
|
||||
Validate().
|
||||
AssertLengthEqual("body", 5, "check length equal"),
|
||||
hrp.NewStep("close connection").
|
||||
WebSocket().
|
||||
CloseConnection("/raw").
|
||||
WithTimeout(30000).
|
||||
WithCloseStatus(1000).
|
||||
Validate().
|
||||
AssertEqual("status_code", 1000, "check close status code"),
|
||||
},
|
||||
}
|
||||
err := hrp.NewRunner(t).Run(testcase)
|
||||
if err != nil {
|
||||
t.Fatalf("run testcase error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebSocketProtocolUsingBaseURL(t *testing.T) {
|
||||
testcase := &hrp.TestCase{
|
||||
Config: hrp.NewConfig("run request with WebSocket protocol").
|
||||
SetBaseURL("wss://ws.postman-echo.com/raw").
|
||||
WithVariables(map[string]interface{}{
|
||||
"n": 5,
|
||||
"a": 12.3,
|
||||
"b": 3.45,
|
||||
"file": "./demo_file_load_ws_message.txt",
|
||||
}),
|
||||
TestSteps: []hrp.IStep{
|
||||
hrp.NewStep("open connection").
|
||||
WebSocket().
|
||||
OpenConnection(). // no url specified, using base url instead
|
||||
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
|
||||
Validate().
|
||||
AssertEqual("status_code", 101, "check open status code").
|
||||
AssertEqualFold("headers.Connection", "Upgrade", "check headers").
|
||||
AssertEqualFold("headers.Server", "nginx", "check server").
|
||||
AssertEqualFold("headers.Upgrade", "websocket", "checkout upgrade"),
|
||||
hrp.NewStep("write json").
|
||||
WebSocket().
|
||||
Write().
|
||||
WithTextMessage(map[string]interface{}{"foo1": "${gen_random_string($n)}", "foo2": "${max($a, $b)}"}),
|
||||
hrp.NewStep("read json").
|
||||
WebSocket().
|
||||
Read().
|
||||
Extract().
|
||||
WithJmesPath("body.foo1", "varFoo1").
|
||||
Validate().
|
||||
AssertLengthEqual("body.foo1", 5, "check json foo1").
|
||||
AssertEqual("body.foo2", 12.3, "check json foo2"),
|
||||
hrp.NewStep("write and read text").
|
||||
WebSocket().
|
||||
WriteAndRead().
|
||||
WithTextMessage("$varFoo1").
|
||||
Validate().
|
||||
AssertLengthEqual("body", 5, "check length equal"),
|
||||
hrp.NewStep("close connection").
|
||||
WebSocket().
|
||||
CloseConnection().
|
||||
WithTimeout(30000).
|
||||
WithCloseStatus(1000).
|
||||
Validate().
|
||||
AssertEqual("status_code", 1000, "check close status code"),
|
||||
},
|
||||
}
|
||||
err := hrp.NewRunner(t).Run(testcase)
|
||||
if err != nil {
|
||||
t.Fatalf("run testcase error: %v", err)
|
||||
}
|
||||
}
|
||||
53
tests/rendezvous_test.go
Normal file
53
tests/rendezvous_test.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
hrp "github.com/httprunner/httprunner/v5"
|
||||
)
|
||||
|
||||
func TestRendezvous(t *testing.T) {
|
||||
testcase := &hrp.TestCase{
|
||||
Config: hrp.NewConfig("run request with rendezvous").
|
||||
SetBaseURL("https://postman-echo.com").
|
||||
WithVariables(map[string]interface{}{
|
||||
"n": 5,
|
||||
"a": 12.3,
|
||||
"b": 3.45,
|
||||
}),
|
||||
TestSteps: []hrp.IStep{
|
||||
hrp.NewStep("waiting for all users in the beginning").
|
||||
SetRendezvous("rendezvous0"),
|
||||
hrp.NewStep("rendezvous before get").
|
||||
SetRendezvous("rendezvous1").
|
||||
WithUserNumber(50).
|
||||
WithTimeout(3000),
|
||||
hrp.NewStep("get with params").
|
||||
GET("/get").
|
||||
WithParams(map[string]interface{}{"foo1": "foo1", "foo2": "foo2"}).
|
||||
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
|
||||
Extract().
|
||||
WithJmesPath("body.args.foo1", "varFoo1").
|
||||
Validate().
|
||||
AssertEqual("status_code", 200, "check status code"),
|
||||
hrp.NewStep("rendezvous before post").
|
||||
SetRendezvous("rendezvous2").
|
||||
WithUserNumber(20).
|
||||
WithTimeout(2000),
|
||||
hrp.NewStep("post json data with functions").
|
||||
POST("/post").
|
||||
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
|
||||
WithBody(map[string]interface{}{"foo1": "foo1", "foo2": "foo2"}).
|
||||
Validate().
|
||||
AssertEqual("status_code", 200, "check status code").
|
||||
AssertLengthEqual("body.json.foo1", 4, "check args foo1").
|
||||
AssertEqual("body.json.foo2", "foo2", "check args foo2"),
|
||||
hrp.NewStep("waiting for all users in the end").
|
||||
SetRendezvous("rendezvous3"),
|
||||
},
|
||||
}
|
||||
err := hrp.NewRunner(t).Run(testcase)
|
||||
if err != nil {
|
||||
t.Fatalf("run testcase error: %v", err)
|
||||
}
|
||||
}
|
||||
75
tests/request_test.go
Normal file
75
tests/request_test.go
Normal file
@@ -0,0 +1,75 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
4
tests/test.env
Normal file
4
tests/test.env
Normal file
@@ -0,0 +1,4 @@
|
||||
UserName=test
|
||||
Password=654321
|
||||
PROJECT_KEY=AAABBBCCC
|
||||
content_type=application/json; charset=UTF-8
|
||||
64
tests/upload_test.go
Normal file
64
tests/upload_test.go
Normal file
@@ -0,0 +1,64 @@
|
||||
//go:build localtest
|
||||
|
||||
package tests
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
hrp "github.com/httprunner/httprunner/v5"
|
||||
)
|
||||
|
||||
func TestCaseUploadFile(t *testing.T) {
|
||||
testcase := &hrp.TestCase{
|
||||
Config: hrp.NewConfig("test upload file to httpbin").
|
||||
SetBaseURL("https://httpbin.org").
|
||||
WithVariables(map[string]interface{}{"upload_file": "test.env"}),
|
||||
TestSteps: []hrp.IStep{
|
||||
hrp.NewStep("upload file explicitly").
|
||||
WithVariables(map[string]interface{}{
|
||||
"m_encoder": "${multipart_encoder($m_upload)}",
|
||||
"m_upload": map[string]interface{}{"file": "@$upload_file"},
|
||||
}).
|
||||
POST("/post").
|
||||
WithHeaders(map[string]string{"Content-Type": "${multipart_content_type($m_encoder)}"}).
|
||||
WithBody("$m_encoder").
|
||||
Validate().
|
||||
AssertEqual("status_code", 200, "check status code").
|
||||
AssertStartsWith("body.files.file", "UserName=test", "check uploaded file"),
|
||||
hrp.NewStep("upload both text and file").
|
||||
POST("/post").
|
||||
WithUpload(map[string]interface{}{
|
||||
"foo1": "\"bar1\"",
|
||||
"foo2": "\"@$upload_file\"",
|
||||
"foo3": "\"\"@$upload_file\"\"",
|
||||
"file1": "@\"$upload_file\"",
|
||||
"file2": "@$upload_file",
|
||||
}).
|
||||
Validate().
|
||||
AssertEqual("status_code", 200, "check status code").
|
||||
AssertEqual("body.form.foo1", "bar1", "check foo1 in form").
|
||||
AssertEqual("body.form.foo2", "@$upload_file", "check foo2 in form").
|
||||
AssertEqual("body.form.foo3", "\"@$upload_file\"", "check foo3 in form").
|
||||
AssertStartsWith("body.files.file1", "UserName=test", "check uploaded file1").
|
||||
AssertStartsWith("body.files.file2", "UserName=test", "check uploaded file2"),
|
||||
hrp.NewStep("upload empty field").
|
||||
POST("/post").
|
||||
WithUpload(map[string]interface{}{
|
||||
"foo1": "",
|
||||
"foo2": "\"\"",
|
||||
"foo3": "\"\";",
|
||||
"dummy": ";filename=empty",
|
||||
}).
|
||||
Validate().
|
||||
AssertEqual("status_code", 200, "check status code").
|
||||
AssertEqual("body.form.foo1", "", "check foo1 in form").
|
||||
AssertEqual("body.form.foo2", "", "check foo2 in form").
|
||||
AssertEqual("body.files.dummy", "", "check dummy file in files"),
|
||||
},
|
||||
}
|
||||
|
||||
err := hrp.NewRunner(t).Run(testcase)
|
||||
if err != nil {
|
||||
t.Fatalf("run testcase error: %v", err)
|
||||
}
|
||||
}
|
||||
55
tests/validate_test.go
Normal file
55
tests/validate_test.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
hrp "github.com/httprunner/httprunner/v5"
|
||||
)
|
||||
|
||||
func TestCaseValidateStep(t *testing.T) {
|
||||
testcase := &hrp.TestCase{
|
||||
Config: hrp.NewConfig("run request with validation").
|
||||
SetBaseURL("https://postman-echo.com").
|
||||
SetVerifySSL(false),
|
||||
TestSteps: []hrp.IStep{
|
||||
hrp.NewStep("get with params").
|
||||
WithVariables(map[string]interface{}{
|
||||
"var1": "bar1",
|
||||
"agent": "HttpRunnerPlus",
|
||||
"expectedStatusCode": 200,
|
||||
}).
|
||||
GET("/get").
|
||||
WithParams(map[string]interface{}{"foo1": "$var1", "foo2": "bar2"}).
|
||||
WithHeaders(map[string]string{"User-Agent": "$agent"}).
|
||||
Extract().
|
||||
WithJmesPath("body.args.foo1", "varFoo1").
|
||||
Validate().
|
||||
AssertEqual("status_code", "$expectedStatusCode", "check status code"). // assert status code
|
||||
AssertEqual("headers.\"Content-Type\"", "application/json; charset=utf-8", "check header Content-Type"). // assert response header, with double quotes
|
||||
AssertEqual("body.args.foo1", "bar1", "check args foo1"). // assert response json body with jmespath
|
||||
AssertEqual("body.args.foo2", "bar2", "check args foo2").
|
||||
AssertEqual("body.headers.\"user-agent\"", "HttpRunnerPlus", "check header user agent"),
|
||||
hrp.NewStep("get with params").
|
||||
WithVariables(map[string]interface{}{
|
||||
"var1": "bar1",
|
||||
"agent": "HttpRunnerPlus",
|
||||
}).
|
||||
GET("/get").
|
||||
WithParams(map[string]interface{}{"foo1": "$var1", "foo2": "bar2"}).
|
||||
WithHeaders(map[string]string{"User-Agent": "$agent"}).
|
||||
Extract().
|
||||
WithJmesPath("status_code", "statusCode").
|
||||
WithJmesPath("headers.\"Content-Type\"", "contentType").
|
||||
Validate().
|
||||
AssertEqual("$statusCode", 200, "check status code"). // assert with extracted variable from current step
|
||||
AssertEqual("$contentType", "application/json; charset=utf-8", "check header Content-Type"). // assert with extracted variable from current step
|
||||
AssertEqual("$varFoo1", "bar1", "check args foo1"). // assert with extracted variable from previous step
|
||||
AssertEqual("body.args.foo2", "bar2", "check args foo2"), // assert response json body with jmespath
|
||||
},
|
||||
}
|
||||
|
||||
err := hrp.NewRunner(t).Run(testcase)
|
||||
if err != nil {
|
||||
t.Fatalf("run testcase error: %v", err)
|
||||
}
|
||||
}
|
||||
144
tests/variables_test.go
Normal file
144
tests/variables_test.go
Normal file
@@ -0,0 +1,144 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
hrp "github.com/httprunner/httprunner/v5"
|
||||
)
|
||||
|
||||
func TestCaseConfigVariables(t *testing.T) {
|
||||
testcase := &hrp.TestCase{
|
||||
Config: hrp.NewConfig("run request with variables").
|
||||
SetBaseURL("https://postman-echo.com").
|
||||
WithVariables(map[string]interface{}{
|
||||
"var1": "bar1",
|
||||
"agent": "HttpRunnerPlus",
|
||||
"expectedStatusCode": 200,
|
||||
}).SetVerifySSL(false),
|
||||
TestSteps: []hrp.IStep{
|
||||
hrp.NewStep("get with params").
|
||||
GET("/get").
|
||||
WithParams(map[string]interface{}{"foo1": "$var1", "foo2": "bar2"}).
|
||||
WithHeaders(map[string]string{"User-Agent": "$agent"}).
|
||||
Validate().
|
||||
AssertEqual("status_code", "$expectedStatusCode", "check status code").
|
||||
AssertEqual("headers.\"Content-Type\"", "application/json; charset=utf-8", "check header Content-Type").
|
||||
AssertEqual("body.args.foo1", "bar1", "check args foo1").
|
||||
AssertEqual("body.args.foo2", "bar2", "check args foo2").
|
||||
AssertEqual("body.headers.\"user-agent\"", "HttpRunnerPlus", "check header user agent"),
|
||||
},
|
||||
}
|
||||
|
||||
err := hrp.NewRunner(t).Run(testcase)
|
||||
if err != nil {
|
||||
t.Fatalf("run testcase error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCaseStepVariables(t *testing.T) {
|
||||
testcase := &hrp.TestCase{
|
||||
Config: hrp.NewConfig("run request with variables").
|
||||
SetBaseURL("https://postman-echo.com").
|
||||
SetVerifySSL(false),
|
||||
TestSteps: []hrp.IStep{
|
||||
hrp.NewStep("get with params").
|
||||
WithVariables(map[string]interface{}{
|
||||
"var1": "bar1",
|
||||
"agent": "HttpRunnerPlus",
|
||||
"expectedStatusCode": 200,
|
||||
}).
|
||||
GET("/get").
|
||||
WithParams(map[string]interface{}{"foo1": "$var1", "foo2": "bar2"}).
|
||||
WithHeaders(map[string]string{"User-Agent": "$agent"}).
|
||||
Validate().
|
||||
AssertEqual("status_code", "$expectedStatusCode", "check status code").
|
||||
AssertEqual("headers.\"Content-Type\"", "application/json; charset=utf-8", "check header Content-Type").
|
||||
AssertEqual("body.args.foo1", "bar1", "check args foo1").
|
||||
AssertEqual("body.args.foo2", "bar2", "check args foo2").
|
||||
AssertEqual("body.headers.\"user-agent\"", "HttpRunnerPlus", "check header user agent"),
|
||||
},
|
||||
}
|
||||
|
||||
err := hrp.NewRunner(t).Run(testcase)
|
||||
if err != nil {
|
||||
t.Fatalf("run testcase error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCaseOverrideConfigVariables(t *testing.T) {
|
||||
testcase := &hrp.TestCase{
|
||||
Config: hrp.NewConfig("run request with variables").
|
||||
SetBaseURL("https://postman-echo.com").
|
||||
WithVariables(map[string]interface{}{
|
||||
"var1": "bar0",
|
||||
"agent": "HttpRunnerPlus",
|
||||
"expectedStatusCode": 200,
|
||||
}).SetVerifySSL(false),
|
||||
TestSteps: []hrp.IStep{
|
||||
hrp.NewStep("get with params").
|
||||
WithVariables(map[string]interface{}{
|
||||
"var1": "bar1", // override config variable
|
||||
"agent": "$agent", // reference config variable
|
||||
// expectedStatusCode, inherit config variable
|
||||
}).
|
||||
GET("/get").
|
||||
WithParams(map[string]interface{}{"foo1": "$var1", "foo2": "bar2"}).
|
||||
WithHeaders(map[string]string{"User-Agent": "$agent"}).
|
||||
Validate().
|
||||
AssertEqual("status_code", "$expectedStatusCode", "check status code").
|
||||
AssertEqual("headers.\"Content-Type\"", "application/json; charset=utf-8", "check header Content-Type").
|
||||
AssertEqual("body.args.foo1", "bar1", "check args foo1").
|
||||
AssertEqual("body.args.foo2", "bar2", "check args foo2").
|
||||
AssertEqual("body.headers.\"user-agent\"", "HttpRunnerPlus", "check header user agent"),
|
||||
},
|
||||
}
|
||||
|
||||
err := hrp.NewRunner(t).Run(testcase)
|
||||
if err != nil {
|
||||
t.Fatalf("run testcase error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCaseParseVariables(t *testing.T) {
|
||||
testcase := &hrp.TestCase{
|
||||
Config: hrp.NewConfig("run request with functions").
|
||||
SetBaseURL("https://postman-echo.com").
|
||||
WithVariables(map[string]interface{}{
|
||||
"n": 5,
|
||||
"a": 12.3,
|
||||
"b": 3.45,
|
||||
"varFoo1": "${gen_random_string($n)}",
|
||||
"varFoo2": "${max($a, $b)}", // 12.3
|
||||
}).SetVerifySSL(false),
|
||||
TestSteps: []hrp.IStep{
|
||||
hrp.NewStep("get with params").
|
||||
WithVariables(map[string]interface{}{
|
||||
"n": 3,
|
||||
"b": 34.5,
|
||||
"varFoo2": "${max($a, $b)}", // 34.5
|
||||
}).
|
||||
GET("/get").
|
||||
WithParams(map[string]interface{}{"foo1": "$varFoo1", "foo2": "$varFoo2"}).
|
||||
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
|
||||
Extract().
|
||||
WithJmesPath("body.args.foo1", "varFoo1").
|
||||
Validate().
|
||||
AssertEqual("status_code", 200, "check status code").
|
||||
AssertLengthEqual("body.args.foo1", 5, "check args foo1").
|
||||
AssertEqual("body.args.foo2", "34.5", "check args foo2"), // notice: request params value will be converted to string
|
||||
hrp.NewStep("post json data with functions").
|
||||
POST("/post").
|
||||
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
|
||||
WithBody(map[string]interface{}{"foo1": "${gen_random_string($n)}", "foo2": "${max($a, $b)}"}).
|
||||
Validate().
|
||||
AssertEqual("status_code", 200, "check status code").
|
||||
AssertLengthEqual("body.json.foo1", 5, "check args foo1").
|
||||
AssertEqual("body.json.foo2", 12.3, "check args foo2"),
|
||||
},
|
||||
}
|
||||
|
||||
err := hrp.NewRunner(t).Run(testcase)
|
||||
if err != nil {
|
||||
t.Fatalf("run testcase error: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user