-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* headers and published versions * headers only in touch and import for now * add tests
- Loading branch information
Morgan Croney
committed
Sep 13, 2017
1 parent
1e60bbe
commit 53bcc84
Showing
10 changed files
with
187 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,3 +35,6 @@ jspm_packages | |
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# ide | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* | ||
* @param {object} argv | ||
* @param {string|Array} [argv.headers] e.g. `key:val` | ||
* @returns {object} headers object for request | ||
* @throws on unknown header format | ||
*/ | ||
function getYargHeaders(argv) { | ||
var headers = argv.headers || []; | ||
|
||
if (typeof headers === 'string') { | ||
headers = [headers]; | ||
} | ||
return headers.reduce((obj, header) => { | ||
const split = header.split(':'); | ||
|
||
if (split.length !== 2) { | ||
throw new Error(`Unknown header format: ${header}`); | ||
} | ||
obj[split[0].trim()] = split[1].trim(); | ||
return obj; | ||
}, {}); | ||
} | ||
|
||
module.exports.getYargHeaders = getYargHeaders; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const lib = require('./headers'), | ||
singleHeaderMock = { | ||
argv: { | ||
headers: 'X-Forwarded-Host: some-site.com' | ||
}, | ||
headersObj: { | ||
'X-Forwarded-Host': 'some-site.com' | ||
} | ||
}, | ||
multipleHeadersMock = { | ||
argv: { | ||
headers: [singleHeaderMock.argv.headers].concat(['Custom-Header: some-value']) | ||
}, | ||
headersObj: Object.assign({'Custom-Header': 'some-value'}, singleHeaderMock.headersObj) | ||
}; | ||
|
||
describe('headers', function () { | ||
|
||
describe('getYargHeaders', function () { | ||
const fn = lib[this.title]; | ||
|
||
it('returns empty object if no headers yarg', () => | ||
expect(fn({})).to.deep.equal({}) | ||
); | ||
|
||
it('returns headers object for single headers yarg', () => | ||
expect(fn(singleHeaderMock.argv)).to.deep.equal(singleHeaderMock.headersObj) | ||
); | ||
|
||
it('returns headers object for multiple headers yargs', () => | ||
expect(fn(multipleHeadersMock.argv)).to.deep.equal(multipleHeadersMock.headersObj) | ||
); | ||
|
||
it('throws on unknown header format', () => | ||
expect(fn.bind(lib, {headers: 'weird-header=unknown-format'})).to.throw(Error) | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters