Open In App

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.

Key Concepts of WebSocket

How WebSocket Works

1. WebSocket Handshake:

The client sends an HTTP request to the server, requesting to upgrade the connection to WebSocket. If the server supports WebSocket, it responds with an HTTP 101 status code (“Switching Protocols”), indicating a successful upgrade to WebSocket.



2. WebSocket Connection Establishment:

Once the handshake is complete, a WebSocket connection is established over a single TCP connection. Both the client and server can now send messages to each other directly without the overhead of HTTP headers.

3. Data Exchange:

The client and server exchange messages asynchronously over the WebSocket connection. Messages can be sent in either text or binary format.

Features of WebSocket

  1. WebSocket Requests: Postman allows you to create WebSocket requests just like HTTP requests. This includes defining WebSocket endpoints and customizing headers.
  2. Message Sending and Receiving: You can send both text and binary messages through WebSocket connections in Postman and view the server’s responses in real-time.
  3. Response Validation: Postman supports assertions on WebSocket responses, enabling you to validate the content, status, or headers of WebSocket messages.
  4. Scripting: Utilize Postman’s scripting capabilities (using JavaScript) to manipulate WebSocket messages, perform dynamic validations, or handle complex workflows during testing.

Approach

Testing WebSocket APIs with Postman

Step 1: Create a New WebSocket Request

new web socket request

Step 2: Enter WebSocket URL

Enter the URL

Step 3: Send a Message

Message Entered

Step 4: Set Up Assertions

// 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

Example 1: Simple WebSocket Connection

  1. Open Postman and create a new request.
  2. Change the request type to “WebSocket.”
  3. Enter the WebSocket URL (e.g., ws://example.com/socket).
  4. Send a text message ({“action”: “hello”}).
  5. Observe the server’s response in the Postman console.

Hitting websocket URL

Example 2: WebSocket with Assertions

  1. Create a WebSocket request as before.
  2. Send a message and expect a specific response.
  3. Use Postman scripts to validate the received message format.
  4. Assert the response status or content.

Response

Article Tags :