From 3d83083ee84e824bd1c7b8b626cfea9db7a2d91c Mon Sep 17 00:00:00 2001 From: vrde Date: Thu, 22 Jun 2017 17:19:31 +0200 Subject: [PATCH] Fix Content-Type issue --- README.md | 4 ++-- src/connection/index.js | 13 +++++++++++-- src/request.js | 1 + test/integration/test_integration.js | 5 +++++ 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index eff8f5f..f61eeb4 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ const tx = driver.Transaction.makeCreateTransaction( const txSigned = driver.Transaction.signTransaction(tx, alice.privateKey) // Send the transaction off to BigchainDB -let conn = new driver.Connection(API_PATH, { 'Content-Type': 'application/json' }) +let conn = new driver.Connection(API_PATH) conn.postTransaction(txSigned) .then(() => conn.getStatus(txSigned.id)) @@ -120,7 +120,7 @@ conn.postTransaction(txSigned) const txSigned = BigchainDB.Transaction.signTransaction(tx, alice.privateKey) // Send the transaction off to BigchainDB - let conn = new BigchainDB.Connection(API_PATH, { 'Content-Type': 'application/json' }) + let conn = new BigchainDB.Connection(API_PATH) conn.postTransaction(txSigned) .then(() => conn.getStatus(txSigned.id)) diff --git a/src/connection/index.js b/src/connection/index.js index aa7131e..7ec382c 100644 --- a/src/connection/index.js +++ b/src/connection/index.js @@ -1,10 +1,19 @@ import request from '../request' +const HEADER_BLACKLIST = ['content-type'] + + export default class Connection { - constructor(path, headers) { + constructor(path, headers = {}) { this.path = path - this.headers = headers + this.headers = Object.assign({}, headers) + + Object.keys(headers).forEach(header => { + if (HEADER_BLACKLIST.includes(header.toLowerCase())) { + throw new Error(`Header ${header} is reserved and cannot be set.`) + } + }) } getApiUrls(endpoint) { diff --git a/src/request.js b/src/request.js index 8d23176..fab3ac9 100644 --- a/src/request.js +++ b/src/request.js @@ -24,6 +24,7 @@ export default function request(url, config = {}, onlyJsonResponse = true) { 'Content-Type': 'application/json' }) } + if (!url) { return Promise.reject(new Error('Request was not given a url.')) } diff --git a/test/integration/test_integration.js b/test/integration/test_integration.js index b6808d5..b5e53fe 100644 --- a/test/integration/test_integration.js +++ b/test/integration/test_integration.js @@ -308,3 +308,8 @@ test('Search transaction containing an asset', t => { .then(({ id }) => conn.listTransactions(id)) .then(transactions => t.truthy(transactions.length === 1)) }) + + +test('Content-Type cannot be set', t => { + t.throws(() => new Connection(API_PATH, { 'Content-Type': 'application/json' }), Error) +})