Skip to content

Commit

Permalink
add node tests
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Weirich <matthias.weirich@selectcode.de>
  • Loading branch information
vavido committed Dec 17, 2020
1 parent 2d9666b commit 8d3b9ce
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
26 changes: 25 additions & 1 deletion javascript/lib/node/src/node-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
* SPDX-License-Identifier: EPL-2.0
*/

import { Base64Encoder, BasicAuth, HttpBasicAuth } from '@eclipse-ditto/ditto-javascript-client-api_1.0';
import {
Base64Encoder,
BasicAuth,
HttpBasicAuth,
HttpBearerAuth
} from '@eclipse-ditto/ditto-javascript-client-api_1.0';

/**
* Node implementation of base64 encoding.
Expand Down Expand Up @@ -57,3 +62,22 @@ export class NodeWebSocketBasicAuth extends HttpBasicAuth {
return new NodeWebSocketBasicAuth(username, password, new NodeBase64Encoder());
}
}

/**
* Node implementation of basic auth for HTTP connections
*/
export class NodeHttpBearerAuth extends HttpBearerAuth {

private constructor(token: string) {
super(token);
}

/**
* Create berer token AuthProvider for HTTP connections
* @param token The bearer token
*/
static newInstance(token: string) {
return new NodeHttpBearerAuth(token);
}

}
25 changes: 24 additions & 1 deletion javascript/lib/node/tests/node-auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


import { BasicAuth, DittoURL, ImmutableURL } from '@eclipse-ditto/ditto-javascript-client-api_1.0';
import { NodeBase64Encoder, NodeHttpBasicAuth, NodeWebSocketBasicAuth } from '../src/node-auth';
import { NodeBase64Encoder, NodeHttpBasicAuth, NodeHttpBearerAuth, NodeWebSocketBasicAuth } from '../src/node-auth';

const USERNAME = 'ditto';
const PASSWORD = 'foo$bar';
Expand Down Expand Up @@ -57,6 +57,29 @@ describe('NodeWebSocketBasicAuth', () => {

});

describe('NodeHttpBearerAuth', () => {

const exampleToken = 'AYjcyMzY3ZDhiNmJkNTY';

it('should leave urls as they are', () => {
const bearerAuth = NodeHttpBearerAuth.newInstance(exampleToken);

const expected = defaultUrl();
const actual = bearerAuth.authenticateWithUrl(defaultUrl());

expectEquals(actual, expected);
});

it('should add a Authorization header', () => {
const bearerAuth = NodeHttpBearerAuth.newInstance(exampleToken);

const dittoHeaders = bearerAuth.authenticateWithHeaders(defaultHeaders());
expect(dittoHeaders.size).toEqual(2);
expect(dittoHeaders.get(DEFAULT_KEY)).toEqual(DEFAULT_VAL);
expect(dittoHeaders.get('Authorization')).toEqual(`Bearer ${exampleToken}`);
});
});

const expectAddsBasicAuthHeader = (implementation: BasicAuth) => {
const dittoHeaders = implementation.authenticateWithHeaders(defaultHeaders());
expect(dittoHeaders.size).toEqual(2);
Expand Down

0 comments on commit 8d3b9ce

Please sign in to comment.