feat: support monitoring WebSocket endpoints (#511)
* feat: support monitoring WebSocket endpoints WebSocket endpoints are automatically identified by the URL protocol specification: `wss://` or `ws://`. The request body is used as the "message" written to the server, and the answer is stored in the `[BODY]`. Optionally, the user can set the `jsonrpc` flag to automatically wrap the request body in a JSON RPC 2.0 method call. * core:websocket: close connection after using it * test: add tests related to WebSocket support - test we can identify the endpoint type for WebSockets based on the URL supplied: `wss://` (with SSL/TLS) and `ws://` (plain text). - test we can generate a JsonRPC 2.0 message via the new endpoint flag `JsonRPC`. * core:endpoint: fix name of jsonrpc parameter See https://en.wikipedia.org/wiki/JSON-RPC#Version_2.0 * core:websocket: fix dangling open connection on error Move the `defer ws.Close()` to after opening the connection, so the socket is closed also in case of errors. * remove jsonrpc flag * core:websocket: fix nil pointer dereference The connection should only be closed if successfully opened. * Move websocket function to client * update go.mod * Fix build errors * Fix errors * Update client/client.go --------- Co-authored-by: TwiN <twin@linux.com>
This commit is contained in:
@ -42,6 +42,7 @@ const (
|
||||
EndpointTypeSTARTTLS EndpointType = "STARTTLS"
|
||||
EndpointTypeTLS EndpointType = "TLS"
|
||||
EndpointTypeHTTP EndpointType = "HTTP"
|
||||
EndpointTypeWS EndpointType = "WEBSOCKET"
|
||||
EndpointTypeUNKNOWN EndpointType = "UNKNOWN"
|
||||
)
|
||||
|
||||
@ -149,6 +150,8 @@ func (endpoint Endpoint) Type() EndpointType {
|
||||
return EndpointTypeTLS
|
||||
case strings.HasPrefix(endpoint.URL, "http://") || strings.HasPrefix(endpoint.URL, "https://"):
|
||||
return EndpointTypeHTTP
|
||||
case strings.HasPrefix(endpoint.URL, "ws://") || strings.HasPrefix(endpoint.URL, "wss://"):
|
||||
return EndpointTypeWS
|
||||
default:
|
||||
return EndpointTypeUNKNOWN
|
||||
}
|
||||
@ -340,6 +343,9 @@ func (endpoint *Endpoint) call(result *Result) {
|
||||
result.Duration = time.Since(startTime)
|
||||
} else if endpointType == EndpointTypeICMP {
|
||||
result.Connected, result.Duration = client.Ping(strings.TrimPrefix(endpoint.URL, "icmp://"), endpoint.ClientConfig)
|
||||
} else if endpointType == EndpointTypeWS {
|
||||
result.Connected, result.Body, err = client.QueryWebSocket(endpoint.URL, endpoint.ClientConfig, endpoint.Body)
|
||||
result.Duration = time.Since(startTime)
|
||||
} else {
|
||||
response, err = client.GetHTTPClient(endpoint.ClientConfig).Do(request)
|
||||
result.Duration = time.Since(startTime)
|
||||
|
@ -313,6 +313,18 @@ func TestEndpoint_Type(t *testing.T) {
|
||||
},
|
||||
want: EndpointTypeHTTP,
|
||||
},
|
||||
{
|
||||
args: args{
|
||||
URL: "wss://example.com/",
|
||||
},
|
||||
want: EndpointTypeWS,
|
||||
},
|
||||
{
|
||||
args: args{
|
||||
URL: "ws://example.com/",
|
||||
},
|
||||
want: EndpointTypeWS,
|
||||
},
|
||||
{
|
||||
args: args{
|
||||
URL: "invalid://example.org",
|
||||
|
Reference in New Issue
Block a user