-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: don't send header values that are 'undefined' (#360)
- Loading branch information
1 parent
a5e601c
commit ae03c1e
Showing
10 changed files
with
82 additions
and
53 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const { cleanHeaders } = require('../../src/lib/cleanHeaders'); | ||
|
||
describe('cleanHeaders', () => { | ||
it('should b64-encode key in ReadMe-friendly format', () => { | ||
expect(cleanHeaders('test')).toStrictEqual({ Authorization: 'Basic dGVzdDo=' }); | ||
}); | ||
|
||
it('should filter out undefined headers', () => { | ||
expect(cleanHeaders('test', { 'x-readme-version': undefined })).toStrictEqual({ Authorization: 'Basic dGVzdDo=' }); | ||
}); | ||
|
||
it('should filter out null headers', () => { | ||
expect(cleanHeaders('test', { 'x-readme-version': undefined, Accept: null })).toStrictEqual({ | ||
Authorization: 'Basic dGVzdDo=', | ||
}); | ||
}); | ||
|
||
it('should pass in properly defined headers', () => { | ||
expect( | ||
cleanHeaders('test', { 'x-readme-version': undefined, Accept: null, 'Content-Type': 'application/json' }) | ||
).toStrictEqual({ | ||
Authorization: 'Basic dGVzdDo=', | ||
'Content-Type': 'application/json', | ||
}); | ||
}); | ||
}); |
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
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,23 @@ | ||
/** | ||
* Returns the basic auth header and any other defined headers for use in node-fetch API calls. | ||
* @param {string} key The ReadMe project API key | ||
* @param {Object} inputHeaders Any additional headers to be cleaned | ||
* @returns An object with cleaned request headers for usage in the node-fetch requests to the ReadMe API. | ||
*/ | ||
function cleanHeaders(key, inputHeaders = {}) { | ||
const encodedKey = Buffer.from(`${key}:`).toString('base64'); | ||
const headers = { | ||
Authorization: `Basic ${encodedKey}`, | ||
}; | ||
|
||
Object.keys(inputHeaders).forEach(header => { | ||
// For some reason, node-fetch will send in the string 'undefined' | ||
// if you pass in an undefined value for a header, | ||
// so that's why headers are added incrementally. | ||
if (typeof inputHeaders[header] === 'string') headers[header] = inputHeaders[header]; | ||
}); | ||
|
||
return headers; | ||
} | ||
|
||
module.exports = { cleanHeaders }; |
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