rob-revisited/ide/ui/js/http-model.test.mjs

79 lines
4.3 KiB
JavaScript

// node --test ide/ui/js/http-model.test.mjs: the HTTP Client's JSONPath, URL parameters, curl, bodies and scripts' sandbox.
import test from 'node:test';
import assert from 'node:assert/strict';
import { jsonPath, jsonPathFirst, splitUrl, joinUrl, toCurl, prettyBody, runScript, expect } from './http-model.js';
test('JSONPath', () => {
const data = { data: { items: [{ id: 1, token: 'a' }, { id: 2, meta: { token: 'b' } }] }, token: 'top' };
assert.equal(jsonPathFirst(data, '$.token'), 'top');
assert.equal(jsonPathFirst(data, 'token'), 'top');
assert.equal(jsonPathFirst(data, '$.data.items[-1].id'), 2);
assert.deepEqual(jsonPath(data, "$.data['items'][*].id"), [1, 2]);
assert.deepEqual(jsonPath(data, '$..token'), ['top', 'a', 'b']);
assert.deepEqual(jsonPath(data, '$.nope.x'), []);
assert.throws(() => jsonPath(data, '$.a[?(@.x)]'));
});
test('URL parameters keep variables and order', () => {
const u = splitUrl('{{baseUrl}}/api/books?page=1&title={{q}}&flag#top');
assert.equal(u.base, '{{baseUrl}}/api/books');
assert.deepEqual(u.params, [['page', '1'], ['title', '{{q}}'], ['flag', '']]);
assert.equal(u.hash, '#top');
assert.equal(joinUrl(u.base, [...u.params, ['', 'ignored']], u.hash), '{{baseUrl}}/api/books?page=1&title={{q}}&flag#top');
});
test('copy as curl', () => {
const c = toCurl({ method: 'POST', url: 'https://x/a', headers: [['Content-Type', 'application/json'], ['Host', 'x'], ['X-Q', "it's"]], body: '{"a":1}' });
assert.equal(c, `curl -X POST 'https://x/a' -H 'Content-Type: application/json' -H 'X-Q: it'\\''s' --data-raw '{"a":1}' -L`);
});
test('pretty bodies', () => {
assert.equal(prettyBody('{"a":[1]}', 'application/json').text, '{\n "a": [\n 1\n ]\n}');
assert.equal(prettyBody('<a><b>1</b></a>', 'application/xml').text, '<a>\n <b>1</b>\n</a>');
assert.equal(prettyBody('<p>hi</p>', 'text/html').kind, 'html');
});
test('a post-response script sets variables and runs tests', () => {
const out = runScript(`
client.test("status is 201", () => client.assert(response.status === 201, "bad status"));
client.test("has a title", () => expect(response.body).to.have.property("title", "Dune"));
client.test("fails", () => expect(response.body.id).to.be.above(100));
client.global.set("bookId", response.body.id);
bru.setVar("same", jsonpath(response.body, "$.title")[0]);
test("bruno style", function () { expect(res.getStatus()).to.equal(201); expect(res.getHeader("x-a")).to.equal("1"); });
console.log("id", response.body.id, response.headers.valueOf("X-A"));
`, { phase: 'post', vars: {}, response: { status: 201, headers: [['Content-Type', 'application/ld+json'], ['X-A', '1']], body: '{"id":7,"title":"Dune"}', time: 12 } });
assert.equal(out.error, null);
assert.deepEqual(out.globals, { bookId: '7', same: 'Dune' });
assert.deepEqual(out.tests.map((t) => [t.name, t.passed]), [['status is 201', true], ['has a title', true], ['fails', false], ['bruno style', true]]);
assert.match(out.tests[2].error, /above 100/);
assert.deepEqual(out.logs, ['id 7 1']);
});
test('a pre-request script changes this request only', () => {
const out = runScript(`
request.variables.set("now", "123");
req.setHeader("X-Sig", request.variables.get("now") + client.global.get("key"));
req.setBody('{"a":1}');
`, { phase: 'pre', vars: { key: 'K' }, request: { method: 'POST', url: 'http://x', headers: [], body: '' } });
assert.equal(out.error, null);
assert.deepEqual(out.locals, { now: '123' });
assert.deepEqual(out.request, { headers: [['X-Sig', '123K']], body: '{"a":1}' });
assert.deepEqual(out.globals, {});
});
test('the sandbox has no network and reports errors', () => {
const out = runScript('if (typeof fetch !== "undefined" || typeof XMLHttpRequest !== "undefined" || typeof globalThis !== "undefined") throw new Error("leak"); nope();', {});
assert.match(out.error, /nope is not defined/);
assert.equal(runScript('this.x = 1', {}).error !== null, true, 'strict mode: no global this');
});
test('expect', () => {
expect([1, { a: 2 }]).to.deep.include({ a: 2 });
expect('abc').to.match(/b/);
expect({ a: 1 }).to.not.be.empty;
expect(null).to.be.null;
expect(3).to.be.a('number');
assert.throws(() => expect(1).to.equal(2), /expected 1 to equal 2/);
assert.throws(() => expect([1]).to.not.have.lengthOf(1), /not: /);
});