Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(request-node): Add Request Node version and Request Client version to requests header #192

Merged
merged 9 commits into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/request-node/src/requestNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ import ipfsAdd from './request/ipfsAdd';
import PersistTransaction from './request/persistTransaction';
import { getEthereumStorage } from './storageUtils';

const packageJson = require('../package.json');

const NOT_FOUND_MESSAGE =
'Not found\nAvailable endpoints:\n/POST persistTransaction\n/GET getTransactionsByChannelId\n/GET getChannelsByTopic\n/POST /ipfsAdd\nGET getConfirmedTransaction';

const NOT_INITIALIZED_MESSAGE = 'The node is not initialized';

const REQUEST_NODE_VERSION_HEADER = 'Request-Node-Version';
lumtis marked this conversation as resolved.
Show resolved Hide resolved

/**
* Main class for request node express server
* This class defines routes to handle requests from client
Expand All @@ -37,6 +41,7 @@ class RequestNode {
private logger: LogTypes.ILogger;
private persistTransaction: PersistTransaction;
private confirmedTransactionStore: ConfirmedTransactionStore;
private requestNodeVersion: string;
/**
* Request Node constructor
*
Expand Down Expand Up @@ -73,6 +78,9 @@ class RequestNode {

this.express = express();
this.mountRoutes();

// Get the version of the Request Node for the request's response header
this.requestNodeVersion = packageJson.version;
}

/**
Expand Down Expand Up @@ -141,6 +149,12 @@ class RequestNode {
});
}

// Set the Request Node version to the header
this.express.use((_: any, res: any, next: any) => {
res.header(REQUEST_NODE_VERSION_HEADER, this.requestNodeVersion),
lumtis marked this conversation as resolved.
Show resolved Hide resolved
next();
});

// Supported encodings
this.express.use(express.json());
this.express.use(express.urlencoded({ extended: true }));
Expand Down
13 changes: 13 additions & 0 deletions packages/request-node/test/requestNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import requestNode from '../src/requestNode';
chai.use(chaiAsPromised);
const expect = chai.expect;

const packageJson = require('../package.json');
const requestNodeVersion = packageJson.version;

const dataAccessInitializeFailureMock = async (): Promise<never> => {
throw Error('This mock function always fails');
};
Expand Down Expand Up @@ -99,6 +102,16 @@ describe('requestNode server', () => {
.expect('x-custom-test-header', 'test-passed');
});

it('the response header contains the Request Node version', async () => {
// Import directly requestNode to create a server
requestNodeInstance = new requestNode();
server = requestNodeInstance.listen(3002, () => 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't have to use listen in unit tests, aren't we using supertest?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping :)


await request(server)
.post('/')
.expect('Request-Node-Version', requestNodeVersion);
});

it('must throw if no mnemonic given with rinkeby', async () => {
process.env.ETHEREUM_NETWORK_ID = '4';

Expand Down
Loading