Skip to content

Commit

Permalink
Merge pull request #64 from lifeomic/AIC-62
Browse files Browse the repository at this point in the history
fix: bug fix for passing in headers containing json
  • Loading branch information
david-wb authored Sep 29, 2023
2 parents 771ee00 + 95678a0 commit 4954ba1
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 19 deletions.
9 changes: 5 additions & 4 deletions src/plugins/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import type { Argv } from 'yargs';
import { AlphaCliConfig, AlphaCliArguments } from '../types';

export const parseLine = (headers: Record<string, string>, line: string) => {
const [untrimmedKey, untrimmedValue] = line.split(':');
if (untrimmedValue === undefined) {
const indexOfFirstColon = line.indexOf(':');

if (indexOfFirstColon < 0) {
return headers;
}

const key = untrimmedKey.trim();
const value = untrimmedValue.trim();
const key = line.slice(0, indexOfFirstColon).trim();
const value = line.slice(indexOfFirstColon + 1).trim();

if (value) {
// This object is simply used to serialize headers on the client
Expand Down
95 changes: 80 additions & 15 deletions test/request-headers.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import Koa from 'koa';

import { createTestServer, destroyTestServer, runCommand, TestContext } from './utils';
import {
createTestServer,
destroyTestServer,
runCommand,
TestContext,
} from './utils';

let context: TestContext;

Expand All @@ -27,50 +32,110 @@ const getHeaders = async (...args: string[]) => {

test('The -H flag can be used to specify a request header', async () => {
const headers = await getHeaders('-H', 'Test-Header: header value');
expect(Object.keys(headers).sort()).toEqual(['accept', 'connection', 'host', 'test-header', 'user-agent']);
expect(Object.keys(headers).sort()).toEqual([
'accept',
'connection',
'host',
'test-header',
'user-agent',
]);
expect(headers['test-header']).toBe('header value');
});

test('The --header flag can be used to specify a request header', async () => {
const headers = await getHeaders('--header', 'Test-Header: header value');

expect(Object.keys(headers).sort()).toEqual(['accept', 'connection', 'host', 'test-header', 'user-agent']);
expect(Object.keys(headers).sort()).toEqual([
'accept',
'connection',
'host',
'test-header',
'user-agent',
]);
expect(headers['test-header']).toBe('header value');
});

test('Specifying multiple request headers adds all the headers to the request', async () => {
const headers = await getHeaders(
'-H', 'First-Header: one',
'-H', 'Second-Header: two',
'-H',
'First-Header: one',
'-H',
'Second-Header: two',
);

expect(Object.keys(headers).sort()).toEqual(
['accept', 'connection', 'first-header', 'host', 'second-header', 'user-agent'],
);
expect(Object.keys(headers).sort()).toEqual([
'accept',
'connection',
'first-header',
'host',
'second-header',
'user-agent',
]);
expect(headers['first-header']).toBe('one');
expect(headers['second-header']).toBe('two');
});

test('Specifying the same header multiple times uses the last instance', async () => {
const headers = await getHeaders(
'-H', 'Test-Header: one',
'-H', 'Test-Header: two',
'-H',
'Test-Header: one',
'-H',
'Test-Header: two',
);

expect(Object.keys(headers).sort()).toEqual(['accept', 'connection', 'host', 'test-header', 'user-agent']);
expect(Object.keys(headers).sort()).toEqual([
'accept',
'connection',
'host',
'test-header',
'user-agent',
]);
expect(headers['test-header']).toBe('two');
});

test('Specifying an empty header deletes the header from the request', async () => {
const headers = await getHeaders(
'-H', 'Test-Header: foo',
'-H', 'Test-Header:',
'-H',
'Test-Header: foo',
'-H',
'Test-Header:',
);
expect(Object.keys(headers).sort()).toEqual(['accept', 'connection', 'host', 'user-agent']);
expect(Object.keys(headers).sort()).toEqual([
'accept',
'connection',
'host',
'user-agent',
]);
});

test('Malformed request headers are ignored', async () => {
const headers = await getHeaders('-H', 'foo');

expect(Object.keys(headers).sort()).toEqual(['accept', 'connection', 'host', 'user-agent']);
expect(Object.keys(headers).sort()).toEqual([
'accept',
'connection',
'host',
'user-agent',
]);
});

test('Can specify a header containing a JSON string', async () => {
const headers = await getHeaders(
'--header',
'LifeOmic-Policy: {"rules": {"readData": true}}',
);

expect(Object.keys(headers).sort()).toEqual([
'accept',
'connection',
'host',
'lifeomic-policy',
'user-agent',
]);
console.log(headers['lifeomic-policy']);
expect(JSON.parse(headers['lifeomic-policy'])).toEqual({
rules: {
readData: true,
},
});
});

0 comments on commit 4954ba1

Please sign in to comment.