Added basic cookies support.

You can now let Beau know you expect cookies to be passed around by
setting COOKIEJAR to true. I'd eventually like to add support for file
storage or something similar.
This commit is contained in:
David Diaz 2018-04-26 17:37:36 -06:00
parent 9d10db3243
commit d3b78bc27d
5 changed files with 38 additions and 3 deletions

View File

@ -4,6 +4,7 @@ exports[`Beau's config Loader. should create a request list 1`] = `
Beau {
"config": Config {
"CACHE": false,
"COOKIEJAR": false,
"DEFAULTS": Object {
"headers": Object {
"authentication": "hello",
@ -23,6 +24,7 @@ Beau {
"REQUESTS": Array [
Object {
"ALIAS": "get-post",
"COOKIEJAR": false,
"ENDPOINT": "http://jsonplaceholder.typicode.com",
"HEADERS": Object {
"authentication": "hello",
@ -31,6 +33,7 @@ Beau {
},
Object {
"ALIAS": "user",
"COOKIEJAR": false,
"ENDPOINT": "http://jsonplaceholder.typicode.com",
"HEADERS": Object {
"authentication": "hello",
@ -48,9 +51,11 @@ Beau {
"DEFAULTS",
"ENVIRONMENT",
"HOSTS",
"COOKIEJAR",
],
"defaultConfigValues": Object {
"CACHE": false,
"COOKIEJAR": false,
"DEFAULTS": Object {},
"ENDPOINT": "",
"ENVIRONMENT": Object {},
@ -79,6 +84,7 @@ Beau {
"REQUESTS": Array [
Object {
"ALIAS": "get-post",
"COOKIEJAR": false,
"ENDPOINT": "http://jsonplaceholder.typicode.com",
"HEADERS": Object {
"authentication": "hello",
@ -87,6 +93,7 @@ Beau {
},
Object {
"ALIAS": "user",
"COOKIEJAR": false,
"ENDPOINT": "http://jsonplaceholder.typicode.com",
"HEADERS": Object {
"authentication": "hello",
@ -102,6 +109,7 @@ Beau {
},
"config": Config {
"CACHE": false,
"COOKIEJAR": false,
"DEFAULTS": Object {
"headers": Object {
"authentication": "hello",
@ -121,6 +129,7 @@ Beau {
"REQUESTS": Array [
Object {
"ALIAS": "get-post",
"COOKIEJAR": false,
"ENDPOINT": "http://jsonplaceholder.typicode.com",
"HEADERS": Object {
"authentication": "hello",
@ -129,6 +138,7 @@ Beau {
},
Object {
"ALIAS": "user",
"COOKIEJAR": false,
"ENDPOINT": "http://jsonplaceholder.typicode.com",
"HEADERS": Object {
"authentication": "hello",
@ -146,9 +156,11 @@ Beau {
"DEFAULTS",
"ENVIRONMENT",
"HOSTS",
"COOKIEJAR",
],
"defaultConfigValues": Object {
"CACHE": false,
"COOKIEJAR": false,
"DEFAULTS": Object {},
"ENDPOINT": "",
"ENVIRONMENT": Object {},
@ -176,6 +188,7 @@ Beau {
"list": Array [
Request {
"ALIAS": "get-post",
"COOKIEJAR": false,
"DEPENDENCIES": Set {},
"ENDPOINT": "http://jsonplaceholder.typicode.com",
"FORM": undefined,
@ -188,6 +201,7 @@ Beau {
"VERB": "GET",
"originalRequest": Object {
"ALIAS": "get-post",
"COOKIEJAR": false,
"ENDPOINT": "http://jsonplaceholder.typicode.com",
"HEADERS": Object {
"authentication": "hello",
@ -205,6 +219,7 @@ Beau {
},
Request {
"ALIAS": "user",
"COOKIEJAR": false,
"DEPENDENCIES": Set {},
"ENDPOINT": "http://jsonplaceholder.typicode.com",
"FORM": undefined,
@ -218,6 +233,7 @@ Beau {
"VERB": "GET",
"originalRequest": Object {
"ALIAS": "user",
"COOKIEJAR": false,
"ENDPOINT": "http://jsonplaceholder.typicode.com",
"HEADERS": Object {
"authentication": "hello",

View File

@ -3,6 +3,7 @@
exports[`Config should load multiple hosts 1`] = `
Config {
"CACHE": false,
"COOKIEJAR": false,
"DEFAULTS": Object {
"HEADERS": Object {
"hello": "mars",
@ -52,6 +53,7 @@ Config {
"REQUESTS": Array [
Object {
"ALIAS": "e1",
"COOKIEJAR": false,
"ENDPOINT": "http://example.org",
"HEADERS": Object {
"hello": "mars",
@ -60,6 +62,7 @@ Config {
},
Object {
"ALIAS": "com:e2",
"COOKIEJAR": false,
"ENDPOINT": "http://example.com",
"HEADERS": Object {
"hello": "world",
@ -69,6 +72,7 @@ Config {
},
Object {
"ALIAS": "com:posts",
"COOKIEJAR": false,
"ENDPOINT": "http://example.com",
"HEADERS": Object {
"hello": "world",
@ -78,6 +82,7 @@ Config {
},
Object {
"ALIAS": "net:e3",
"COOKIEJAR": false,
"ENDPOINT": "http://example.net",
"HEADERS": Object {
"hello": "world",
@ -87,6 +92,7 @@ Config {
},
Object {
"ALIAS": "net:posts",
"COOKIEJAR": false,
"ENDPOINT": "http://example.net",
"HEADERS": Object {
"hello": "world",
@ -96,6 +102,7 @@ Config {
},
Object {
"ALIAS": "info:posts",
"COOKIEJAR": false,
"ENDPOINT": "http://example.info",
"HEADERS": Object {
"hello": "mars",
@ -112,9 +119,11 @@ Config {
"DEFAULTS",
"ENVIRONMENT",
"HOSTS",
"COOKIEJAR",
],
"defaultConfigValues": Object {
"CACHE": false,
"COOKIEJAR": false,
"DEFAULTS": Object {},
"ENDPOINT": "",
"ENVIRONMENT": Object {},
@ -168,6 +177,7 @@ Config {
exports[`Config should set up defaults for all requests 1`] = `
Config {
"CACHE": false,
"COOKIEJAR": false,
"DEFAULTS": Object {
"HEADERS": Object {
"authentication": "hello",
@ -187,6 +197,7 @@ Config {
"REQUESTS": Array [
Object {
"ALIAS": "get-post",
"COOKIEJAR": false,
"ENDPOINT": "http://jsonplaceholder.typicode.com",
"HEADERS": Object {
"authentication": "hello",
@ -195,6 +206,7 @@ Config {
},
Object {
"ALIAS": "user",
"COOKIEJAR": false,
"ENDPOINT": "http://jsonplaceholder.typicode.com",
"HEADERS": Object {
"authentication": "hello",
@ -212,9 +224,11 @@ Config {
"DEFAULTS",
"ENVIRONMENT",
"HOSTS",
"COOKIEJAR",
],
"defaultConfigValues": Object {
"CACHE": false,
"COOKIEJAR": false,
"DEFAULTS": Object {},
"ENDPOINT": "",
"ENVIRONMENT": Object {},

View File

@ -11,7 +11,8 @@ class Config {
PLUGINS: [],
DEFAULTS: {},
ENVIRONMENT: {},
HOSTS: []
HOSTS: [],
COOKIEJAR: false
};
this.configKeys = Object.keys(this.defaultConfigValues);
@ -73,6 +74,7 @@ class Config {
}
request.REQUEST = key;
request.COOKIEJAR = this.COOKIEJAR;
request.ENDPOINT = settings.ENDPOINT;
let defaults = UpperCaseKeys(settings.DEFAULTS);

View File

@ -23,7 +23,8 @@ class Request {
ENDPOINT,
PARAMS,
HEADERS,
FORM
FORM,
COOKIEJAR
} = UpperCaseKeys(req);
if (!ALIAS) {
@ -42,6 +43,7 @@ class Request {
this.FORM = FORM;
this.ALIAS = ALIAS;
this.COOKIEJAR = COOKIEJAR;
this.DEPENDENCIES = this.findDependencies(req);
}
@ -70,6 +72,7 @@ class Request {
replacementRegex,
(match, g1) => !match.startsWith('\\') && matches.push(g1)
);
const deps = matches.map(m => m.split('.')[0]);
return new Set([...set, ...deps]);
@ -83,6 +86,7 @@ class Request {
baseUrl: this.ENDPOINT,
uri: this.PATH,
method: this.VERB,
jar: this.COOKIEJAR,
headers: this.HEADERS,
qs: this.PARAMS,

View File

@ -69,7 +69,6 @@ const replaceInObject = function(obj, fn) {
};
module.exports = {
httpVerbs,
requestRegex,
replacementRegex,
dynamicValueRegex,