Allow urls as endpoints (#55)

* 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
```

* Allows using full urls in the request key

This allows you to break out of the top-level endpoint. If you send a
full url instead of a path it'll be used instead of the top level
endpoint.

Example:
```
endpoint: http://example.com
GET /posts: get-posts
POST http://api.example.com/posts: post-post
```
This commit is contained in:
Sergio Díaz 2019-03-11 22:04:20 -06:00 committed by GitHub
parent 94332bd125
commit fea72dde2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 3 deletions

View File

@ -73,4 +73,28 @@ describe('Request', () => {
requestPromiseNativeMock.fail = true; requestPromiseNativeMock.fail = true;
await expect(requestWithoutDependencies.exec()).rejects.toThrow(Error); await expect(requestWithoutDependencies.exec()).rejects.toThrow(Error);
}); });
it(`should use the full url if given one as part of the path instead of the global endpoint`, async () => {
const requestWithPath = new Request({
endpoint: 'http://example.com',
request: 'GET http://martianwabbit.com/user',
alias: 'get-user'
});
const requestWithoutPath = new Request({
endpoint: 'http://example.com',
request: 'GET /user',
alias: 'get-user'
});
await expect(requestWithPath.exec()).resolves.toHaveProperty(
'request.endpoint',
'http://martianwabbit.com/user'
);
await expect(requestWithoutPath.exec()).resolves.toHaveProperty(
'request.endpoint',
'http://example.com/user'
);
});
}); });

View File

@ -6,7 +6,8 @@ const {
requestRegex, requestRegex,
replacementRegex, replacementRegex,
UpperCaseKeys, UpperCaseKeys,
removeOptionalKeys removeOptionalKeys,
isUrl
} = require('./shared'); } = require('./shared');
class Request { class Request {
@ -63,8 +64,10 @@ class Request {
} }
async exec(cache = new RequestCache()) { async exec(cache = new RequestCache()) {
const isPathFullUrl = isUrl(this.PATH);
let settings = cache.parse({ let settings = cache.parse({
baseUrl: this.ENDPOINT, baseUrl: isPathFullUrl ? '' : this.ENDPOINT,
uri: this.PATH, uri: this.PATH,
method: this.VERB, method: this.VERB,
jar: this.COOKIEJAR, jar: this.COOKIEJAR,

View File

@ -1,3 +1,5 @@
const { URL } = require('url');
const httpVerbs = [ const httpVerbs = [
'GET', 'GET',
'HEAD', 'HEAD',
@ -66,6 +68,15 @@ const replaceInObject = function(obj, fn) {
const moduleVersion = () => parseInt(require('../package.json').version, 10); const moduleVersion = () => parseInt(require('../package.json').version, 10);
const isUrl = function(str) {
try {
new URL(str);
return true;
} catch (e) {
return false;
}
};
module.exports = { module.exports = {
requestRegex, requestRegex,
replacementRegex, replacementRegex,
@ -74,5 +85,6 @@ module.exports = {
removeOptionalKeys, removeOptionalKeys,
toKebabCase, toKebabCase,
replaceInObject, replaceInObject,
moduleVersion moduleVersion,
isUrl
}; };