mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-03 06:49:38 +08:00
feat: support websocket protocol
This commit is contained in:
1
hrp/tests/demo_file_load_ws_message.txt
Normal file
1
hrp/tests/demo_file_load_ws_message.txt
Normal file
@@ -0,0 +1 @@
|
||||
demo file used for testing load_ws_message function
|
||||
@@ -6,9 +6,9 @@ import (
|
||||
"github.com/httprunner/httprunner/hrp"
|
||||
)
|
||||
|
||||
func TestProtocol(t *testing.T) {
|
||||
func TestHTTPProtocol(t *testing.T) {
|
||||
testcase := &hrp.TestCase{
|
||||
Config: hrp.NewConfig("run request with different protocol types").
|
||||
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").
|
||||
@@ -52,3 +52,76 @@ func TestProtocol(t *testing.T) {
|
||||
t.Fatalf("run testcase error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
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",
|
||||
}),
|
||||
TestSteps: []hrp.IStep{
|
||||
hrp.NewStep("open connection").
|
||||
WebSocket().
|
||||
OpenConnection("/").
|
||||
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("/").
|
||||
WithTimeout(5000),
|
||||
hrp.NewStep("read sponsor info").
|
||||
WebSocket().
|
||||
Read("/").
|
||||
WithTimeout(5000).
|
||||
Validate().
|
||||
AssertContains("body", "Lob.com", "check sponsor message"),
|
||||
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("write and read binary file").
|
||||
WebSocket().
|
||||
WriteAndRead("/").
|
||||
WithBinaryMessage("${load_ws_message($file)}"),
|
||||
hrp.NewStep("write something redundant").
|
||||
WebSocket().
|
||||
Write("/").
|
||||
WithTextMessage("have a nice day!"),
|
||||
hrp.NewStep("write something redundant").
|
||||
WebSocket().
|
||||
Write("/").
|
||||
WithTextMessage("balabala ..."),
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user