Added support for multiple request configurations.

This allows multiple requests to hit the same verb+path combination.
This was previously impossible, to address it, it's now possible to pass
an array of request settings as the body of a request each request will
be added to the request list individually.

Example:
```
GET /some/path:
  - alias: first
    headers:
      request: first
  - alias: second
    headers:
      request: second
```
This commit is contained in:
David Diaz 2019-03-11 21:06:30 -06:00
parent 8a1ecc67bd
commit 839291e716
3 changed files with 1543 additions and 1518 deletions

2919
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -5,21 +5,21 @@ const requireg = require('requireg');
requireg.resolving = false; requireg.resolving = false;
describe('Config', () => { describe('Config', () => {
it('should load valid config keys', () => { it('should load valid config keys', () => {
const doc = yaml.safeLoad(` const doc = yaml.safeLoad(`
version: 1 version: 1
endpoint: http://martianwabbit.com endpoint: http://martianwabbit.com
shouldntBeAdded: true shouldntBeAdded: true
`); `);
const config = new Config(doc); const config = new Config(doc);
expect(config.ENDPOINT).toBe(doc.endpoint); expect(config.ENDPOINT).toBe(doc.endpoint);
expect(config.VERSION).toBe(doc.version); expect(config.VERSION).toBe(doc.version);
expect(config.shouldntBeAdded).toBeUndefined(); expect(config.shouldntBeAdded).toBeUndefined();
}); });
it('should load requests', () => { it('should load requests', () => {
const doc = yaml.safeLoad(` const doc = yaml.safeLoad(`
endpoint: http://example.com endpoint: http://example.com
GET /profile: get-profile GET /profile: get-profile
@ -31,12 +31,12 @@ describe('Config', () => {
hello: world hello: world
`); `);
const config = new Config(doc); const config = new Config(doc);
expect(Object.keys(config.REQUESTS).length).toBe(4); expect(Object.keys(config.REQUESTS).length).toBe(4);
}); });
it('should set up defaults for all requests', () => { it('should set up defaults for all requests', () => {
const doc = yaml.safeLoad(` const doc = yaml.safeLoad(`
version: 1 version: 1
endpoint: 'http://example.com' endpoint: 'http://example.com'
@ -51,16 +51,16 @@ describe('Config', () => {
hello: world hello: world
`); `);
const config = new Config(doc); const config = new Config(doc);
expect(config).toMatchSnapshot(); expect(config).toMatchSnapshot();
Object.values(config.REQUESTS).forEach(r => { Object.values(config.REQUESTS).forEach(r => {
expect(r.HEADERS.authentication).toMatch('hello'); expect(r.HEADERS.authentication).toMatch('hello');
});
}); });
});
it('should load multiple hosts', () => { it('should load multiple hosts', () => {
const doc = yaml.safeLoad(` const doc = yaml.safeLoad(`
version: 1 version: 1
endpoint: http://example.org endpoint: http://example.org
@ -99,13 +99,13 @@ describe('Config', () => {
GET /posts: posts GET /posts: posts
`); `);
let config = new Config(doc); let config = new Config(doc);
expect(config).toMatchSnapshot(); expect(config).toMatchSnapshot();
}); });
it('should namespace all aliases within an host', () => { it('should namespace all aliases within an host', () => {
const doc = yaml.safeLoad(` const doc = yaml.safeLoad(`
hosts: hosts:
- host: test1 - host: test1
endpoint: http://example.com endpoint: http://example.com
@ -115,14 +115,14 @@ describe('Config', () => {
GET /posts: posts GET /posts: posts
`); `);
let config = new Config(doc); let config = new Config(doc);
expect(config.REQUESTS[0].ALIAS).toBe('test1:posts'); expect(config.REQUESTS[0].ALIAS).toBe('test1:posts');
expect(config.REQUESTS[1].ALIAS).toBe('test2:posts'); expect(config.REQUESTS[1].ALIAS).toBe('test2:posts');
}); });
it(`should throw if host doesn't have a host key`, () => { it(`should throw if host doesn't have a host key`, () => {
const doc = yaml.safeLoad(` const doc = yaml.safeLoad(`
hosts: hosts:
- endpoint: http://example.com - endpoint: http://example.com
GET /posts: posts GET /posts: posts
@ -132,11 +132,11 @@ describe('Config', () => {
GET /posts: posts GET /posts: posts
`); `);
expect(() => new Config(doc)).toThrow(); expect(() => new Config(doc)).toThrow();
}); });
it(`should merge host settings with global settings`, () => { it(`should merge host settings with global settings`, () => {
const doc = yaml.safeLoad(` const doc = yaml.safeLoad(`
defaults: defaults:
headers: headers:
hello: 1 hello: 1
@ -154,7 +154,23 @@ describe('Config', () => {
GET /posts: posts GET /posts: posts
`); `);
let config = new Config(doc); let config = new Config(doc);
expect(config.REQUESTS[0].HEADERS.hello).toBe(1); expect(config.REQUESTS[0].HEADERS.hello).toBe(1);
}); });
it(`should allow different settings for the same request`, () => {
const doc = yaml.safeLoad(`
host: https://example.com
GET /1:
- alias: req1
headers:
request: 1
- alias: req2
headers:
request: 2
`);
let config = new Config(doc);
expect(config.REQUESTS.length).toBe(2);
});
}); });

View File

@ -52,30 +52,38 @@ class Config {
} }
loadRequests(host, settings) { loadRequests(host, settings) {
let requests = Object.entries(host) Object.entries(host)
.filter(([key]) => requestRegex.test(key)) .filter(([key]) => requestRegex.test(key))
.map(([key, rDefinition]) => { .forEach(([key, rDefinition]) => {
let requestDefinitionIsString = typeof rDefinition === 'string'; if (Array.isArray(rDefinition)) {
let originalRequest = requestDefinitionIsString rDefinition.forEach(req =>
? { ALIAS: rDefinition } this.addRequest(key, req, settings)
: rDefinition; );
} else {
let request = UpperCaseKeys(originalRequest); this.addRequest(key, rDefinition, settings);
if (settings.NAMESPACE) {
request.ALIAS = `${settings.NAMESPACE}:${request.ALIAS}`;
} }
request.REQUEST = key;
request.COOKIEJAR = this.COOKIEJAR;
request.ENDPOINT = settings.ENDPOINT;
let defaults = UpperCaseKeys(settings.DEFAULTS);
return deepMerge(defaults, request);
}); });
}
this.REQUESTS = this.REQUESTS.concat(requests); addRequest(key, rDefinition, settings) {
let requestDefinitionIsString = typeof rDefinition === 'string';
let originalRequest = requestDefinitionIsString
? { ALIAS: rDefinition }
: rDefinition;
let request = UpperCaseKeys(originalRequest);
if (settings.NAMESPACE) {
request.ALIAS = `${settings.NAMESPACE}:${request.ALIAS}`;
}
request.REQUEST = key;
request.COOKIEJAR = this.COOKIEJAR;
request.ENDPOINT = settings.ENDPOINT;
let defaults = UpperCaseKeys(settings.DEFAULTS);
this.REQUESTS.push(deepMerge(defaults, request));
} }
loadConfig(host) { loadConfig(host) {