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;
describe('Config', () => {
it('should load valid config keys', () => {
const doc = yaml.safeLoad(`
it('should load valid config keys', () => {
const doc = yaml.safeLoad(`
version: 1
endpoint: http://martianwabbit.com
shouldntBeAdded: true
`);
const config = new Config(doc);
expect(config.ENDPOINT).toBe(doc.endpoint);
expect(config.VERSION).toBe(doc.version);
expect(config.shouldntBeAdded).toBeUndefined();
});
const config = new Config(doc);
expect(config.ENDPOINT).toBe(doc.endpoint);
expect(config.VERSION).toBe(doc.version);
expect(config.shouldntBeAdded).toBeUndefined();
});
it('should load requests', () => {
const doc = yaml.safeLoad(`
it('should load requests', () => {
const doc = yaml.safeLoad(`
endpoint: http://example.com
GET /profile: get-profile
@ -31,12 +31,12 @@ describe('Config', () => {
hello: world
`);
const config = new Config(doc);
expect(Object.keys(config.REQUESTS).length).toBe(4);
});
const config = new Config(doc);
expect(Object.keys(config.REQUESTS).length).toBe(4);
});
it('should set up defaults for all requests', () => {
const doc = yaml.safeLoad(`
it('should set up defaults for all requests', () => {
const doc = yaml.safeLoad(`
version: 1
endpoint: 'http://example.com'
@ -51,16 +51,16 @@ describe('Config', () => {
hello: world
`);
const config = new Config(doc);
const config = new Config(doc);
expect(config).toMatchSnapshot();
Object.values(config.REQUESTS).forEach(r => {
expect(r.HEADERS.authentication).toMatch('hello');
expect(config).toMatchSnapshot();
Object.values(config.REQUESTS).forEach(r => {
expect(r.HEADERS.authentication).toMatch('hello');
});
});
});
it('should load multiple hosts', () => {
const doc = yaml.safeLoad(`
it('should load multiple hosts', () => {
const doc = yaml.safeLoad(`
version: 1
endpoint: http://example.org
@ -99,13 +99,13 @@ describe('Config', () => {
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', () => {
const doc = yaml.safeLoad(`
it('should namespace all aliases within an host', () => {
const doc = yaml.safeLoad(`
hosts:
- host: test1
endpoint: http://example.com
@ -115,14 +115,14 @@ describe('Config', () => {
GET /posts: posts
`);
let config = new Config(doc);
let config = new Config(doc);
expect(config.REQUESTS[0].ALIAS).toBe('test1:posts');
expect(config.REQUESTS[1].ALIAS).toBe('test2:posts');
});
expect(config.REQUESTS[0].ALIAS).toBe('test1:posts');
expect(config.REQUESTS[1].ALIAS).toBe('test2:posts');
});
it(`should throw if host doesn't have a host key`, () => {
const doc = yaml.safeLoad(`
it(`should throw if host doesn't have a host key`, () => {
const doc = yaml.safeLoad(`
hosts:
- endpoint: http://example.com
GET /posts: posts
@ -132,11 +132,11 @@ describe('Config', () => {
GET /posts: posts
`);
expect(() => new Config(doc)).toThrow();
});
expect(() => new Config(doc)).toThrow();
});
it(`should merge host settings with global settings`, () => {
const doc = yaml.safeLoad(`
it(`should merge host settings with global settings`, () => {
const doc = yaml.safeLoad(`
defaults:
headers:
hello: 1
@ -154,7 +154,23 @@ describe('Config', () => {
GET /posts: posts
`);
let config = new Config(doc);
expect(config.REQUESTS[0].HEADERS.hello).toBe(1);
});
let config = new Config(doc);
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) {
let requests = Object.entries(host)
Object.entries(host)
.filter(([key]) => requestRegex.test(key))
.map(([key, rDefinition]) => {
let requestDefinitionIsString = typeof rDefinition === 'string';
let originalRequest = requestDefinitionIsString
? { ALIAS: rDefinition }
: rDefinition;
let request = UpperCaseKeys(originalRequest);
if (settings.NAMESPACE) {
request.ALIAS = `${settings.NAMESPACE}:${request.ALIAS}`;
.forEach(([key, rDefinition]) => {
if (Array.isArray(rDefinition)) {
rDefinition.forEach(req =>
this.addRequest(key, req, settings)
);
} else {
this.addRequest(key, rDefinition, settings);
}
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) {