2017-11-26 20:45:40 +00:00
|
|
|
const tag = require('..').templateTags[0];
|
2017-12-01 14:18:38 +00:00
|
|
|
const iconv = require('iconv-lite');
|
2017-11-26 20:45:40 +00:00
|
|
|
|
|
|
|
describe('Response tag', () => {
|
|
|
|
describe('General', () => {
|
|
|
|
it('fails on no responses', async () => {
|
2018-06-25 17:42:50 +00:00
|
|
|
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
|
2017-11-26 20:45:40 +00:00
|
|
|
|
|
|
|
const context = _genTestContext(requests);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await tag.run(context, 'body', 'req_1', '$.foo');
|
|
|
|
fail('JSON should have failed');
|
|
|
|
} catch (err) {
|
|
|
|
expect(err.message).toContain('No responses for request');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails on no request', async () => {
|
2018-06-25 17:42:50 +00:00
|
|
|
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
|
|
|
|
|
|
|
|
const responses = [
|
|
|
|
{
|
|
|
|
_id: 'res_1',
|
|
|
|
parentId: 'req_1',
|
|
|
|
statusCode: 200,
|
2018-12-12 17:36:11 +00:00
|
|
|
_body: '{"foo": "bar"}',
|
|
|
|
},
|
2018-06-25 17:42:50 +00:00
|
|
|
];
|
2017-11-26 20:45:40 +00:00
|
|
|
|
|
|
|
const context = _genTestContext(requests, responses);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await tag.run(context, 'body', 'req_test', '$.foo');
|
|
|
|
fail('JSON should have failed');
|
|
|
|
} catch (err) {
|
|
|
|
expect(err.message).toContain('Could not find request req_test');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails on empty filter', async () => {
|
2018-06-25 17:42:50 +00:00
|
|
|
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
|
|
|
|
|
|
|
|
const responses = [
|
|
|
|
{
|
|
|
|
_id: 'res_1',
|
|
|
|
parentId: 'req_1',
|
|
|
|
statusCode: 200,
|
2018-12-12 17:36:11 +00:00
|
|
|
_body: '{"foo": "bar"}',
|
|
|
|
},
|
2018-06-25 17:42:50 +00:00
|
|
|
];
|
2017-11-26 20:45:40 +00:00
|
|
|
|
|
|
|
const context = _genTestContext(requests, responses);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await tag.run(context, 'body', 'req_1', '');
|
|
|
|
fail('JSON should have failed');
|
|
|
|
} catch (err) {
|
|
|
|
expect(err.message).toContain('No body filter specified');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('JSONPath', () => {
|
|
|
|
it('basic query', async () => {
|
2018-06-25 17:42:50 +00:00
|
|
|
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
|
|
|
|
|
|
|
|
const responses = [
|
|
|
|
{
|
|
|
|
_id: 'res_1',
|
|
|
|
parentId: 'req_1',
|
|
|
|
statusCode: 200,
|
|
|
|
contentType: 'application/json',
|
2018-12-12 17:36:11 +00:00
|
|
|
_body: '{"foo": "bar"}',
|
|
|
|
},
|
2018-06-25 17:42:50 +00:00
|
|
|
];
|
2017-11-26 20:45:40 +00:00
|
|
|
|
|
|
|
const context = _genTestContext(requests, responses);
|
|
|
|
const result = await tag.run(context, 'body', 'req_1', '$.foo');
|
|
|
|
|
|
|
|
expect(result).toBe('bar');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails on invalid JSON', async () => {
|
2018-06-25 17:42:50 +00:00
|
|
|
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
|
|
|
|
|
|
|
|
const responses = [
|
|
|
|
{
|
|
|
|
_id: 'res_1',
|
|
|
|
parentId: 'req_1',
|
|
|
|
statusCode: 200,
|
|
|
|
contentType: 'application/json',
|
2018-12-12 17:36:11 +00:00
|
|
|
_body: '{"foo": "',
|
|
|
|
},
|
2018-06-25 17:42:50 +00:00
|
|
|
];
|
2017-11-26 20:45:40 +00:00
|
|
|
|
|
|
|
const context = _genTestContext(requests, responses);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await tag.run(context, 'body', 'req_1', '$.foo');
|
|
|
|
fail('JSON should have failed');
|
|
|
|
} catch (err) {
|
2018-10-17 16:42:33 +00:00
|
|
|
expect(err.message).toContain('Invalid JSON: Unexpected end of JSON input');
|
2017-11-26 20:45:40 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails on invalid query', async () => {
|
2018-06-25 17:42:50 +00:00
|
|
|
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
|
|
|
|
|
|
|
|
const responses = [
|
|
|
|
{
|
|
|
|
_id: 'res_1',
|
|
|
|
parentId: 'req_1',
|
|
|
|
statusCode: 200,
|
|
|
|
contentType: 'application/json',
|
2018-12-12 17:36:11 +00:00
|
|
|
_body: '{"foo": "bar"}',
|
|
|
|
},
|
2018-06-25 17:42:50 +00:00
|
|
|
];
|
2017-11-26 20:45:40 +00:00
|
|
|
|
|
|
|
const context = _genTestContext(requests, responses);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await tag.run(context, 'body', 'req_1', '$$');
|
|
|
|
fail('JSON should have failed');
|
|
|
|
} catch (err) {
|
|
|
|
expect(err.message).toContain('Invalid JSONPath query: $$');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails on no results', async () => {
|
2018-06-25 17:42:50 +00:00
|
|
|
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
|
|
|
|
|
|
|
|
const responses = [
|
|
|
|
{
|
|
|
|
_id: 'res_1',
|
|
|
|
parentId: 'req_1',
|
|
|
|
statusCode: 200,
|
|
|
|
contentType: 'application/json',
|
2018-12-12 17:36:11 +00:00
|
|
|
_body: '{"foo": "bar"}',
|
|
|
|
},
|
2018-06-25 17:42:50 +00:00
|
|
|
];
|
2017-11-26 20:45:40 +00:00
|
|
|
|
|
|
|
const context = _genTestContext(requests, responses);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await tag.run(context, 'body', 'req_1', '$.missing');
|
|
|
|
fail('JSON should have failed');
|
|
|
|
} catch (err) {
|
|
|
|
expect(err.message).toContain('Returned no results: $.missing');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails on more than 1 result', async () => {
|
2018-06-25 17:42:50 +00:00
|
|
|
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
|
|
|
|
|
|
|
|
const responses = [
|
|
|
|
{
|
|
|
|
_id: 'res_1',
|
|
|
|
parentId: 'req_1',
|
|
|
|
statusCode: 200,
|
|
|
|
contentType: 'application/json',
|
2018-12-12 17:36:11 +00:00
|
|
|
_body: '{"array": ["bar", "baz"]}',
|
|
|
|
},
|
2018-06-25 17:42:50 +00:00
|
|
|
];
|
2017-11-26 20:45:40 +00:00
|
|
|
|
|
|
|
const context = _genTestContext(requests, responses);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await tag.run(context, 'body', 'req_1', '$.array.*');
|
|
|
|
fail('JSON should have failed to parse');
|
|
|
|
} catch (err) {
|
2018-10-17 16:42:33 +00:00
|
|
|
expect(err.message).toContain('Returned more than one result: $.array.*');
|
2017-11-26 20:45:40 +00:00
|
|
|
}
|
|
|
|
});
|
2017-12-01 14:18:38 +00:00
|
|
|
|
|
|
|
it('works with utf-16 encoding', async () => {
|
2018-06-25 17:42:50 +00:00
|
|
|
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
|
|
|
|
|
|
|
|
const responses = [
|
|
|
|
{
|
|
|
|
_id: 'res_1',
|
|
|
|
parentId: 'req_1',
|
|
|
|
statusCode: 200,
|
|
|
|
contentType: 'application/json; charset=UTF-16',
|
2018-12-12 17:36:11 +00:00
|
|
|
_body: iconv.encode('{"array": ["bar", "baz"]}', 'UTF-16'),
|
|
|
|
},
|
2018-06-25 17:42:50 +00:00
|
|
|
];
|
2017-12-01 14:18:38 +00:00
|
|
|
|
|
|
|
const context = _genTestContext(requests, responses);
|
|
|
|
|
|
|
|
expect(await tag.run(context, 'body', 'req_1', '$.array[0]')).toBe('bar');
|
|
|
|
});
|
2017-11-26 20:45:40 +00:00
|
|
|
});
|
|
|
|
|
2019-05-08 17:59:16 +00:00
|
|
|
describe('XPath', () => {
|
2017-11-26 20:45:40 +00:00
|
|
|
it('renders basic response "body" query', async () => {
|
2018-06-25 17:42:50 +00:00
|
|
|
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
|
|
|
|
|
|
|
|
const responses = [
|
|
|
|
{
|
|
|
|
_id: 'res_1',
|
|
|
|
parentId: 'req_1',
|
|
|
|
statusCode: 200,
|
|
|
|
contentType: 'application/xml',
|
2018-12-12 17:36:11 +00:00
|
|
|
_body: '<foo><bar>Hello World!</bar></foo>',
|
|
|
|
},
|
2018-06-25 17:42:50 +00:00
|
|
|
];
|
2017-11-26 20:45:40 +00:00
|
|
|
|
|
|
|
const context = _genTestContext(requests, responses);
|
|
|
|
const result = await tag.run(context, 'body', 'req_1', '/foo/bar');
|
|
|
|
|
|
|
|
expect(result).toBe('Hello World!');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders basic response "body" attribute query', async () => {
|
2018-06-25 17:42:50 +00:00
|
|
|
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
|
|
|
|
|
|
|
|
const responses = [
|
|
|
|
{
|
|
|
|
_id: 'res_1',
|
|
|
|
parentId: 'req_1',
|
|
|
|
statusCode: 200,
|
|
|
|
contentType: 'application/xml',
|
2018-12-12 17:36:11 +00:00
|
|
|
_body: '<foo><bar hello="World">Hello World!</bar></foo>',
|
|
|
|
},
|
2018-06-25 17:42:50 +00:00
|
|
|
];
|
2017-11-26 20:45:40 +00:00
|
|
|
|
|
|
|
const context = _genTestContext(requests, responses);
|
|
|
|
const result = await tag.run(context, 'body', 'req_1', '/foo/bar/@hello');
|
|
|
|
|
|
|
|
expect(result).toBe('World');
|
|
|
|
});
|
|
|
|
|
2018-01-16 05:46:24 +00:00
|
|
|
it('renders query that does not start with slash', async () => {
|
2018-06-25 17:42:50 +00:00
|
|
|
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
|
|
|
|
|
|
|
|
const responses = [
|
|
|
|
{
|
|
|
|
_id: 'res_1',
|
|
|
|
parentId: 'req_1',
|
|
|
|
statusCode: 200,
|
|
|
|
contentType: 'application/xml',
|
2018-12-12 17:36:11 +00:00
|
|
|
_body: '<foo><bar hello="World">Hello World!</bar></foo>',
|
|
|
|
},
|
2018-06-25 17:42:50 +00:00
|
|
|
];
|
2018-01-16 05:46:24 +00:00
|
|
|
|
|
|
|
const context = _genTestContext(requests, responses);
|
2018-10-17 16:42:33 +00:00
|
|
|
const result = await tag.run(context, 'body', 'req_1', 'substring(/foo/bar, 7)');
|
2018-01-16 05:46:24 +00:00
|
|
|
|
|
|
|
expect(result).toBe('World!');
|
|
|
|
});
|
|
|
|
|
2017-11-26 20:45:40 +00:00
|
|
|
it('no results on invalid XML', async () => {
|
2018-06-25 17:42:50 +00:00
|
|
|
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
|
|
|
|
|
|
|
|
const responses = [
|
|
|
|
{
|
|
|
|
_id: 'res_1',
|
|
|
|
parentId: 'req_1',
|
|
|
|
statusCode: 200,
|
|
|
|
contentType: 'application/xml',
|
2018-12-12 17:36:11 +00:00
|
|
|
_body: '<hi></hi></sstr>',
|
|
|
|
},
|
2018-06-25 17:42:50 +00:00
|
|
|
];
|
2017-11-26 20:45:40 +00:00
|
|
|
|
|
|
|
const context = _genTestContext(requests, responses);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await tag.run(context, 'body', 'req_1', '/foo');
|
|
|
|
fail('Should have failed');
|
|
|
|
} catch (err) {
|
|
|
|
expect(err.message).toContain('Returned no results: /foo');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails on invalid query', async () => {
|
2018-06-25 17:42:50 +00:00
|
|
|
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
|
|
|
|
|
|
|
|
const responses = [
|
|
|
|
{
|
|
|
|
_id: 'res_1',
|
|
|
|
parentId: 'req_1',
|
|
|
|
statusCode: 200,
|
|
|
|
contentType: 'application/xml',
|
2018-12-12 17:36:11 +00:00
|
|
|
_body: '<foo></foo>',
|
|
|
|
},
|
2018-06-25 17:42:50 +00:00
|
|
|
];
|
2017-11-26 20:45:40 +00:00
|
|
|
|
|
|
|
const context = _genTestContext(requests, responses);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await tag.run(context, 'body', 'req_1', '//');
|
|
|
|
fail('Should have failed');
|
|
|
|
} catch (err) {
|
|
|
|
expect(err.message).toContain('Invalid XPath query: //');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails on no results', async () => {
|
2018-06-25 17:42:50 +00:00
|
|
|
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
|
|
|
|
|
|
|
|
const responses = [
|
|
|
|
{
|
|
|
|
_id: 'res_1',
|
|
|
|
parentId: 'req_1',
|
|
|
|
statusCode: 200,
|
|
|
|
contentType: 'application/xml',
|
2018-12-12 17:36:11 +00:00
|
|
|
_body: '<foo></foo>',
|
|
|
|
},
|
2018-06-25 17:42:50 +00:00
|
|
|
];
|
2017-11-26 20:45:40 +00:00
|
|
|
|
|
|
|
const context = _genTestContext(requests, responses);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await tag.run(context, 'body', 'req_1', '/missing');
|
|
|
|
fail('Should have failed');
|
|
|
|
} catch (err) {
|
|
|
|
expect(err.message).toContain('Returned no results: /missing');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails on more than 1 result', async () => {
|
2018-06-25 17:42:50 +00:00
|
|
|
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
|
|
|
|
|
|
|
|
const responses = [
|
|
|
|
{
|
|
|
|
_id: 'res_1',
|
|
|
|
parentId: 'req_1',
|
|
|
|
statusCode: 200,
|
|
|
|
contentType: 'application/xml',
|
2018-12-12 17:36:11 +00:00
|
|
|
_body: '<foo><bar>Hello World!</bar><bar>And again!</bar></foo>',
|
|
|
|
},
|
2018-06-25 17:42:50 +00:00
|
|
|
];
|
2017-11-26 20:45:40 +00:00
|
|
|
|
|
|
|
const context = _genTestContext(requests, responses);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await tag.run(context, 'body', 'req_1', '/foo/*');
|
|
|
|
fail('Should have failed');
|
|
|
|
} catch (err) {
|
|
|
|
expect(err.message).toContain('Returned more than one result: /foo/*');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-05-08 17:59:16 +00:00
|
|
|
describe('ResponseExtension Header', () => {
|
2017-11-26 20:45:40 +00:00
|
|
|
it('renders basic response "header"', async () => {
|
2018-06-25 17:42:50 +00:00
|
|
|
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
|
|
|
|
|
|
|
|
const responses = [
|
|
|
|
{
|
|
|
|
_id: 'res_1',
|
|
|
|
parentId: 'req_1',
|
|
|
|
statusCode: 200,
|
|
|
|
contentType: '',
|
|
|
|
headers: [
|
|
|
|
{ name: 'Content-Type', value: 'application/json' },
|
2018-12-12 17:36:11 +00:00
|
|
|
{ name: 'Content-Length', value: '20' },
|
|
|
|
],
|
|
|
|
},
|
2018-06-25 17:42:50 +00:00
|
|
|
];
|
2017-11-26 20:45:40 +00:00
|
|
|
|
|
|
|
const context = _genTestContext(requests, responses);
|
|
|
|
|
2018-10-17 16:42:33 +00:00
|
|
|
expect(await tag.run(context, 'header', 'req_1', 'content-type')).toBe('application/json');
|
|
|
|
expect(await tag.run(context, 'header', 'req_1', 'Content-Type')).toBe('application/json');
|
|
|
|
expect(await tag.run(context, 'header', 'req_1', 'CONTENT-type')).toBe('application/json');
|
|
|
|
expect(await tag.run(context, 'header', 'req_1', 'CONTENT-type ')).toBe(
|
2018-12-12 17:36:11 +00:00
|
|
|
'application/json',
|
2018-06-25 17:42:50 +00:00
|
|
|
);
|
2017-11-26 20:45:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('no results on missing header', async () => {
|
2018-06-25 17:42:50 +00:00
|
|
|
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
|
|
|
|
|
|
|
|
const responses = [
|
|
|
|
{
|
|
|
|
_id: 'res_1',
|
|
|
|
parentId: 'req_1',
|
|
|
|
statusCode: 200,
|
|
|
|
headers: [
|
|
|
|
{ name: 'Content-Type', value: 'application/json' },
|
2018-12-12 17:36:11 +00:00
|
|
|
{ name: 'Content-Length', value: '20' },
|
|
|
|
],
|
|
|
|
},
|
2018-06-25 17:42:50 +00:00
|
|
|
];
|
2017-11-26 20:45:40 +00:00
|
|
|
|
|
|
|
const context = _genTestContext(requests, responses);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await tag.run(context, 'header', 'req_1', 'missing');
|
|
|
|
fail('should have failed');
|
|
|
|
} catch (err) {
|
2018-06-25 17:42:50 +00:00
|
|
|
expect(err.message).toBe(
|
|
|
|
'No header with name "missing".\n' +
|
2018-12-12 17:36:11 +00:00
|
|
|
'Choices are [\n\t"Content-Type",\n\t"Content-Length"\n]',
|
2018-06-25 17:42:50 +00:00
|
|
|
);
|
2017-11-26 20:45:40 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-05-08 17:59:16 +00:00
|
|
|
describe('Raw', () => {
|
2017-11-26 20:45:40 +00:00
|
|
|
it('renders basic response', async () => {
|
2018-06-25 17:42:50 +00:00
|
|
|
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
|
|
|
|
|
|
|
|
const responses = [
|
|
|
|
{
|
|
|
|
_id: 'res_1',
|
|
|
|
parentId: 'req_1',
|
|
|
|
statusCode: 200,
|
|
|
|
contentType: 'text/plain',
|
2018-12-12 17:36:11 +00:00
|
|
|
_body: 'Hello World!',
|
|
|
|
},
|
2018-06-25 17:42:50 +00:00
|
|
|
];
|
2017-11-26 20:45:40 +00:00
|
|
|
|
|
|
|
const context = _genTestContext(requests, responses);
|
|
|
|
|
|
|
|
expect(await tag.run(context, 'raw', 'req_1')).toBe('Hello World!');
|
|
|
|
});
|
|
|
|
});
|
2019-05-15 20:01:36 +00:00
|
|
|
|
|
|
|
describe('Dependency sending', () => {
|
|
|
|
it('sends when behavior=always and no responses', async () => {
|
|
|
|
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
|
|
|
|
const responses = [];
|
|
|
|
const context = _genTestContext(requests, responses);
|
|
|
|
|
|
|
|
expect(await tag.run(context, 'raw', 'req_1', '', 'always')).toBe('Response res_1');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sends when behavior=always and some responses', async () => {
|
|
|
|
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
|
|
|
|
const responses = [
|
|
|
|
{
|
|
|
|
_id: 'res_1',
|
|
|
|
parentId: 'req_1',
|
|
|
|
statusCode: 200,
|
|
|
|
contentType: 'text/plain',
|
|
|
|
_body: 'Hello World!',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
const context = _genTestContext(requests, responses);
|
|
|
|
|
|
|
|
expect(await tag.run(context, 'raw', 'req_1', '', 'always')).toBe('Response res_2');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sends when behavior=no-history and no responses', async () => {
|
|
|
|
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
|
|
|
|
const responses = [];
|
|
|
|
const context = _genTestContext(requests, responses);
|
|
|
|
|
|
|
|
expect(await tag.run(context, 'raw', 'req_1', '', 'no-history')).toBe('Response res_1');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not send when behavior=no-history and some responses', async () => {
|
|
|
|
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
|
|
|
|
const responses = [
|
|
|
|
{
|
|
|
|
_id: 'res_existing',
|
|
|
|
parentId: 'req_1',
|
|
|
|
statusCode: 200,
|
|
|
|
contentType: 'text/plain',
|
|
|
|
_body: 'Response res_existing',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
const context = _genTestContext(requests, responses);
|
|
|
|
|
|
|
|
expect(await tag.run(context, 'raw', 'req_1', '', 'no-history')).toBe(
|
|
|
|
'Response res_existing',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-06-11 20:00:46 +00:00
|
|
|
it('sends when behavior=when-expired and no responses', async () => {
|
|
|
|
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
|
|
|
|
const responses = [];
|
|
|
|
const context = _genTestContext(requests, responses);
|
|
|
|
|
|
|
|
expect(await tag.run(context, 'raw', 'req_1', '', 'when-expired', 60)).toBe('Response res_1');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sends when behavior=when-expired and response is old', async () => {
|
|
|
|
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
|
|
|
|
const responses = [
|
|
|
|
{
|
|
|
|
created: Date.now() - 60000,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
const context = _genTestContext(requests, responses);
|
|
|
|
|
|
|
|
expect(await tag.run(context, 'raw', 'req_1', '', 'when-expired', 30)).toBe('Response res_2');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not send when behavior=when-expired and response is new', async () => {
|
|
|
|
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
|
|
|
|
const responses = [
|
|
|
|
{
|
|
|
|
_id: 'res_existing',
|
|
|
|
parentId: 'req_1',
|
|
|
|
statusCode: 200,
|
|
|
|
contentType: 'text/plain',
|
|
|
|
_body: 'Response res_existing',
|
|
|
|
created: Date.now() - 60000,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
const context = _genTestContext(requests, responses);
|
|
|
|
|
|
|
|
expect(await tag.run(context, 'raw', 'req_1', '', 'when-expired', 90)).toBe(
|
|
|
|
'Response res_existing',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2019-05-15 20:01:36 +00:00
|
|
|
it('does not send when behavior=never and no responses', async () => {
|
|
|
|
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
|
|
|
|
const responses = [];
|
|
|
|
const context = _genTestContext(requests, responses);
|
|
|
|
|
|
|
|
try {
|
|
|
|
expect(await tag.run(context, 'raw', 'req_1', '', 'never')).toBe('Response res_1');
|
|
|
|
} catch (err) {
|
|
|
|
expect(err.message).toBe('No responses for request');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('Running tag should have thrown exception');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not send when behavior=never and some responses', async () => {
|
|
|
|
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
|
|
|
|
const responses = [
|
|
|
|
{
|
|
|
|
_id: 'res_existing',
|
|
|
|
parentId: 'req_1',
|
|
|
|
statusCode: 200,
|
|
|
|
contentType: 'text/plain',
|
|
|
|
_body: 'Response res_existing',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
const context = _genTestContext(requests, responses);
|
|
|
|
|
|
|
|
expect(await tag.run(context, 'raw', 'req_1', '', 'never')).toBe('Response res_existing');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not resend recursive', async () => {
|
|
|
|
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
|
|
|
|
|
|
|
|
const responses = [];
|
|
|
|
|
|
|
|
const context = _genTestContext(requests, responses, { fromResponseTag: true });
|
|
|
|
|
|
|
|
try {
|
|
|
|
await tag.run(context, 'raw', 'req_1', '', 'always');
|
|
|
|
} catch (err) {
|
|
|
|
expect(err.message).toBe('No responses for request');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('Running tag should have thrown exception');
|
|
|
|
});
|
|
|
|
});
|
2017-11-26 20:45:40 +00:00
|
|
|
});
|
|
|
|
|
2019-05-15 20:01:36 +00:00
|
|
|
function _genTestContext(requests, responses, extraInfoRoot) {
|
|
|
|
let _extraInfo = extraInfoRoot || {};
|
2017-11-26 20:45:40 +00:00
|
|
|
requests = requests || [];
|
|
|
|
responses = responses || [];
|
|
|
|
const bodies = {};
|
|
|
|
for (const res of responses) {
|
|
|
|
bodies[res._id] = res._body || null;
|
|
|
|
delete res._body;
|
|
|
|
}
|
2019-05-08 17:59:16 +00:00
|
|
|
const store = {};
|
2017-11-26 20:45:40 +00:00
|
|
|
return {
|
2019-05-15 20:01:36 +00:00
|
|
|
renderPurpose: 'send',
|
|
|
|
context: {
|
2020-01-22 19:23:19 +00:00
|
|
|
getEnvironmentId() {
|
|
|
|
return null;
|
|
|
|
},
|
2019-05-15 20:01:36 +00:00
|
|
|
getExtraInfo(key) {
|
|
|
|
if (_extraInfo) {
|
|
|
|
return _extraInfo[key] || null;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
network: {
|
|
|
|
sendRequest(request, extraInfo) {
|
|
|
|
_extraInfo = { ..._extraInfo, ...extraInfo };
|
|
|
|
const id = `res_${responses.length + 1}`;
|
|
|
|
const res = {
|
|
|
|
_id: id,
|
|
|
|
parentId: request._id,
|
|
|
|
statusCode: 200,
|
|
|
|
contentType: 'text/plain',
|
|
|
|
};
|
|
|
|
|
|
|
|
bodies[res._id] = `Response ${id}`;
|
|
|
|
responses.push(res);
|
|
|
|
return res;
|
|
|
|
},
|
|
|
|
},
|
2019-05-08 17:59:16 +00:00
|
|
|
store: {
|
2020-04-09 17:32:19 +00:00
|
|
|
hasItem: key => Object.prototype.hasOwnProperty.call(store, key),
|
2019-05-08 17:59:16 +00:00
|
|
|
getItem: key => store[key],
|
|
|
|
removeItem: key => {
|
|
|
|
delete store[key];
|
|
|
|
},
|
|
|
|
setItem: (key, value) => {
|
|
|
|
store[key] = value;
|
|
|
|
},
|
|
|
|
},
|
2017-11-26 20:45:40 +00:00
|
|
|
util: {
|
|
|
|
models: {
|
|
|
|
request: {
|
2018-06-25 17:42:50 +00:00
|
|
|
getById(requestId) {
|
2017-11-26 20:45:40 +00:00
|
|
|
return requests.find(r => r._id === requestId) || null;
|
2018-12-12 17:36:11 +00:00
|
|
|
},
|
2017-11-26 20:45:40 +00:00
|
|
|
},
|
|
|
|
response: {
|
2020-02-11 18:45:47 +00:00
|
|
|
getLatestForRequestId(requestId, environmentId) {
|
2017-11-26 20:45:40 +00:00
|
|
|
return responses.find(r => r.parentId === requestId) || null;
|
|
|
|
},
|
2018-06-25 17:42:50 +00:00
|
|
|
getBodyBuffer(response) {
|
2017-12-01 14:18:38 +00:00
|
|
|
const strOrBuffer = bodies[response._id];
|
|
|
|
|
|
|
|
if (typeof strOrBuffer === 'string') {
|
|
|
|
return Buffer.from(strOrBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strOrBuffer) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return strOrBuffer;
|
2018-12-12 17:36:11 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-11-26 20:45:40 +00:00
|
|
|
};
|
|
|
|
}
|