Skip to content

Commit

Permalink
feat: export headers to reduce duplication (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Feb 16, 2018
1 parent 5105200 commit 0b6eb38
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export const HOST_ADDRESS = 'http://metadata.google.internal';
export const BASE_PATH = '/computeMetadata/v1';
export const BASE_URL = HOST_ADDRESS + BASE_PATH;
export const HEADER_NAME = 'Metadata-Flavor';
export const HEADER_VALUE = 'Google';
export const HEADERS = Object.freeze({[HEADER_NAME]: HEADER_VALUE});

export type Options = AxiosRequestConfig&
{[index: string]: {} | string | undefined, property?: string, uri?: string};
Expand Down Expand Up @@ -43,15 +45,15 @@ async function metadataAccessor(type: string, options?: string|Options) {
rax.attach(ax);
const baseOpts = {
url: `${BASE_URL}/${type}${property}`,
headers: {'Metadata-Flavor': 'Google'},
headers: Object.assign({}, HEADERS),
raxConfig: {noResponseRetries: 0}
};
const reqOpts = extend(true, baseOpts, options);
delete (reqOpts as {property: string}).property;
return ax.request(reqOpts)
.then(res => {
// NOTE: node.js converts all incoming headers to lower case.
if (res.headers[HEADER_NAME.toLowerCase()] !== 'Google') {
if (res.headers[HEADER_NAME.toLowerCase()] !== HEADER_VALUE) {
throw new Error(`Invalid response from metadata service: incorrect ${
HEADER_NAME} header.`);
} else if (!res.data) {
Expand Down
13 changes: 7 additions & 6 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const PROPERTY = 'property';

// NOTE: nodejs switches all incoming header names to lower case.
const HEADERS = {
'metadata-flavor': 'Google'
[gcp.HEADER_NAME.toLowerCase()]: gcp.HEADER_VALUE
};

nock.disableNetConnect();
Expand All @@ -31,8 +31,8 @@ test.serial('should access all the metadata properly', async t => {
const res = await gcp.instance();
scope.done();
t.is(res.config.url, `${BASE_URL}/${TYPE}`);
t.is(res.config.headers[HEADER_NAME], 'Google');
t.is(res.headers[HEADER_NAME.toLowerCase()], 'Google');
t.is(res.config.headers[HEADER_NAME], gcp.HEADER_VALUE);
t.is(res.headers[HEADER_NAME.toLowerCase()], gcp.HEADER_VALUE);
});

test.serial('should access a specific metadata property', async t => {
Expand Down Expand Up @@ -82,9 +82,10 @@ test.serial('should return error when res is empty', async t => {
});

test.serial('should return error when flavor header is incorrect', async t => {
const scope = nock(HOST).get(`${PATH}/${TYPE}`).reply(200, {}, {
'metadata-flavor': 'Hazelnut'
});
const scope =
nock(HOST)
.get(`${PATH}/${TYPE}`)
.reply(200, {}, {[gcp.HEADER_NAME.toLowerCase()]: 'Hazelnut'});
await t.throws(
gcp.instance(),
`Invalid response from metadata service: incorrect Metadata-Flavor header.`);
Expand Down

0 comments on commit 0b6eb38

Please sign in to comment.