Swell 0.8.0: Testing Your Streaming APIs

Write your unit and integration tests in Swell

Header image for article; Just says “API testing.”
Credit: Shutterstock

From writing your first fizz-buzz solution to solving those Hard problems on Leetcode testing and debugging code is an inevitable but necessary part of becoming a competent software engineer. Whether you are new to full-stack development or have years of experience, endpoint testing is crucial. Frontend components are just empty shells hungry for data to consume without a functioning backend API.

Test-driven development (or TDD for short) is a software development process that requires all features of a given product to be testable and be fully compliant with that testing suite before being deployed. TDD encourages developers to write higher quality code. It both validates the correctness of the code and helps drive the design process by having developers focus only on the features wanted by end-users. Practicing TDD commonly requires the usage of a library like Chai to make assertion statements that need to resolve as true in order for code to be considered correct.

Swell 0.8.0 now includes a new scripting feature based on Node.js that allows you to write automated behavior to occur on responses from RESTful, GraphQL, and gRPC endpoints. This allows you to write test suites that can be a part of your TDD cycle to validate endpoint behavior.

What can you do?

After composing a request, click the “View Tests” button and that will reveal the code editor where you can begin to write JavaScript. Swell utilizes the popular Chai assertion library as the underlying tool for you to write assertion statements. While you can write any JavaScript, we implemented the feature with the intent of enabling Swell users to write their own test suites.

A screen recording of Swell’s new testing feature being used.
Compose your test suite directly in Swell

You can access the response data by simply accessing the response object. This object will resolve to the object that Swell constructs upon receipt of API data. Below are some of the properties on the response object you could make assertions on but note that any changes made to this object have no effect on the actual response object kept in the Redux store.

  • .headers — An object containing all the headers included in the response from the server. Each associated value for a given header is an array that contains one or more values.
  • .events An array containing the body of the response data.

If a GET request is made to an endpoint serving an HTML file, response.events would be an array containing one element, the stringified HTML. If the endpoint returns JSON as the response, such as with GraphQL, then response.events would be contain the payload data. In the case of GraphQL subscriptions or gRPC streams, the array would be populated with multiple message objects.

  • .status The status code of the response code for HTTP requests.

What else can you do?

In conjunction with the scripting feature, you can now compose several requests, stage them in the center workspace pane, and then automate their execution. Instead of needing to manually hit send, there is now a “Send Collection” button that will fire off each request in the order that the requests were added.

A demo of Swell’s collection runner feature.
Stage a collection of requests to run in succession

Additionally, in the new Schedule tab in the center workspace pane, you can automate the sending of requests to occur on a periodic basis. A user could leave Swell running in the background and it would send a collection of requests every hour and run the associated tests once the response comes back. This is useful because now a developer can now partly automate the process of ensuring their API is functioning throughout different parts of the day and be notified if services are not behaving as expected.

A demo of Swell’s scheduling feature.
A demo of Swell’s scheduling feature.
Schedule requests to automatically run on an interval

Lastly, you can now also open a rendered preview of a response if the endpoint returns an HTML document.

A demo of Swell’s additional new features including rendering HTML responses
A demo of Swell’s additional new features including rendering HTML responses
Render a preview of HTML responses

How does it work?

Under the hood, the JavaScript a developer writes is sent to a sandboxed runtime instantiated with VM2. This ephemeral environment exists only for a brief time in the main Electron process and its purpose is to execute the script. It has limited access to process methods and restrictions on built-in and external modules. This protects the user from potentially malicious scripts being hidden in Swell workspaces that are being shared over the internet.

The only external library currently allowed to be imported using require is Chai, which Swell does for you automatically.

Inspiration

Postman logo
The current leader for collaborative API development

We would be remiss in not acknowledging the most popular tool for developing and testing RESTful endpoints, Postman. In fact, the comprehensive scripting and testing features in Postman was the main inspiration for us to bring this feature to Swell.

Postman is used by over 500,000 different companies and 10 million developers worldwide. To say that level of adoption and success is commendable is only an understatement.

Insomnia logo

Insomnia is another popular API testing tool that already included support for REST and GraphQL and also just announced support for gRPC in early December 2020.

Insomnia offers a beautiful and responsive UI and has a flourishing and active open-source community.

Conclusion

Swell’s logo
Testing for streaming APIs, including RESTful, GraphQL, and gRPC
  • Swell is a free and open-source, desktop Electron application. Now with version 0.8.0, it offers a scripting feature that enables developers to create test suites to validate the behavior of their API endpoints.
  • Swell also now has the ability to launch a collection of requests that will run in ordered succession and run any associated tests.
  • It can now also schedule requests to run on a periodic basis (e.g. once an hour) to help a developer gain a better understanding of how their API functions throughout the day/week/month etc.
  • A minor addition is that there is also a preview in the response pane that renders the response received, such as an HTML document.
  • Swell draws some inspiration from the two big names in the API tooling space, namely Postman and Insomnia, but offers more functionality with the additional support for WebSockets, gRPC, and GraphQL.
  • WebSocket persistent connections are easily testable with simple messaging support but can also support listening and displaying a stream after subscription.
  • Unary, server-side, client-side, and bidirectional gRPC streams are supported to aid in the development and testing of backend microservices.
  • While Swell is still young and yet to reach a major 1.0.0 release, its growth has been significant and it continues to offer many useful features to any API developer.
  • Our core team is tiny in comparison to Postman or Insomnia but kept in active development. If you’re curious to check out Swell, please visit our Github page! We welcome any comments or issues and are always looking for ways to improve.

Get Swell!

This article was co-authored by

Software Engineer