From 95678a0bf9c22e7d65dc2de4615be6b4826bd5e8 Mon Sep 17 00:00:00 2001 From: David Brown Date: Fri, 29 Sep 2023 14:07:06 +0200 Subject: [PATCH] chore: add test --- src/plugins/headers.ts | 14 +++--- test/request-headers.test.ts | 95 ++++++++++++++++++++++++++++++------ 2 files changed, 87 insertions(+), 22 deletions(-) diff --git a/src/plugins/headers.ts b/src/plugins/headers.ts index bb1565e..8394820 100644 --- a/src/plugins/headers.ts +++ b/src/plugins/headers.ts @@ -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, line: string) => { - const indexOfFirstColon = line.indexOf(":"); + const indexOfFirstColon = line.indexOf(':'); if (indexOfFirstColon < 0) { return headers; @@ -25,10 +25,10 @@ export const parseLine = (headers: Record, 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) => { diff --git a/test/request-headers.test.ts b/test/request-headers.test.ts index 0ae78d2..17003c6 100644 --- a/test/request-headers.test.ts +++ b/test/request-headers.test.ts @@ -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; @@ -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, + }, + }); });