Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: export headers to reduce duplication #33

Merged
merged 3 commits into from
Feb 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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