optimize websocket step

This commit is contained in:
buyuxiang
2022-07-11 18:25:43 +08:00
parent 28d5f414d0
commit aa5de4ec83
6 changed files with 278 additions and 119 deletions

View File

@@ -56,38 +56,39 @@ func TestHTTPProtocol(t *testing.T) {
func TestWebSocketProtocol(t *testing.T) {
testcase := &hrp.TestCase{
Config: hrp.NewConfig("run request with WebSocket protocol").
SetBaseURL("ws://echo.websocket.events").
WithVariables(map[string]interface{}{
"n": 5,
"a": 12.3,
"b": 3.45,
"file": "./demo_file_load_ws_message.txt",
"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("/").
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("/").
PingPong("$wsEchoURL").
WithTimeout(5000),
hrp.NewStep("read sponsor info").
WebSocket().
Read("/").
Read("$wsEchoURL").
WithTimeout(5000).
Validate().
AssertContains("body", "Lob.com", "check sponsor message"),
hrp.NewStep("write json").
WebSocket().
Write("/").
Write("$wsEchoURL").
WithTextMessage(map[string]interface{}{"foo1": "${gen_random_string($n)}", "foo2": "${max($a, $b)}"}),
hrp.NewStep("read json").
WebSocket().
Read("/").
Read("$wsEchoURL").
Extract().
WithJmesPath("body.foo1", "varFoo1").
Validate().
@@ -95,25 +96,163 @@ func TestWebSocketProtocol(t *testing.T) {
AssertEqual("body.foo2", 12.3, "check json foo2"),
hrp.NewStep("write and read text").
WebSocket().
WriteAndRead("/").
WriteAndRead("$wsEchoURL").
WithTextMessage("$varFoo1").
Validate().
AssertLengthEqual("body", 5, "check length equal"),
hrp.NewStep("write and read binary file").
WebSocket().
WriteAndRead("/").
WriteAndRead("$wsEchoURL").
WithBinaryMessage("${load_ws_message($file)}"),
hrp.NewStep("write something redundant").
WebSocket().
Write("/").
Write("$wsEchoURL").
WithTextMessage("have a nice day!"),
hrp.NewStep("write something redundant").
WebSocket().
Write("/").
Write("$wsEchoURL").
WithTextMessage("balabala ..."),
hrp.NewStep("close connection").
WebSocket().
CloseConnection("/").
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().