Skip to content

Commit

Permalink
feat: Create functions to initialise client (#19)
Browse files Browse the repository at this point in the history
Needed for #7
  • Loading branch information
gnarea committed Aug 27, 2020
1 parent 291bbed commit 52847ed
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 3 deletions.
59 changes: 59 additions & 0 deletions src/lib/PoWebClient.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { PoWebClient } from './PoWebClient';

describe('PoWebClient', () => {
describe('initLocal', () => {
test('Host name should be the localhost IP address', () => {
const client = PoWebClient.initLocal();

expect(client.hostName).toEqual('127.0.0.1');
});

test('TLS should not be used', () => {
const client = PoWebClient.initLocal();

expect(client.useTLS).toBeFalsy();
});

test('Port should default to 276', () => {
const client = PoWebClient.initLocal();

expect(client.port).toEqual(276);
});

test('Port should be overridable', () => {
const port = 13276;
const client = PoWebClient.initLocal(port);

expect(client.port).toEqual(port);
});
});

describe('initRemote', () => {
const hostName = 'gw.relaycorp.tech';

test('Specified host name should be honored', () => {
const client = PoWebClient.initRemote(hostName);

expect(client.hostName).toEqual(hostName);
});

test('TLS should be used', () => {
const client = PoWebClient.initRemote(hostName);

expect(client.useTLS).toBeTruthy();
});

test('Port should default to 443', () => {
const client = PoWebClient.initRemote(hostName);

expect(client.port).toEqual(443);
});

test('Port should be overridable', () => {
const port = 13276;
const client = PoWebClient.initRemote(hostName, port);

expect(client.port).toEqual(port);
});
});
});
37 changes: 37 additions & 0 deletions src/lib/PoWebClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* PoWeb client.
*/
export class PoWebClient {
/**
* Connect to a private gateway from a private endpoint.
*
* @param port The port for the PoWeb server
*
* TLS won't be used.
*/
public static initLocal(port: number = PoWebClient.DEFAULT_LOCAL_PORT): PoWebClient {
return new PoWebClient('127.0.0.1', port, false);
}

/**
* Connect to a public gateway from a private gateway via TLS.
*
* @param hostName The IP address or domain for the PoWeb server
* @param port The port for the PoWeb server
*/
public static initRemote(
hostName: string,
port: number = PoWebClient.DEFAULT_REMOVE_PORT,
): PoWebClient {
return new PoWebClient(hostName, port, true);
}

private static readonly DEFAULT_LOCAL_PORT = 276;
private static readonly DEFAULT_REMOVE_PORT = 443;

protected constructor(
public readonly hostName: string,
public readonly port: number,
public readonly useTLS: boolean,
) {}
}
3 changes: 0 additions & 3 deletions src/lib/thingy.spec.ts

This file was deleted.

0 comments on commit 52847ed

Please sign in to comment.