Skip to content

Commit

Permalink
chore: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
david-wb committed Sep 29, 2023
1 parent 6f7c605 commit 95678a0
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 22 deletions.
14 changes: 7 additions & 7 deletions src/plugins/headers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Argv } from "yargs";
import { AlphaCliConfig, AlphaCliArguments } from "../types";
import type { Argv } from 'yargs';
import { AlphaCliConfig, AlphaCliArguments } from '../types';

export const parseLine = (headers: Record<string, string>, line: string) => {
const indexOfFirstColon = line.indexOf(":");
const indexOfFirstColon = line.indexOf(':');

if (indexOfFirstColon < 0) {
return headers;
Expand All @@ -25,10 +25,10 @@ export const parseLine = (headers: Record<string, string>, line: string) => {
};

export default (yargs: Argv) => {
yargs.option("H", {
alias: "header",
type: "string",
describe: "Pass custom header line to server",
yargs.option('H', {
alias: 'header',
type: 'string',
describe: 'Pass custom header line to server',
});

return (config: AlphaCliConfig, { header }: AlphaCliArguments) => {
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 95678a0

Please sign in to comment.