From fc91577562f15345d9362c2cb1b5a58362b1d8f9 Mon Sep 17 00:00:00 2001 From: Nathan Givens Date: Thu, 7 Apr 2022 11:30:32 -0400 Subject: [PATCH 1/2] add test cases for `X-OADA-Ensure-Link` header --- test/get.test.ts | 18 ++++++++++++++++++ test/put.test.ts | 22 ++++++++++++++++++++++ test/utils.ts | 13 +++++++++++-- 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/test/get.test.ts b/test/get.test.ts index 99aa7a1..0b44420 100644 --- a/test/get.test.ts +++ b/test/get.test.ts @@ -26,6 +26,8 @@ import * as oada from '../dist/index.js'; import { Nested, deleteLinkAxios, + putAxios, + getAxios, getTreeWithTestName, putResourceAxios, } from './utils'; @@ -136,6 +138,22 @@ for (const connection of ['ws', 'http']) { ); }); + test(`${connection}: Should error when 'X-OADA-Ensure-Link' is present`, async (t) => { + const putResp = await putAxios( + { somedata: 789 }, + `/bookmarks/${testName}/sometest4`, + { 'X-OADA-Ensure-Link': 'versioned' } + ); + t.is(putResp.status, 201); + t.assert(putResp.headers['content-location']); + t.assert(putResp.headers['x-oada-rev']); + + await t.throwsAsync(getAxios( + `/bookmarks/${testName}/sometest4`, + { 'X-OADA-Ensure-Link': 'versioned' } + )); + }); + test(`${connection}: Should allow you to get resources based on a tree`, async (t) => { // Prepare resources const basePath = `/bookmarks/${testName}`; diff --git a/test/put.test.ts b/test/put.test.ts index 9147a5f..729512c 100644 --- a/test/put.test.ts +++ b/test/put.test.ts @@ -28,6 +28,7 @@ import { deleteLinkAxios, getAxios, getTreeWithTestName, + putAxios, putResourceAxios, } from './utils.js'; import { OADAClient, connect } from '../dist/index.js'; @@ -112,6 +113,18 @@ for (const connection of ['ws', 'http']) { t.assert(response.headers['x-oada-rev']); }); + test(`${connection}: Shouldn't error when the 'X-OADA-Ensure-Link' header is a supported value`, async (t) => { + const { testName } = t.context as Context; + const response = await putAxios( + { _type: 'application/json' }, + `/bookmarks/${testName}/sometest`, + { 'X-OADA-Ensure-Link': 'versioned' } + ); + t.is(response.status, 201); + t.assert(response.headers['content-location']); + t.assert(response.headers['x-oada-rev']); + }); + // TODO: Check the rejection reason test.skip(`${connection}: Should error when _type cannot be derived from the above tested sources`, async (t) => { const { testName } = t.context as Context; @@ -151,6 +164,15 @@ for (const connection of ['ws', 'http']) { ); }); + test(`${connection}: Should error when 'X-OADA-Ensure-Link' contains an unsupported value`, async (t) => { + const { testName } = t.context as Context; + await t.throwsAsync(putAxios( + { somedata: 456 }, + `/bookmarks/${testName}/sometest4`, + { 'X-OADA-Ensure-Link': 'unsupportedValue' } + )); + }); + test(`${connection}: Should create the proper resource breaks on the server when a tree parameter is supplied to a deep endpoint`, async (t) => { const { testName, testTree } = t.context as Context; const putResp = await client.put({ diff --git a/test/utils.ts b/test/utils.ts index 334edf5..6f262cf 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -25,17 +25,25 @@ export type Nested = } | undefined; -export async function getAxios(path: string) { +export async function getAxios( + path: string, + headers?: Record, +) { return axios({ method: 'get', url: `${domain}${path}`, headers: { Authorization: `Bearer ${token}`, + ...headers, }, }); } -export async function putAxios(data: Record, path: string) { +export async function putAxios( + data: Record, + path: string, + headers?: Record, +) { return axios({ method: 'put', url: `${domain}${path}`, @@ -43,6 +51,7 @@ export async function putAxios(data: Record, path: string) { 'Authorization': `Bearer ${token}`, // eslint-disable-next-line sonarjs/no-duplicate-string 'Content-Type': 'application/json', + ...headers, }, data, }); From 45640fb46f4e403fd1d3d548b0d6f8b342f2d75b Mon Sep 17 00:00:00 2001 From: Nathan Givens Date: Fri, 8 Apr 2022 14:45:09 -0400 Subject: [PATCH 2/2] add error code and response --- test/get.test.ts | 10 ++++++++-- test/put.test.ts | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/test/get.test.ts b/test/get.test.ts index 0b44420..a64f128 100644 --- a/test/get.test.ts +++ b/test/get.test.ts @@ -148,10 +148,16 @@ for (const connection of ['ws', 'http']) { t.assert(putResp.headers['content-location']); t.assert(putResp.headers['x-oada-rev']); - await t.throwsAsync(getAxios( + await t.throwsAsync( + getAxios( `/bookmarks/${testName}/sometest4`, { 'X-OADA-Ensure-Link': 'versioned' } - )); + ), + { + code: '400', + message: 'X-OADA-Ensure-Link not allowed for this method', + } + ); }); test(`${connection}: Should allow you to get resources based on a tree`, async (t) => { diff --git a/test/put.test.ts b/test/put.test.ts index 729512c..042847c 100644 --- a/test/put.test.ts +++ b/test/put.test.ts @@ -166,11 +166,17 @@ for (const connection of ['ws', 'http']) { test(`${connection}: Should error when 'X-OADA-Ensure-Link' contains an unsupported value`, async (t) => { const { testName } = t.context as Context; - await t.throwsAsync(putAxios( + await t.throwsAsync( + putAxios( { somedata: 456 }, `/bookmarks/${testName}/sometest4`, { 'X-OADA-Ensure-Link': 'unsupportedValue' } - )); + ), + { + code: '400', + message: 'Unsupported value for X-OADA-Ensure-Link', + } + ); }); test(`${connection}: Should create the proper resource breaks on the server when a tree parameter is supplied to a deep endpoint`, async (t) => {