Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
mcanever committed Jan 27, 2019
2 parents cd2b9aa + 2225c05 commit 9c48391
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
29 changes: 29 additions & 0 deletions src/apis/requests/BaseRequest.ts
Expand Up @@ -109,8 +109,19 @@ export abstract class BaseRequest<Out, In> implements IAPIRequest<Out, In> {

protected decodeProtoBufResponse(res: {body: Buffer, peer: IPeerLogic}, pbNamespace: string, pbMessageType?: string): Out {
let error: { success: false, error: string };
if (!Buffer.isBuffer(res.body)) {
throw new Error('res.body must be a Buffer');
}
if (res.body.length === 0) {
return this.getEmptyResponse(pbNamespace, pbMessageType, res.peer);
}

try {
error = this.protoBufHelper.decode(res.body, 'APIError');
const errChunk = error.error.substr(0, 5);
if (errChunk.length > 0 && !errChunk.match(/^[ -~]+$/)) {
error = undefined;
}
} catch (e) {
// NOOP;
}
Expand All @@ -121,4 +132,22 @@ export abstract class BaseRequest<Out, In> implements IAPIRequest<Out, In> {
.decodeToObj(res.body, pbNamespace, pbMessageType, this.getConversionOptions());
}
}

private getEmptyResponse(pbNamespace: string, pbMessageType: string, pl: IPeerLogic): any {
const key = pbNamespace + (pbMessageType || '');
const emptyResponses = {
transportBlocks: {blocks: []},
transportBlockscommonBlock: {common: null},
transportBlockstransportBlockResponse: {success: false, blockId: null},
transportPeers: {peers: []},
transportSignaturesgetSignaturesResponse: {signatures: []},
transportTransactions: {transactions: []},
};
if (typeof emptyResponses[key] !== 'undefined') {
return emptyResponses[key];
} else {
const peer = JSON.stringify(pl.object());
throw new Error(`[${pbNamespace} ${pbMessageType ? pbMessageType : ''}] Unexpected empty res from peer ${peer}`);
}
}
}
2 changes: 1 addition & 1 deletion tests/integration/api/blocks.spec.ts
Expand Up @@ -91,7 +91,7 @@ describe('api/blocks', () => {
const { body } = await supertest(initializer.appManager.expressApp)
.get('/api/blocks/?limit=1&orderBy=height:desc')
.expect(200);
expect(body.blocks[0].transactions[0].asset).deep.eq(txs[0].asset);
expect(body.blocks[0].transactions[0].asset).to.be.undefined;
expect(body.blocks[0].transactions[1].asset).deep.eq(txs[1].asset);
expect(body.blocks[0].transactions[2].asset).deep.eq(txs[2].asset);
expect(body.blocks[0].transactions[3].asset).deep.eq(txs[3].asset);
Expand Down
1 change: 1 addition & 0 deletions tests/unit/apis/blocksAPI.spec.ts
Expand Up @@ -95,6 +95,7 @@ describe('apis/blocksAPI', () => {
limit : 100,
offset : 0,
order : [['height', 'desc']],
raw : true,
};
findAllStub = sandbox.stub(blocksModel, 'findAndCountAll').resolves({ rows: [], count: 0 });
txfindAllStub = sandbox.stub(TxModel, 'findAll').resolves([{a: 'a'},{b: 'b'}]);
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/apis/requests/BaseRequest.spec.ts
Expand Up @@ -176,7 +176,7 @@ describe('apis/requests/BaseRequest', () => {
describe('decodeProtoBufResponse', () => {

describe('when response status is 200', () => {
const res = {status: 200, body: Buffer.from('', 'hex')};
const res = {status: 200, body: Buffer.from('aa', 'hex')};
it('should call protoBufHelper.decodeToObj and return if it message is validated', () => {
protoBufStub.enqueueResponse('decodeToObj', 'decodedResult');
const resp = (instance as any).decodeProtoBufResponse(res, 'namespace', 'messageType');
Expand All @@ -189,7 +189,7 @@ describe('apis/requests/BaseRequest', () => {
});

describe('when response is an error', () => {
const res = {status: 200, body: Buffer.from('', 'hex')};
const res = {status: 200, body: Buffer.from('aa', 'hex')};
it('should try first to parse the request as an API error', () => {
const err = {success: false, error: 'thisIsAnErr'};
protoBufStub.stubs.decode.returns(err);
Expand Down

0 comments on commit 9c48391

Please sign in to comment.