postmanlabs / postman-app-support

Postman is an API platform for building and using APIs. Postman simplifies each step of the API lifecycle and streamlines collaboration so you can create better APIs—faster.
https://www.postman.com
5.78k stars 837 forks source link

Enable scripting for websockets #10181

Open tibi-extera opened 2 years ago

tibi-extera commented 2 years ago

Is there an existing request for this feature?

Is your feature request related to a problem?

My API allows for subscriptions for notification via WebSocket. While testing the API & WebSocket subscriptions, I need to be able to create the subscription at login time (as in connecting to the WebSocket) and test the notification content generated when I invoke other API endpoints.

Describe the solution you'd like

I would like to handle this in the test and/or the pre-request script for the login request and/or other API requests.

Ideally, I would be able to write code in the test for the login request to create the WebSocket connection and code in the test for each of the API requests to validate that the proper notification happens.

For completeness, I would also like to have a "Test" tab in the WebSocket request editor, where I can script tests for whatever communication my API supports over the WebSocket. This would allow me to build and send messages, as well as receive and parse responses.

Having the ability to use variables when building messages or in any JavaScript related to WebSockets is a must.

Finally, WebSocket requests must be allowed to be part of collections, to allow me better organization of my tests.

Describe alternatives you've considered

No response

Additional context

No response

shamasis commented 2 years ago

Saving WebSocket to collection is scheduled up next to allow for sharing as well as documentation. Scripting (testing, etc) is to follow immediately after.

I'm curious to know, how do you currently test WebSockets? It will greatly help us understand what's the best way to enable testing in Postman.

nguoithichkhampha commented 2 years ago

Our sofware provide API via websocket protocol. We are planning to use postman as automation tool for API testing. Allow to run test and scripting in websocket collection is very useful feature. Can I know when this feature available @shamasis ?

shamasis commented 2 years ago

The plan is to integrate one RPC style protocol and then address testing and scripting at one go. I'm estimating we can start planning within weeks and wrap it up within the quarter one of 2022.

nguoithichkhampha commented 2 years ago

great, we are also using Json RPC 2.0 for the request/response via websocket. Hope we can use new feature soon. Thanks @shamasis

pe-jot commented 2 years ago

As a first step, it would be great to be able to simply open and close a Websocket, e.g. via JavaScript's Websocket() constructor. Our system in many cases needs a websocket connection to be established, even not communicating through it in the particular situation. We could increase our test coverage in a great amount by having this simple way available.

nguoithichkhampha commented 2 years ago

@shamasis , we are going to the end of Q1 2022. Can you share a bit about the progress ?

shamasis commented 1 year ago

It's coming. We will be adding support for scripting in GRPC first. Hopefully early next month. Followed by WebSocket in a few weeks after that.

For WebSocket we need to design a neat way to do scripts. Few interesting questions for us to answer - when will these scripts run? What kind of tests or other stuff will the scripts be used for? How does connect/disconnect events affect scripting?

Would love to listen to more about how everyone wants to use scripting in WebSocket. 😞

brandonmaul commented 1 year ago

@shamasis I can help answer your question. My team uses an Azure SignalR Service to send realtime messages over websocket. We are already using postman for our API's integration tests, but cannot properly test these SignalR messages coming in over websocket using postman.

Ideally, we'd want test scripts to run after a websocket message is received to verify the contents of it.

We'd be initiating the websocket message by hitting an endpoint on our API, and would also like to track the time between the initial API request and receiving the websocket message. Hope this helps!

shamasis commented 1 year ago

@brandonmaul I wish GitHub had a real-time messaging / chat. I wanna run some thoughts by you - I am @shamasis on Twitter / LinkedIn ... can you ping me anywhere on either of them?

brandonmaul commented 1 year ago

@shamasis No problem! Sent you a message on LinkedIn

tibi-extera commented 1 year ago

My API generates real-time messages via the websocket when endpoints are invoked. I would like to be able to a. confirm that the messages are sent when they should be sent b. confirm that the messages are sent to the correct websocket (as in messages for a specific user do not end up at some other user). c. confirm that the messages have the correct content

vladimir-kouznetsov commented 1 year ago

Could you please increase priority of adding Pre-request Script to WebSocket? “Pre-request Script” is very useful for testing Authorization, and I’m missing it on Postman WebSocket a lot,

martin-cll commented 9 months ago

Seconding vladimir, we currently use prerequest scripts to test and save auth for HTTP requests and would also love to use it on our WS endpoints as well.

brxxxx commented 5 months ago

It would be great if we could run a script that would allow us to parse a websocket message, as json, and then save some value into a variable.

diana-little-xylem commented 5 months ago

Adding onto what vladimir and martin said, we use prerequest scripts for auth which expire within seconds, and would love to use it for websockets as well.

TomBailey167 commented 3 weeks ago

I have to do a lot of copying and pasting from one response into another request.

Whether in a script (as suggested by brxxxx) or just through some UI on the WebSocket request page for the time being, it would be great to have a way to extract data from messages into variables. For JSON something like a "set variable x to value y on condition m":

variable condition value
myVar message["key"] == "x" message["value"]

Scripting would also be really useful. Again if there was a way to parse for a condition in a received message, and on that condition send another message.

Automatic validation against a JSON Schema would also be good but that likely falls beyond the scope of this issue.

zhanzhenzhen commented 3 weeks ago

+1. I have a WebSocket API to test, and it uses HTTP Basic Authentication. I cannot set the username and password as environment variables, but have to manually convert them to a Base64 string and set that long Base64 string as a variable. For WebSocket, Postman currently neither supports scripting nor the Authorization tab, making Base64 automatic encoding impossible.