Testing WebSocket APIs with Postman
Step 1: Create a New WebSocket Request
- Open Postman and click on “New” to create a new request.
- Select “WebSocket” as the request type.
Step 2: Enter WebSocket URL
- Enter the WebSocket URL you want to connect to (e.g., wss://echo.websocket.org).
Step 3: Send a Message
- Go to the “Message” tab within the WebSocket request.
- Enter a message you want to send to the WebSocket server (e.g., “Hello, WebSocket!”).
Step 4: Set Up Assertions
- Switch to the “Tests” tab in Postman.
- Write JavaScript code to validate the response received from the WebSocket server.
// Validate the response received from the WebSocket server
pm.test("Check WebSocket Response", function () {
// Parse the response data from the message received
const responseData = pm.response.text();
// Define the expected response message
const expectedMessage = "Hello, WebSocket!";
// Assert that the response message matches the expected message
pm.expect(responseData).to.equal(expectedMessage);
});
Step 5: Send the WebSocket Request
- Click on “Send” to execute the WebSocket request.
- Postman will establish a WebSocket connection, send the message, and receive a response from the server.
Example 1: Simple WebSocket Connection
- Open Postman and create a new request.
- Change the request type to “WebSocket.”
- Enter the WebSocket URL (e.g., ws://example.com/socket).
- Send a text message ({“action”: “hello”}).
- Observe the server’s response in the Postman console.
Example 2: WebSocket with Assertions
- Create a WebSocket request as before.
- Send a message and expect a specific response.
- Use Postman scripts to validate the received message format.
- Assert the response status or content.
Features postman gives to work with Sockets.
Let’s Look at some feature which comes with tests in Postman to work with Sockets. Below is the image of output of one of the socket connection.
So let’s understand the features postman gives to work with Sockets.
1. Message : Here we can write the message to send to the socket.
2. Params: It is a key-value based list which is used to pass the parameters with the message.
3. Headers: It is used to send any particular header like Authorisation header.
4. Responses: In responses we will have all the messages in list. It will have Sent messages and Received messages.
5. Handshake Details: Also with connection we will have all the Details related to Handshake.
Also we can see how message is passed and received with socket.
How to Test WebSocket APIs With Postman?
WebSocket is a communication protocol that provides full-duplex communication channels over a single, long-lived connection between clients and servers. Unlike HTTP, which is a request-response protocol, WebSocket allows both the client and server to send messages to each other independently at any time, enabling real-time data transfer.
Table of Content
- Key Concepts of WebSocket
- How WebSocket Works
- Features of WebSocket
- Approach
- Testing WebSocket APIs with Postman
Contact Us