2022-08-11 17:39:37 +03:00
|
|
|
const shared = require('../');
|
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 () {
|
2019-07-05 14:40:43 +03:00
|
|
|
return shared.headers.get().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 () {
|
2018-12-17 14:47:19 +03:00
|
|
|
return shared.headers.get({}, {disposition: {type: 'json', value: 'value'}})
|
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 () {
|
2018-12-17 14:47:19 +03:00
|
|
|
return shared.headers.get({}, {disposition: {type: 'csv', value: 'my.csv'}})
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
result.should.eql({
|
|
|
|
'Content-Disposition': 'Attachment; filename="awesome-data-2022-08-01.csv"',
|
|
|
|
'Content-Type': 'text/csv'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('file', async function () {
|
|
|
|
const result = await shared.headers.get({}, {disposition: {type: 'file', value: 'my.txt'}});
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
result.should.eql({
|
|
|
|
'Content-Disposition': 'Attachment; filename="awesome-data-2022-08-01.txt"'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-05 01:50:45 +03:00
|
|
|
it('yaml', function () {
|
2018-12-17 14:47:19 +03:00
|
|
|
return shared.headers.get('yaml file', {disposition: {type: 'yaml', value: 'my.yaml'}})
|
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 () {
|
2018-12-17 14:47:19 +03:00
|
|
|
return shared.headers.get({}, {cacheInvalidate: true})
|
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 () {
|
2018-12-17 14:47:19 +03:00
|
|
|
return shared.headers.get({}, {cacheInvalidate: {value: 'value'}})
|
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 = {};
|
|
|
|
const frame = {
|
|
|
|
docName: 'posts',
|
|
|
|
method: 'add',
|
|
|
|
original: {
|
|
|
|
url: {
|
|
|
|
host: 'example.com',
|
2022-04-06 15:36:12 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const frame = {
|
|
|
|
docName: 'posts',
|
|
|
|
method: 'copy',
|
|
|
|
original: {
|
|
|
|
url: {
|
|
|
|
host: 'example.com',
|
|
|
|
pathname: `/api/content/posts/existing_post_id_value/copy`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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 = {};
|
|
|
|
const frame = {
|
|
|
|
docName: 'posts',
|
|
|
|
method: 'add',
|
|
|
|
original: {
|
|
|
|
url: {
|
|
|
|
host: 'example.com',
|
|
|
|
pathname: `/api/content/posts/`,
|
|
|
|
secure: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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 = {};
|
|
|
|
const frame = {
|
|
|
|
docName: 'posts',
|
|
|
|
method: 'add',
|
|
|
|
original: {
|
|
|
|
url: {
|
|
|
|
host: 'example.com',
|
2022-04-06 15:36:12 +03:00
|
|
|
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 = {};
|
|
|
|
const frame = {
|
|
|
|
docName: 'posts',
|
|
|
|
method: 'add',
|
|
|
|
original: {
|
|
|
|
url: {
|
|
|
|
host: 'example.com',
|
2022-04-06 15:36:12 +03:00
|
|
|
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
|
|
|
});
|