A WebSocket is a standard protocol for two-way data transfer between a client and server. It is supported by all modern browsers.
Why this post?
My know-how of WebSocket connections was very limited. When I had to test an application which involved a WebSocket connection, I couldn’t find much information on how to go about testing it. It took me a bit of time to research on it and know more about it. Here is an attempt from a tester to make life simple for all my fellow testers who face a similar scenario.
What kind of applications use WebSockets
WebSockets offers connections that are both persistent and bi-directional. This means that the server can actively push information to the client and the client can push information to the server any time. So in case your application needs to get server side data that’s constantly changing WebSockets may be ideal. Applications like social networking sites, chat, gaming, stock market data sites, etc. use WebSockets
How do you begin testing Websockets?
Below are some of the important things you need to know to test WebSocket. We will take https://www.websocket.org/echo.html as an example. In this application, we can open a WebSocket connection by clicking on connect button and also send and receive frame data.
1. How do you track WebSocket using your browser
2. WebSocket Protocol
3. Testing Websocket API
Step1. How do you track WebSocket using your browser
Browsers like Chrome and Firefox helps you track WebSocket traffic.
a. Using Chrome Dev Tools:
Open the developer tools after connecting to https://www.websocket.org/echo.html and open the network tab. Now click on connect. You should be able to see a WS tab which will have the Get request with Status Code: 101 Web Socket Protocol Handshake.
b. Using Websocket Monitor Plugin for Firefox:
Websocket Monitor is a plugin you can add to your Firefox browser. Once you have added the plugin you can a see a tab called Web Sockets when you go to Developer options. This helps you track Web Sockets similarly as you have for chrome browser
Step2. The WebSocket Protocol
The WebSocket Protocol has two parts:
b. Data transfer.
The protocol switch from HTTP to WebSocket is referred to as a WebSocket handshake. The browser sends a request to the server, indicating that it wants to switch protocols from HTTP to WebSocket. The client expresses its desire through the Upgrade header
For the chat application in websocket.org the handshake request looks like this.
Request URL:wss://echo.websocket.org/?encoding=text Request Method:GET
Request Header from client to server looks like this:
Host: echo.websocket.org Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: V/HjGWrwuzgpv+yoWuIhng== Origin: https://www.websocket.org Sec-WebSocket-Version: 13
Status Code:101 Web Socket Protocol Handshake
The Response Header from server looks as follows:
Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: qHHKA5IeLEhMfkYgo1ItVR9Zd70=
Once the client and server have both sent their handshakes, and if the handshake was successful, then the data transfer part starts. This is a two-way communication channel where each side can independently send their data. The data message is composed of one or more frames. For our application, the request and response frames look like below
The WebSocket handshake can also be closed. The close frame may contain a body that indicates a reason for closing, such as endpoint shutting down, an endpoint having received a frame too large, or an endpoint having received a frame that does not conform to the format expected by the endpoint.
Step3. Testing WebSocket API
Now once you know about WebSocket Protocol and how to track the WebSockets we will see how can we test these API’s. At Qxf2 Services we love Python. So naturally this tutorial uses Python.
a. WebSocket client for python:
I will use websocket-client as it is very easy to use.
If you have pip just run the below command to install it.
sudo pip install websocket-client
b. Python code to test WebSocket
This is a simple example using Python to connect to websocket.org. Once a WebSocket connection is established we will send data to server and print the response we receive back from server
import websocket ws = websocket.WebSocket() # Connect to host url ws.connect("wss://echo.websocket.org/") send_string = "Testing WebSocket" print "Sending: ",send_string # Use ws.send() to send data to server ws.send(send_string) # Use ws.recv() to get the data sent from server result = ws.recv() print "Received: ",result # Use ws.close() to close the WebSocket handshake ws.close()
Well, that seems easy now. We would love to hear your feedback and questions in the comments
If you liked this article, learn more about Qxf2’s testing services for startups.