Skip to content

Commit

Permalink
Merge branch 'ensure-link-get' of https://github.com/N-give/client in…
Browse files Browse the repository at this point in the history
…to N-give-ensure-link-get
  • Loading branch information
awlayton committed Apr 20, 2022
2 parents 44c13d8 + 45640fb commit 98344d4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
24 changes: 24 additions & 0 deletions test/get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import * as oada from '../dist/index.js';
import {
Nested,
deleteLinkAxios,
putAxios,
getAxios,
getTreeWithTestName,
putResourceAxios,
} from './utils.js';
Expand Down Expand Up @@ -136,6 +138,28 @@ for (const connection of ['ws', 'http'] as const) {
);
});

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' }
),
{
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) => {
// Prepare resources
const basePath = `/bookmarks/${testName}`;
Expand Down
28 changes: 28 additions & 0 deletions test/put.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
deleteLinkAxios,
getAxios,
getTreeWithTestName,
putAxios,
putResourceAxios,
} from './utils.js';
import { OADAClient, connect } from '../dist/index.js';
Expand Down Expand Up @@ -112,6 +113,18 @@ for (const connection of ['ws', 'http'] as const) {
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;
Expand Down Expand Up @@ -151,6 +164,21 @@ for (const connection of ['ws', 'http'] as const) {
);
});

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' }
),
{
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) => {
const { testName, testTree } = t.context as Context;
const putResp = await client.put({
Expand Down
10 changes: 8 additions & 2 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,30 @@ export type Nested =
}
| undefined;

export async function getAxios(uri: string) {
export async function getAxios(uri: string, headers?: Record<string, unknown>) {
return axios({
method: 'get',
url: new URL(uri, domain).toString(),
headers: {
Authorization: `Bearer ${token}`,
...headers,
},
});
}

export async function putAxios(data: Record<string, unknown>, uri: string) {
export async function putAxios(
data: Record<string, unknown>,
uri: string,
headers?: Record<string, unknown>
) {
return axios({
method: 'put',
url: new URL(uri, domain).toString(),
headers: {
'Authorization': `Bearer ${token}`,
// eslint-disable-next-line sonarjs/no-duplicate-string
'Content-Type': 'application/json',
...headers,
},
data,
});
Expand Down

0 comments on commit 98344d4

Please sign in to comment.