Skip to content

Commit

Permalink
Rename "client registration" to "private node registration"
Browse files Browse the repository at this point in the history
  • Loading branch information
gnarea committed Aug 28, 2020
1 parent aa1adae commit afe1928
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 34 deletions.
47 changes: 16 additions & 31 deletions src/lib/PoWebClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import MockAdapter from 'axios-mock-adapter';
import bufferToArray from 'buffer-to-arraybuffer';

import { ServerError } from './errors';
import { CRA_CONTENT_TYPE, PoWebClient } from './PoWebClient';
import { PNRA_CONTENT_TYPE, PoWebClient } from './PoWebClient';

describe('PoWebClient', () => {
describe('Common Axios instance attributes', () => {
describe('Common Axios instance defaults', () => {
test('responseType should be ArrayBuffer', () => {
const client = PoWebClient.initLocal();

Expand Down Expand Up @@ -127,33 +127,15 @@ describe('PoWebClient', () => {
mockAxios = new MockAdapter(client.internalAxios);
});

const defaultReply: readonly [number, any, {}] = [
200,
null,
{ 'content-type': CRA_CONTENT_TYPE },
];

test('Request method should be POST', async () => {
mockAxios.onPost('/pre-registrations').reply(...defaultReply);
test('Empty request should be POSTed to /v1/pre-registrations', async () => {
mockAxios
.onPost('/pre-registrations')
.reply(200, null, { 'content-type': PNRA_CONTENT_TYPE });

await client.preRegister();

expect(mockAxios.history.post).toHaveLength(1);
});

test('Endpoint should be /v1/pre-registrations', async () => {
mockAxios.onPost('/pre-registrations').reply(...defaultReply);

await client.preRegister();

expect(mockAxios.history.post[0].url).toEqual('/pre-registrations');
});

test('Request body should be empty', async () => {
mockAxios.onPost('/pre-registrations').reply(...defaultReply);

await client.preRegister();

expect(mockAxios.history.post[0].data).toBeUndefined();
});

Expand All @@ -172,23 +154,26 @@ describe('PoWebClient', () => {
const statusCode = 201;
mockAxios
.onPost('/pre-registrations')
.reply(statusCode, null, { 'content-type': CRA_CONTENT_TYPE });
.reply(statusCode, null, { 'content-type': PNRA_CONTENT_TYPE });

await expect(client.preRegister()).rejects.toEqual(
new ServerError(`Unexpected response status (${statusCode})`),
);
});

test('CRA should be output serialized if status is 200', async () => {
const dummyCRABuffer = Buffer.from('the CRA');
const dummyCRAArrayBuffer = bufferToArray(dummyCRABuffer);
test('Authorization should be output serialized if status is 200', async () => {
const expectedAuthorizationSerialized = Buffer.from('the PNRA');
mockAxios
.onPost('/pre-registrations')
.reply(200, dummyCRAArrayBuffer, { 'content-type': CRA_CONTENT_TYPE });
.reply(200, bufferToArray(expectedAuthorizationSerialized), {
'content-type': PNRA_CONTENT_TYPE,
});

const cra = await client.preRegister();
const authorizationSerialized = await client.preRegister();

expect(dummyCRABuffer.equals(Buffer.from(cra))).toBeTruthy();
expect(
expectedAuthorizationSerialized.equals(Buffer.from(authorizationSerialized)),
).toBeTruthy();
});
});
});
6 changes: 3 additions & 3 deletions src/lib/PoWebClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const DEFAULT_REMOTE_TIMEOUT_MS = 5_000;

const OCTETS_IN_ONE_MIB = 2 ** 20;

export const CRA_CONTENT_TYPE = 'application/vnd.relaynet.cra';
export const PNRA_CONTENT_TYPE = 'application/vnd.relaynet.pnra';

/**
* PoWeb client.
Expand Down Expand Up @@ -63,7 +63,7 @@ export class PoWebClient {
}

/**
* Request a Client Registration Authorization (CRA)
* Request a Private Node Registration Authorization (PNRA).
*
* @throws [ServerError] If the server doesn't adhere to the protocol
*/
Expand All @@ -75,7 +75,7 @@ export class PoWebClient {
}

const contentType = response.headers['content-type'];
if (contentType !== CRA_CONTENT_TYPE) {
if (contentType !== PNRA_CONTENT_TYPE) {
throw new ServerError(`Server responded with invalid content type (${contentType})`);
}

Expand Down

0 comments on commit afe1928

Please sign in to comment.