2022-08-11 17:39:37 +03:00
|
|
|
const shared = require('../');
|
2024-02-28 00:13:08 +03:00
|
|
|
const Frame = require('../lib/Frame');
|
2018-10-05 01:50:45 +03:00
|
|
|
|
2022-08-11 17:42:21 +03:00
|
|
|
describe('Headers', function () {
|
2018-10-05 01:50:45 +03:00
|
|
|
it('empty headers config', function () {
|
2024-02-28 00:13:08 +03:00
|
|
|
return shared.headers.get({}, {}, new Frame()).then((result) => {
|
2018-12-17 14:47:19 +03:00
|
|
|
result.should.eql({});
|
|
|
|
});
|
2018-10-05 01:50:45 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('config.disposition', function () {
|
|
|
|
it('json', function () {
|
2024-02-28 00:13:08 +03:00
|
|
|
return shared.headers.get({}, {disposition: {type: 'json', value: 'value'}}, new Frame())
|
2019-07-05 14:40:43 +03:00
|
|
|
.then((result) => {
|
2018-12-17 14:47:19 +03:00
|
|
|
result.should.eql({
|
2022-08-11 17:39:37 +03:00
|
|
|
'Content-Disposition': 'Attachment; filename="value"',
|
2018-12-17 14:47:19 +03:00
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Content-Length': 2
|
|
|
|
});
|
|
|
|
});
|
2018-10-05 01:50:45 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('csv', function () {
|
2024-02-28 00:13:08 +03:00
|
|
|
return shared.headers.get({}, {disposition: {type: 'csv', value: 'my.csv'}}, new Frame())
|
2019-07-05 14:40:43 +03:00
|
|
|
.then((result) => {
|
2018-12-17 14:47:19 +03:00
|
|
|
result.should.eql({
|
2022-08-11 17:39:37 +03:00
|
|
|
'Content-Disposition': 'Attachment; filename="my.csv"',
|
2018-12-17 14:47:19 +03:00
|
|
|
'Content-Type': 'text/csv'
|
|
|
|
});
|
|
|
|
});
|
2018-10-05 01:50:45 +03:00
|
|
|
});
|
|
|
|
|
2022-08-12 09:34:20 +03:00
|
|
|
it('csv with function', async function () {
|
|
|
|
const result = await shared.headers.get({}, {
|
|
|
|
disposition: {
|
|
|
|
type: 'csv',
|
|
|
|
value() {
|
|
|
|
// pretend we're doing some dynamic filename logic in this function
|
|
|
|
const filename = `awesome-data-2022-08-01.csv`;
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
}
|
2024-02-28 00:13:08 +03:00
|
|
|
}, new Frame());
|
2022-08-12 09:34:20 +03:00
|
|
|
result.should.eql({
|
|
|
|
'Content-Disposition': 'Attachment; filename="awesome-data-2022-08-01.csv"',
|
|
|
|
'Content-Type': 'text/csv'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('file', async function () {
|
2024-02-28 00:13:08 +03:00
|
|
|
const result = await shared.headers.get({}, {disposition: {type: 'file', value: 'my.txt'}}, new Frame());
|
2022-08-12 09:34:20 +03:00
|
|
|
result.should.eql({
|
|
|
|
'Content-Disposition': 'Attachment; filename="my.txt"'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('file with function', async function () {
|
|
|
|
const result = await shared.headers.get({}, {
|
|
|
|
disposition: {
|
|
|
|
type: 'file',
|
|
|
|
value() {
|
|
|
|
// pretend we're doing some dynamic filename logic in this function
|
|
|
|
const filename = `awesome-data-2022-08-01.txt`;
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
}
|
2024-02-28 00:13:08 +03:00
|
|
|
}, new Frame());
|
2022-08-12 09:34:20 +03:00
|
|
|
result.should.eql({
|
|
|
|
'Content-Disposition': 'Attachment; filename="awesome-data-2022-08-01.txt"'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-05 01:50:45 +03:00
|
|
|
it('yaml', function () {
|
2024-02-28 00:13:08 +03:00
|
|
|
return shared.headers.get('yaml file', {disposition: {type: 'yaml', value: 'my.yaml'}}, new Frame())
|
2019-07-05 14:40:43 +03:00
|
|
|
.then((result) => {
|
2018-12-17 14:47:19 +03:00
|
|
|
result.should.eql({
|
2022-08-11 17:39:37 +03:00
|
|
|
'Content-Disposition': 'Attachment; filename="my.yaml"',
|
2018-12-17 14:47:19 +03:00
|
|
|
'Content-Type': 'application/yaml',
|
|
|
|
'Content-Length': 11
|
|
|
|
});
|
|
|
|
});
|
2018-10-05 01:50:45 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('config.cacheInvalidate', function () {
|
|
|
|
it('default', function () {
|
2024-02-28 00:13:08 +03:00
|
|
|
return shared.headers.get({}, {cacheInvalidate: true}, new Frame())
|
2019-07-05 14:40:43 +03:00
|
|
|
.then((result) => {
|
2018-12-17 14:47:19 +03:00
|
|
|
result.should.eql({
|
|
|
|
'X-Cache-Invalidate': '/*'
|
|
|
|
});
|
|
|
|
});
|
2018-10-05 01:50:45 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('custom value', function () {
|
2024-02-28 00:13:08 +03:00
|
|
|
return shared.headers.get({}, {cacheInvalidate: {value: 'value'}}, new Frame())
|
2019-07-05 14:40:43 +03:00
|
|
|
.then((result) => {
|
2018-12-17 14:47:19 +03:00
|
|
|
result.should.eql({
|
|
|
|
'X-Cache-Invalidate': 'value'
|
|
|
|
});
|
|
|
|
});
|
2018-10-05 01:50:45 +03:00
|
|
|
});
|
|
|
|
});
|
2020-09-14 13:33:37 +03:00
|
|
|
|
|
|
|
describe('location header', function () {
|
2023-05-15 11:30:32 +03:00
|
|
|
it('adds header when all needed data is present and method is add', function () {
|
2020-09-14 13:33:37 +03:00
|
|
|
const apiResult = {
|
|
|
|
posts: [{
|
|
|
|
id: 'id_value'
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
|
|
|
const apiConfigHeaders = {};
|
2024-02-28 00:13:08 +03:00
|
|
|
const frame = new Frame();
|
|
|
|
frame.docName = 'posts',
|
|
|
|
frame.method = 'add',
|
|
|
|
frame.original = {
|
|
|
|
url: {
|
|
|
|
host: 'example.com',
|
|
|
|
pathname: `/api/content/posts/`
|
2020-09-14 13:33:37 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return shared.headers.get(apiResult, apiConfigHeaders, frame)
|
|
|
|
.then((result) => {
|
|
|
|
result.should.eql({
|
|
|
|
// NOTE: the backslash in the end is important to avoid unecessary 301s using the header
|
2022-04-06 15:36:12 +03:00
|
|
|
Location: 'https://example.com/api/content/posts/id_value/'
|
2020-09-14 13:33:37 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-05-15 11:30:32 +03:00
|
|
|
it('adds header when a location resolver is provided', function () {
|
|
|
|
const apiResult = {
|
|
|
|
posts: [{
|
|
|
|
id: 'id_value'
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
|
|
|
const resolvedLocationUrl = 'resolved location';
|
|
|
|
|
|
|
|
const apiConfigHeaders = {
|
|
|
|
location: {
|
|
|
|
resolve() {
|
|
|
|
return resolvedLocationUrl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2024-02-28 00:13:08 +03:00
|
|
|
const frame = new Frame();
|
|
|
|
frame.docName = 'posts';
|
|
|
|
frame.method = 'copy';
|
|
|
|
frame.original = {
|
|
|
|
url: {
|
|
|
|
host: 'example.com',
|
|
|
|
pathname: `/api/content/posts/existing_post_id_value/copy`
|
2023-05-15 11:30:32 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return shared.headers.get(apiResult, apiConfigHeaders, frame)
|
|
|
|
.then((result) => {
|
|
|
|
result.should.eql({
|
|
|
|
Location: resolvedLocationUrl
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-08-12 09:34:20 +03:00
|
|
|
it('respects HTTP redirects', async function () {
|
|
|
|
const apiResult = {
|
|
|
|
posts: [{
|
|
|
|
id: 'id_value'
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
|
|
|
const apiConfigHeaders = {};
|
2024-02-28 00:13:08 +03:00
|
|
|
const frame = new Frame();
|
|
|
|
|
|
|
|
frame.docName = 'posts';
|
|
|
|
frame.method = 'add';
|
|
|
|
frame.original = {
|
|
|
|
url: {
|
|
|
|
host: 'example.com',
|
|
|
|
pathname: `/api/content/posts/`,
|
|
|
|
secure: false
|
2022-08-12 09:34:20 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const result = await shared.headers.get(apiResult, apiConfigHeaders, frame);
|
|
|
|
result.should.eql({
|
|
|
|
// NOTE: the backslash in the end is important to avoid unecessary 301s using the header
|
|
|
|
Location: 'http://example.com/api/content/posts/id_value/'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-09-14 13:33:37 +03:00
|
|
|
it('adds and resolves header to correct url when pathname does not contain backslash in the end', function () {
|
|
|
|
const apiResult = {
|
|
|
|
posts: [{
|
|
|
|
id: 'id_value'
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
|
|
|
const apiConfigHeaders = {};
|
2024-02-28 00:13:08 +03:00
|
|
|
const frame = new Frame();
|
|
|
|
frame.docName = 'posts';
|
|
|
|
frame.method = 'add';
|
|
|
|
frame.original = {
|
|
|
|
url: {
|
|
|
|
host: 'example.com',
|
|
|
|
pathname: `/api/content/posts`
|
2020-09-14 13:33:37 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return shared.headers.get(apiResult, apiConfigHeaders, frame)
|
|
|
|
.then((result) => {
|
|
|
|
result.should.eql({
|
|
|
|
// NOTE: the backslash in the end is important to avoid unecessary 301s using the header
|
2022-04-06 15:36:12 +03:00
|
|
|
Location: 'https://example.com/api/content/posts/id_value/'
|
2020-09-14 13:33:37 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not add header when missing result values', function () {
|
|
|
|
const apiResult = {};
|
|
|
|
|
|
|
|
const apiConfigHeaders = {};
|
2024-02-28 00:13:08 +03:00
|
|
|
const frame = new Frame();
|
|
|
|
frame.docName = 'posts';
|
|
|
|
frame.method = 'add';
|
|
|
|
frame.original = {
|
|
|
|
url: {
|
|
|
|
host: 'example.com',
|
|
|
|
pathname: `/api/content/posts/`
|
2020-09-14 13:33:37 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return shared.headers.get(apiResult, apiConfigHeaders, frame)
|
|
|
|
.then((result) => {
|
|
|
|
result.should.eql({});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-10-05 01:50:45 +03:00
|
|
|
});
|