Last active
November 24, 2024 04:29
-
-
Save lambrospetrou/4e07bf79abea9fd82b52d1a6f985405c to your computer and use it in GitHub Desktop.
Hurl test suite for the Cloudflare D1 REST API as explained in https://www.lambrospetrou.com/articles/hurl-cloudflare-d1/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Cloudflare D1 REST API testing suite. | |
# https://www.lambrospetrou.com/articles/hurl-cloudflare-d1/ | |
# Try to create the test database! | |
# This can fail if the database with the same name exists already, so we will check | |
# if this fails and then delete and re-create it. | |
POST https://api.cloudflare.com/client/v4/accounts/{{ CLOUDFLARE_ACC_ID }}/d1/database | |
Authorization: Bearer {{ CLOUDFLARE_TOKEN }} | |
{ | |
"name": "skybear-test-001", | |
"primary_location_hint": "weur" | |
} | |
HTTP * | |
[Captures] | |
db_created: jsonpath "$.success" | |
# List databases to find the ID of the same-named existing one. | |
GET https://api.cloudflare.com/client/v4/accounts/{{ CLOUDFLARE_ACC_ID }}/d1/database | |
Authorization: Bearer {{ CLOUDFLARE_TOKEN }} | |
HTTP 200 | |
[Captures] | |
db_id: jsonpath "$.result[?(@.name == 'skybear-test-001')].uuid" nth 0 | |
# Conditionally delete existing database. | |
DELETE https://api.cloudflare.com/client/v4/accounts/{{ CLOUDFLARE_ACC_ID }}/d1/database/{{ db_id }} | |
Authorization: Bearer {{ CLOUDFLARE_TOKEN }} | |
[Options] | |
skip: {{ db_created }} | |
HTTP 200 | |
# Create a new one again if we have deleted it above. | |
POST https://api.cloudflare.com/client/v4/accounts/{{ CLOUDFLARE_ACC_ID }}/d1/database | |
Authorization: Bearer {{ CLOUDFLARE_TOKEN }} | |
[Options] | |
skip: {{ db_created }} | |
{ | |
"name": "skybear-test-001", | |
"primary_location_hint": "weur" | |
} | |
HTTP 200 | |
[Asserts] | |
jsonpath "$.success" == true | |
[Captures] | |
db_id: jsonpath "$.result.uuid" | |
############################ | |
# We have a fresh database! | |
# GET Database details | |
GET https://api.cloudflare.com/client/v4/accounts/{{ CLOUDFLARE_ACC_ID }}/d1/database/{{ db_id }} | |
Authorization: Bearer {{ CLOUDFLARE_TOKEN }} | |
HTTP 200 | |
[Asserts] | |
jsonpath "$.success" == true | |
jsonpath "$.result.uuid" == {{ db_id }} | |
jsonpath "$.result.name" == "skybear-test-001" | |
jsonpath "$.result.running_in_region" == "WEUR" | |
############ | |
# SQL Query | |
POST https://api.cloudflare.com/client/v4/accounts/{{ CLOUDFLARE_ACC_ID }}/d1/database/{{ db_id }}/query | |
Authorization: Bearer {{ CLOUDFLARE_TOKEN }} | |
Content-Type: application/json | |
{"sql": "CREATE TABLE IF NOT EXISTS marvel (name TEXT, power INTEGER); SELECT name, type FROM sqlite_master ORDER BY name ASC;"} | |
HTTP 200 | |
[Asserts] | |
jsonpath "$.success" == true | |
jsonpath "$.result[0].success" == true | |
jsonpath "$.result[0].results" count == 0 | |
jsonpath "$.result[1].success" == true | |
jsonpath "$.result[1].results[0].name" == "_cf_KV" | |
jsonpath "$.result[1].results[0].type" == "table" | |
jsonpath "$.result[1].results[1].name" == "marvel" | |
jsonpath "$.result[1].results[1].type" == "table" | |
# SQL Duration in the Durable Object should be FAST! (less than 2ms) | |
jsonpath "$.result[0].meta.duration" < 2.0 | |
jsonpath "$.result[1].meta.duration" < 2.0 | |
################ | |
# SQL Query RAW | |
POST https://api.cloudflare.com/client/v4/accounts/{{ CLOUDFLARE_ACC_ID }}/d1/database/{{ db_id }}/raw | |
Authorization: Bearer {{ CLOUDFLARE_TOKEN }} | |
Content-Type: application/json | |
{"sql": "CREATE TABLE IF NOT EXISTS marvel (name TEXT, power INTEGER); SELECT name, type FROM sqlite_master ORDER BY name ASC;"} | |
HTTP 200 | |
[Asserts] | |
jsonpath "$.success" == true | |
jsonpath "$.result[0].success" == true | |
jsonpath "$.result[0].results.columns" count == 0 | |
jsonpath "$.result[0].results.rows" count == 0 | |
jsonpath "$.result[1].success" == true | |
jsonpath "$.result[1].results.columns[0]" == "name" | |
jsonpath "$.result[1].results.columns[1]" == "type" | |
jsonpath "$.result[1].results.rows[0][0]" == "_cf_KV" | |
jsonpath "$.result[1].results.rows[0][1]" == "table" | |
jsonpath "$.result[1].results.rows[1][0]" == "marvel" | |
jsonpath "$.result[1].results.rows[1][1]" == "table" | |
# SQL Duration in the Durable Object should be FAST! (less than 2ms) | |
jsonpath "$.result[0].meta.duration" < 2.0 | |
jsonpath "$.result[1].meta.duration" < 2.0 | |
# Cleanup. | |
DELETE https://api.cloudflare.com/client/v4/accounts/{{ CLOUDFLARE_ACC_ID }}/d1/database/{{ db_id }} | |
Authorization: Bearer {{ CLOUDFLARE_TOKEN }} | |
HTTP 200 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment