Skip to content

Commit

Permalink
0.2.0 add Header.expandFirstArgIfArray(), fix but in List
Browse files Browse the repository at this point in the history
  • Loading branch information
MorganConrad committed Nov 15, 2018
1 parent ea286dd commit a8d7192
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ Sets all headers in options.SAFE or options.safe, creating if needed.

#### add(data)
Adds data to the header value
- `List.add(...items)` accepts a single value or an array
- `List.add(...items)` for convenience, accepts an array, **or** one or more individual items
- _e.g._ `add('a','b')` is equivalent to `add(['a','b'])`
- `Policies.add(policyName, ...items)` requires a policy name first
- `...items` may be an array, **or** one or more individual items

#### applyTo(response)
Write the header to the response. You will seldom call this directly.
Expand All @@ -121,7 +123,7 @@ Set the header to a "safe" value, as provided in the options.

#### set(value)
Sets the value

- `List.set(...items)` like add(), this accepts an array, or one or more individual items


## Customization
Expand Down
14 changes: 9 additions & 5 deletions header.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Header {

switch (typeName.toUpperCase()) {
case "DATE": return { clazz: DateValue };
case "LIST,": return { clazz: List };
case "LIST,": return { clazz: List, delimiter: ', ' };
case "LIST;": return { clazz: List, delimiter: '; ' };
case "POLICIES": return { clazz: Policies };
case "VALUE": return { clazz: Value };
Expand Down Expand Up @@ -78,7 +78,7 @@ class Header {
if (nameInfo.clazz && dataInfo.clazz && (nameInfo.clazz !== dataInfo.clazz))
throw new Error(`Header ${fullName} expects type ${nameInfo.clazz.name} but data is type ${dataInfo.clazz.name}`);

options.delimiter = options.delimiter || nameInfo.delimiter;
options.delimiter = nameInfo.delimiter;

let Clazz = nameInfo.clazz || dataInfo.clazz || Value;

Expand Down Expand Up @@ -141,6 +141,7 @@ class Policies extends Header {
}

add(policyName, ...items) {
items = expandFirstArgIfArray(items);
let policyValues = this.data[policyName];
if (policyValues)
policyValues.push(...items);
Expand Down Expand Up @@ -194,12 +195,12 @@ class List extends Header {
}

add(...items) {
this.data.push(...items);
this.data.push(...expandFirstArgIfArray(items));
return this;
}

set(items = []) {
this.data = [...items];
set(...items) {
this.data = [...expandFirstArgIfArray(items)];
return this;
}

Expand All @@ -214,5 +215,8 @@ function forceArray(x) {
return x ? [x] : [];
}

function expandFirstArgIfArray(rest) {
return (rest.length === 1) && Array.isArray(rest[0]) ? rest[0] : rest;
}

module.exports = { Header };
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kepi",
"version": "0.1.0",
"version": "0.2.0",
"description": "lightweight HTTP headers",
"main": "kepi.js",
"scripts": {
Expand Down
15 changes: 9 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ test('List,', function(t) {

kepi.header("WWW-Authenticate").set().add('Basic').add('charset="UTF-8"');
kepi.header("Content-Encoding").set(['a', 'b']).clear();
kepi.header("Access-Control-Allow-Methods").set('GET','PUT');

kepi.applyTo(res);
t.equals(res.toString(), '{"WWW-Authenticate":"Basic, charset=\\"UTF-8\\""}');
t.equals(res.toString(), '{"WWW-Authenticate":"Basic, charset=\\"UTF-8\\"","Access-Control-Allow-Methods":"GET, PUT"}');
t.end();
});

Expand All @@ -72,9 +73,11 @@ test('List;', function(t) {

kepi.header("Content-Disposition").set().add('attachment', 'filename="cool.html"');
kepi.header("Content-Type", ['text/html', 'charset=utf-8']).clear();
kepi.header("Access-Control-Allow-Methods").add(['GET','PUT']); // was a bug in 0.1.0
kepi.header("Content-Type").set(['abc','def']);

kepi.applyTo(res);
t.equals(res.toString(), '{"Content-Disposition":"attachment; filename=\\"cool.html\\""}');
t.equals(res.toString(), '{"Content-Disposition":"attachment; filename=\\"cool.html\\"","Content-Type":"abc; def","Access-Control-Allow-Methods":"GET, PUT"}');
t.end();
});

Expand Down Expand Up @@ -102,7 +105,7 @@ test('somesafe', function(t) {
});


const BIG_STRING3 = '{"X-DNS-Prefetch-Control":"test-dnsPrefetchControl","Access-Control-Allow-Methods":"PUT, POST, DELETE","Feature-Policy":"foo foo1 foo2; bar bar1"}';
const BIG_STRING3 = '{"X-DNS-Prefetch-Control":"test-dnsPrefetchControl","Access-Control-Allow-Methods":"PUT, POST, DELETE","Feature-Policy":"foo foo1 foo2 foo3; bar bar1"}';


test('delete', function(t) {
Expand All @@ -114,7 +117,7 @@ test('delete', function(t) {
t.end();
})

const CLEAR_AND_ADD = '{"stringHeader":"addedstring","listHeader":"a,b,c","directiveHeader1":"foo1 newValue1,newValue2","directiveHeader2":"bar2 originalbarValue2; foo3 newfoovalue3"}';
const CLEAR_AND_ADD = '{"stringHeader":"addedstring","listHeader":"a, b, c","directiveHeader1":"foo1 newValue1 newValue2","directiveHeader2":"bar2 originalbarValue2; foo3 newfoovalue3"}';

test('clear & add', function(t) {
let kepi = Kepi({
Expand Down Expand Up @@ -145,8 +148,8 @@ test('headers', function(t) {
kepi.accessControl.allowMethods().add('PUT', 'POST');
kepi.accessControl.allowMethods().add('DELETE');
kepi.header('Feature-Policy').add('foo', "foo1");
kepi.header('Feature-Policy').add('foo', "foo2");
kepi.header('Feature-Policy').add('bar', "bar1");
kepi.header('Feature-Policy').add('foo', "foo2", "foo3");
kepi.header('Feature-Policy').add('bar', ["bar1"]);
kepi.header('Feature-Policy').add('removeLater', "shouldBeRemoved");
kepi.header('Feature-Policy').clear('removeLater');

Expand Down

0 comments on commit a8d7192

Please sign in to comment.