devbcn-workshop icon indicating copy to clipboard operation
devbcn-workshop copied to clipboard

for users of Jetbrains IDEs: a HTTP Client script corresponding to `api.http`

Open bronsen opened this issue 2 years ago • 0 comments

What it is good for

I have taken the vscode testing script file api.http and adapted it to a script for the HTTP Client, which is a free plugin for IDEs from Jetbrains.

It features (grand word, isn't it)

  • distinct environments (local dev, remote shuttle)
  • asserting reponses' status codes
  • tracking of film_id of a newly created film

Where to find the script

There are two files needed. You can find both of them in this gist: https://gist.github.com/bronsen/509f5087b2b463d0268750bd6f236d87

Please make use of them! 😄

Where you could put the script

I have put them in api/lib/tests, alongside health.rs. However, you can put them whereever you like, as long as you keep those two files (rest-api.http, http-client.env.json) together.

How to use the script

  1. Open rest-api.http in your Jetbrains IDE where you have installed the HTTP Client plugin.
  2. At the top of the file tab, select the appropriate environment from Run with:
  3. Click the green double arrow
  4. Observe the results

How the script works

Just like the original api.http file, it's a bunch of http calls (GET, POST, etc) seperated by markers (###). There are placeholders (for example {{host}}), that are filled from the corresponding env file.

GET {{host}}/health HTTP/1.1

HTTP Client also has so-called Response handlers, which are javascript and can access the response object of the previous call, and can also set variables. One such variable is film_id that is used to fill the {{film_id}} placeholder.

#shortened example
POST {{host}}/v1/films 

{"some": "data"}

> {%
client.global.set("film_id", response.body["id"]);
# from now on we can use {{film_id}} in the script
%}

I hope this is useful to others, who follow that exhaustive tutorial 😅 👍🏾


bronsen avatar Jul 09 '23 15:07 bronsen