The JavaScript ecosystem has been benefiting lately from pieces of its dev tooling being (re)written in Rust. Projects like swc, Parcel 2 and parcel-css, deno, dprint and others have brought us tremendous performance improvements with tasks like bundling, formatting, etc. Recently, my favourite Rust-based, HTTP testing tool gained the ability to be run in node/npm projects, and I wanted to show you how it works.
Hurl is a command-line tool for running HTTP requests defined in simple text files (*.hurl). I learned about it by chance on Twitter over a year ago, and have been using and teaching it to my programming students ever since. The name comes from the fact that it builds on top of curl's HTTP code. The real benefit to Hurl is that it lets you write simple, declarative tests that read just like the HTTP requests and responses they model. Oh, and it runs them ridiculously fast!
Here's an example test file that makes sure http://example.net/404.html returns a 404
:
GET http://example.net/404.html
HTTP/1.0 404
You can get much fancier, by setting headers, cookies, auth, etc. on the request and assert things on the response, including using JSONPath, XPath, Regexes, and lots of other conveniences. You can also capture data from the headers or body, and use these variables in subsequent chained requests. The docs are fantastic (including this tutorial), and go through all the various ways you can write your tests.
Here's a slightly more complex test, which uses a few of the techniques I've just mentioned:
# 1. Get the GitHub user info for @Orange-OpenSource
GET https://api.github.com/users/Orange-OpenSource
# 2. We expect to get back an HTTP/2 200 response. Also, assert
# various things about the Headers and JSON body. Finally
# capture the value of the `blog` property from the body into
# a variable, so we can use that in the next step.
HTTP/2 200
[Asserts]
header "access-control-allow-origin" == "*"
jsonpath "$.login" == "Orange-OpenSource"
jsonpath "$.public_repos" >= 286
jsonpath "$.folowers" isInteger
jsonpath "$.node_id" matches /^[A-Za-z0-9=]+$/
[Captures]
blog_url: jsonpath "$.blog"
# 3. Get the blog URL we received earlier, GET it, and make
# sure it's an HTML page
GET {{blog_url}}
HTTP/2 200
[Asserts]
header "Content-Type" startsWith "text/html"
I've been using Hurl to write tests for node.js HTTP APIs, especially integration tests, and it's been a joy to use. I still write unit tests in JavaScript-based testing frameworks, but one immediate benefit of adding Hurl is its speed, which helps shake out race conditions. Many of my students are still learning asynchronous programming, and often forget to await
Promise-based calls. With JavaScript-based test runners, I've found that the test runs take long enough that the promises usually resolve in time (despite not being await
'ed), and you often don't realize you have a bug. However, when I have the students use Hurl, the tests run so fast that any async
code path that is missing await
becomes obvious: the tests pass in JS but start failing in Hurl.
I also found that Hurl is pretty easy to learn or teach. My AWS Cloud students picked it up really quickly last term, and I think most node.js devs would have no trouble becoming productive with it in a short time. Here's what one of my students wrote about getting started with Hurl in his blog:
"The learning curve is pretty simple (I managed to learn the basics in a couple of hours), less setup todo since it's just a plain file, the syntax is English friendly, besides the jsonPath that could take some times to adapt."
As I've been writing tests and teaching with Hurl over the past year, I've been pretty active filing issues. The devs are really friendly and open to suggestions, and the tool has gotten better and better with each new release. Recently, I filed an issue to add support for running hurl via npm, and it was shipped a little over a week later!
Installing and Using Hurl with npm
Let me show you how to use Hurl in a node.js project. Say you have a directory of *.hurl
files, maybe inside ./test/integration
. First, install Hurl via npm:
$ npm install --save-dev @orangeopensource/hurl
This will download the appropriate Hurl binary for your OS/platform from the associated release, and create node_modules/.bin/hurl
which you can call in your scripts
within package.json
. For example:
"scripts": {
"test:integration": "hurl --test --glob \"test/integration/**/*.hurl\""
}
Here I'm using the --test
(i.e., run in test mode) and --glob
(specify a pattern for input files) options, but there are many more that you can use. NOTE: I'm not showing how to start a server before running these tests, since that's outside the scope of what Hurl does. In my case, I typically run my integration tests against Docker containers, but you could do it lots of ways (e.g., use npm-run-all to start your server before running the tests).
In terms of Hurl's output, running the two tests I discussed above looks like this:
npm test
> hurl-npm-example@1.0.0 test
> hurl --test --glob *.hurl
expr=test1.hurl
test2.hurl: RUNNING [1/2]
error: Assert Failure
--> test2.hurl:14:0
|
14 | jsonpath "$.folowers" isInteger
| actual: none
| expected: integer
|
test2.hurl: FAILURE
test1.hurl: RUNNING [2/2]
error: Assert Http Version
--> test1.hurl:3:6
|
3 | HTTP/1.0 404
| ^^^ actual value is <1.1>
|
test1.hurl: FAILURE
--------------------------------------------------------------------------------
Executed: 2
Succeeded: 0 (0.0%)
Failed: 2 (100.0%)
Duration: 174ms
As you can see, both tests are failing. The error message format is more Rust-like than most JS devs will be used to, but it's quite friendly. In test2.hurl
, I've got a typo in $.folowers
, and in test1.hurl
, the response is returning HTTP/1.1
vs. HTTP/1.0
. A few quick fixes and the tests are now passing:
$ npm test
> hurl-npm-example@1.0.0 test
> hurl --test --glob *.hurl
expr=test1.hurl
test2.hurl: RUNNING [1/2]
test2.hurl: SUCCESS
test1.hurl: RUNNING [2/2]
test1.hurl: SUCCESS
--------------------------------------------------------------------------------
Executed: 2
Succeeded: 2 (100.0%)
Failed: 0 (0.0%)
Duration: 169ms
Part of what's great about Hurl is that it isn't limited to a single language or runtime. Despite the title of my post, Hurl isn't really a JS testing tool per se. However, being able to "npm install
" it and use it as part of your local or CI testing adds something new to your testing toolkit. I still love, use, and teach tools like Jest, Playwright, and others, but I'm excited that JS devs now have an easy way to add Hurl to the mix.
Hopefully this will inspire you to try including Hurl in your node.js HTTP project testing. I promise you that you'll write less test code and spend less time waiting to find out if everything works!