mirror of https://github.com/Seich/Beau.git
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:
parent
94332bd125
commit
fea72dde2a
|
|
@ -73,4 +73,28 @@ describe('Request', () => {
|
|||
requestPromiseNativeMock.fail = true;
|
||||
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'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -6,7 +6,8 @@ const {
|
|||
requestRegex,
|
||||
replacementRegex,
|
||||
UpperCaseKeys,
|
||||
removeOptionalKeys
|
||||
removeOptionalKeys,
|
||||
isUrl
|
||||
} = require('./shared');
|
||||
|
||||
class Request {
|
||||
|
|
@ -63,8 +64,10 @@ class Request {
|
|||
}
|
||||
|
||||
async exec(cache = new RequestCache()) {
|
||||
const isPathFullUrl = isUrl(this.PATH);
|
||||
|
||||
let settings = cache.parse({
|
||||
baseUrl: this.ENDPOINT,
|
||||
baseUrl: isPathFullUrl ? '' : this.ENDPOINT,
|
||||
uri: this.PATH,
|
||||
method: this.VERB,
|
||||
jar: this.COOKIEJAR,
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
const { URL } = require('url');
|
||||
|
||||
const httpVerbs = [
|
||||
'GET',
|
||||
'HEAD',
|
||||
|
|
@ -66,6 +68,15 @@ const replaceInObject = function(obj, fn) {
|
|||
|
||||
const moduleVersion = () => parseInt(require('../package.json').version, 10);
|
||||
|
||||
const isUrl = function(str) {
|
||||
try {
|
||||
new URL(str);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
requestRegex,
|
||||
replacementRegex,
|
||||
|
|
@ -74,5 +85,6 @@ module.exports = {
|
|||
removeOptionalKeys,
|
||||
toKebabCase,
|
||||
replaceInObject,
|
||||
moduleVersion
|
||||
moduleVersion,
|
||||
isUrl
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue