Skip to content
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
3 changes: 3 additions & 0 deletions src/exec_auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ export class ExecAuth implements Authenticator {
}
const token = this.getToken(credential);
if (token) {
if (!opts.headers) {
opts.headers = [];
}
opts.headers!.Authorization = `Bearer ${token}`;
}
}
Expand Down
35 changes: 35 additions & 0 deletions src/exec_auth_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import * as shell from 'shelljs';

import execa = require('execa');
import request = require('request');
import https = require('https');

import { ExecAuth } from './exec_auth';
import { User } from './config_types';
import { fail } from 'assert';

describe('ExecAuth', () => {
it('should claim correctly', () => {
Expand Down Expand Up @@ -246,4 +248,37 @@ describe('ExecAuth', () => {
expect(optsOut.env.PATH).to.equal(process.env.PATH);
expect(optsOut.env.BLABBLE).to.equal(process.env.BLABBLE);
});

it('should handle empty headers array correctly', async () => {
const auth = new ExecAuth();
(auth as any).execFn = (
command: string,
args: string[],
opts: execa.SyncOptions,
): execa.ExecaSyncReturnValue => {
return {
code: 0,
stdout: JSON.stringify({ status: { token: 'foo' } }),
} as execa.ExecaSyncReturnValue;
};
const opts = {} as https.RequestOptions;
auth.applyAuthentication(
{
name: 'user',
authProvider: {
config: {
exec: {
command: 'echo',
},
},
},
},
opts,
);
if (!opts.headers) {
fail('unexpected null headers!');
} else {
expect(opts.headers.Authorization).to.equal('Bearer foo');
}
});
});