diff --git a/package-lock.json b/package-lock.json index 3b419fe9..89bb6e7c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,6 +21,18 @@ "epsagon": "1.64.1" } }, + "@adobe/helix-fetch": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/@adobe/helix-fetch/-/helix-fetch-1.6.0.tgz", + "integrity": "sha512-YdZSt9/HLWblfU9QS+e/mRhLvEDrlqApOLfuSBVbW372KR+L16kSuJ6niR6WahmE+yfhmiRZEtCS877PM4G1DQ==", + "requires": { + "fetch-h2": "^2.4.3", + "get-stream": "^5.1.0", + "http-cache-semantics": "^4.0.3", + "lru-cache": "^5.1.1", + "object-sizeof": "^1.5.3" + } + }, "@adobe/helix-log": { "version": "4.5.1", "resolved": "https://registry.npmjs.org/@adobe/helix-log/-/helix-log-4.5.1.tgz", @@ -100,6 +112,29 @@ } } }, + "@adobe/helix-shared": { + "version": "7.3.0", + "resolved": "https://registry.npmjs.org/@adobe/helix-shared/-/helix-shared-7.3.0.tgz", + "integrity": "sha512-trZqFEPEpLHnZ1GN9AcltjV2GDDxfVZQ7rUCSIS7DeITq9FKkwQ08o6raWziR6q01GX/7E5IEhCGWJ2z5li98Q==", + "requires": { + "@adobe/helix-log": "^4.4.1", + "ajv": "^6.10.2", + "ferrum": "^1.4.1", + "fs-extra": "^9.0.0", + "lodash": "^4.17.15", + "object-hash": "^2.0.0", + "uri-js": "^4.2.2", + "uuid": "^8.0.0", + "yaml": "1.9.2" + }, + "dependencies": { + "uuid": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.0.0.tgz", + "integrity": "sha512-jOXGuXZAWdsTH7eZLtyXMqUb9EcWMGZNbL9YcGBJl4MH4nrxHmZJhEHvyLFrkxo+28uLb/NYRcStH48fnD0Vzw==" + } + } + }, "@adobe/helix-status": { "version": "7.1.3", "resolved": "https://registry.npmjs.org/@adobe/helix-status/-/helix-status-7.1.3.tgz", @@ -851,7 +886,6 @@ "version": "7.9.2", "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.9.2.tgz", "integrity": "sha512-NE2DtOdufG7R5vnfQUTehdTfNycfUANEtCa9PssN9O/xmTzP4E08UI797ixaei6hBEVL9BI/PsdJS5x7mWoB9Q==", - "dev": true, "requires": { "regenerator-runtime": "^0.13.4" }, @@ -859,8 +893,7 @@ "regenerator-runtime": { "version": "0.13.5", "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.5.tgz", - "integrity": "sha512-ZS5w8CpKFinUzOwW3c83oPeVXoNsrLsaCoLtJvAClH135j/R77RuymhiSErhm2lKcwSCIpmvIWSbDkIfAqKQlA==", - "dev": true + "integrity": "sha512-ZS5w8CpKFinUzOwW3c83oPeVXoNsrLsaCoLtJvAClH135j/R77RuymhiSErhm2lKcwSCIpmvIWSbDkIfAqKQlA==" } } }, @@ -1697,6 +1730,11 @@ "@types/node": "*" } }, + "@types/tough-cookie": { + "version": "2.3.7", + "resolved": "https://registry.npmjs.org/@types/tough-cookie/-/tough-cookie-2.3.7.tgz", + "integrity": "sha512-rMQbgMGxnLsdn8e9aPVyuN+zMQLrZ2QW8xlv7eWS1mydfGXN+tsTKffcIzd8rGCcLdmi3xvQw2MDaZI1bBNTaw==" + }, "@webassemblyjs/ast": { "version": "1.9.0", "resolved": "https://registry.npmjs.org/@webassemblyjs/ast/-/ast-1.9.0.tgz", @@ -1976,6 +2014,14 @@ "integrity": "sha512-RO1ibKvd27e6FEShVFfPALuHI3WjSVNeK5FIsmme/LYRNxjKuNj+Dt7bucLa6NdSv3JcVTyMlm9kGR84z1XpaQ==", "dev": true }, + "already": { + "version": "1.12.0", + "resolved": "https://registry.npmjs.org/already/-/already-1.12.0.tgz", + "integrity": "sha512-JOq+vzTKA3qheL4rOcTYvdUwUhLFrUSiRshlDxxj+7CJTQlw9qHGveuMzEbyGRfoOscREA1gqJxxTnK7GDAfxQ==", + "requires": { + "throat": "^5.0.0" + } + }, "ansi-colors": { "version": "3.2.3", "resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-3.2.3.tgz", @@ -2346,8 +2392,7 @@ "at-least-node": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/at-least-node/-/at-least-node-1.0.0.tgz", - "integrity": "sha512-+q/t7Ekv1EDY2l6Gda6LLiX14rU9TV20Wa3ofeQmwPFZbOMo9DXrLbOjFaaclkXKWidIaopwAObQDqwWtGUjqg==", - "dev": true + "integrity": "sha512-+q/t7Ekv1EDY2l6Gda6LLiX14rU9TV20Wa3ofeQmwPFZbOMo9DXrLbOjFaaclkXKWidIaopwAObQDqwWtGUjqg==" }, "atob": { "version": "2.1.2", @@ -2674,7 +2719,6 @@ "version": "5.6.0", "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.6.0.tgz", "integrity": "sha512-/gDYp/UtU0eA1ys8bOs9J6a+E/KWIY+DZ+Q2WESNUA0jFRsJOc0SNUO6xJ5SGA1xueg3NL65W6s+NY5l9cunuw==", - "dev": true, "requires": { "base64-js": "^1.0.2", "ieee754": "^1.1.4" @@ -2860,6 +2904,11 @@ "caller-callsite": "^2.0.0" } }, + "callguard": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/callguard/-/callguard-1.2.1.tgz", + "integrity": "sha512-QTxtuE+QrPA/ZStfYEf/IL1RqyOEpg3CMKp35oTs4n91WveYjbnbPJ0O2ob+RxhPzOuvb0KgQvYwJzhZ6nswXQ==" + }, "callsites": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/callsites/-/callsites-3.1.0.tgz", @@ -4404,7 +4453,6 @@ "version": "1.4.4", "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.4.tgz", "integrity": "sha512-+uw1inIHVPQoaVuHzRyXd21icM+cnt4CzD5rW+NC1wjOUSTOs+Te7FOv7AhN7vS9x/oIyhLP5PR1H+phQAHu5Q==", - "dev": true, "requires": { "once": "^1.4.0" } @@ -5277,6 +5325,40 @@ "lodash.isplainobject": "4.0.6" } }, + "fetch-h2": { + "version": "2.4.5", + "resolved": "https://registry.npmjs.org/fetch-h2/-/fetch-h2-2.4.5.tgz", + "integrity": "sha512-Nl6wvKqwHPBKlGbXQsnA55EgzI62p/pTb5ISrfxJEAb7ShwcrITEQHAVHKiuf9Z3ulMd64fkildG/bQB4J3qzw==", + "requires": { + "@types/tough-cookie": "^2.3.6", + "already": "^1.12.0", + "callguard": "^1.2.1", + "get-stream": "^5.1.0", + "through2": "^3.0.1", + "to-arraybuffer": "^1.0.1", + "tough-cookie": "^3.0.1" + }, + "dependencies": { + "through2": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/through2/-/through2-3.0.1.tgz", + "integrity": "sha512-M96dvTalPT3YbYLaKaCuwu+j06D/8Jfib0o/PxbVt6Amhv3dUAtW6rTV1jPgJSBG83I/e04Y6xkVdVhSRhi0ww==", + "requires": { + "readable-stream": "2 || 3" + } + }, + "tough-cookie": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-3.0.1.tgz", + "integrity": "sha512-yQyJ0u4pZsv9D4clxO69OEjLWYw+jbgspjTue4lTQZLfV0c5l1VmK2y1JK8E9ahdpltPOaAThPcp5nKPUgSnsg==", + "requires": { + "ip-regex": "^2.1.0", + "psl": "^1.1.28", + "punycode": "^2.1.1" + } + } + } + }, "figgy-pudding": { "version": "3.5.2", "resolved": "https://registry.npmjs.org/figgy-pudding/-/figgy-pudding-3.5.2.tgz", @@ -5591,7 +5673,6 @@ "version": "9.0.0", "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-9.0.0.tgz", "integrity": "sha512-pmEYSk3vYsG/bF651KPUXZ+hvjpgWYw/Gc7W9NFUe3ZVLczKKWIij3IKpOrQcdw4TILtibFslZ0UmR8Vvzig4g==", - "dev": true, "requires": { "at-least-node": "^1.0.0", "graceful-fs": "^4.2.0", @@ -6267,7 +6348,6 @@ "version": "5.1.0", "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-5.1.0.tgz", "integrity": "sha512-EXr1FOzrzTfGeL0gQdeFEvOMm2mzMOglyiOXSTpPC+iAjAKftbr3jpCMWynogwYnM+eSj9sHGc6wjIcDvYiygw==", - "dev": true, "requires": { "pump": "^3.0.0" } @@ -6512,8 +6592,7 @@ "graceful-fs": { "version": "4.2.3", "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.3.tgz", - "integrity": "sha512-a30VEBm4PEdx1dRB7MFK7BejejvCvBronbLjht+sHuGYj8PHs7M/5Z+rt5lw551vZ7yfTCj4Vuyy3mSJytDWRQ==", - "dev": true + "integrity": "sha512-a30VEBm4PEdx1dRB7MFK7BejejvCvBronbLjht+sHuGYj8PHs7M/5Z+rt5lw551vZ7yfTCj4Vuyy3mSJytDWRQ==" }, "growl": { "version": "1.10.5", @@ -6747,6 +6826,11 @@ "integrity": "sha512-H2iMtd0I4Mt5eYiapRdIDjp+XzelXQ0tFE4JS7YFwFevXXMmOp9myNrUvCg0D6ws8iqkRPBfKHgbwig1SmlLfg==", "dev": true }, + "http-cache-semantics": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/http-cache-semantics/-/http-cache-semantics-4.1.0.tgz", + "integrity": "sha512-carPklcUh7ROWRK7Cv27RPtdhYhUsela/ue5/jKzjegVvXDqM2ILE9Q2BGn9JZJh1g87cp56su/FgQSzcWS8cQ==" + }, "http-proxy-agent": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/http-proxy-agent/-/http-proxy-agent-4.0.0.tgz", @@ -6843,8 +6927,7 @@ "ieee754": { "version": "1.1.13", "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.1.13.tgz", - "integrity": "sha512-4vf7I2LYV/HaWerSo3XmlMkp5eZ83i+/CDluXi/IGTs/O1sejBNhTtnxzmRZfvOUqj7lZjqHkeTvpgSFDlWZTg==", - "dev": true + "integrity": "sha512-4vf7I2LYV/HaWerSo3XmlMkp5eZ83i+/CDluXi/IGTs/O1sejBNhTtnxzmRZfvOUqj7lZjqHkeTvpgSFDlWZTg==" }, "iferr": { "version": "0.1.5", @@ -7074,8 +7157,7 @@ "ip-regex": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/ip-regex/-/ip-regex-2.1.0.tgz", - "integrity": "sha1-+ni/XS5pE8kRzp+BnuUUa7bYROk=", - "dev": true + "integrity": "sha1-+ni/XS5pE8kRzp+BnuUUa7bYROk=" }, "is-accessor-descriptor": { "version": "0.1.6", @@ -7825,7 +7907,6 @@ "version": "6.0.1", "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.0.1.tgz", "integrity": "sha512-jR2b5v7d2vIOust+w3wtFKZIfpC2pnRmFAhAC/BuweZFQR8qZzxH1OyrQ10HmdVYiXWkYUqPVsz91cG7EL2FBg==", - "dev": true, "requires": { "graceful-fs": "^4.1.6", "universalify": "^1.0.0" @@ -13216,6 +13297,11 @@ "integrity": "sha1-ciu9tgA576R8rTxtws5RqFwCxa4=", "dev": true }, + "object-hash": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/object-hash/-/object-hash-2.0.3.tgz", + "integrity": "sha512-JPKn0GMu+Fa3zt3Bmr66JhokJU5BaNBIh4ZeTlaCBzrBsOeXzwcKKAK1tbLiPKgvwmPXsDvvLHoWh5Bm7ofIYg==" + }, "object-inspect": { "version": "1.7.0", "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.7.0.tgz", @@ -13228,6 +13314,14 @@ "integrity": "sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA==", "dev": true }, + "object-sizeof": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/object-sizeof/-/object-sizeof-1.6.0.tgz", + "integrity": "sha512-Z+suoK94o8WjEMYItJjpPyG6I78STcI1CxM7rjxM8LhbgegAY+jhGSFY0c5Ez20LXgDGtyKzYZNXHpDyr6hj8Q==", + "requires": { + "buffer": "^5.5.0" + } + }, "object-to-spawn-args": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/object-to-spawn-args/-/object-to-spawn-args-1.1.1.tgz", @@ -13850,7 +13944,6 @@ "version": "3.0.0", "resolved": "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz", "integrity": "sha512-LwZy+p3SFs1Pytd/jYct4wpv49HiYCqd9Rlc5ZVdk0V+8Yzv6jR5Blk3TRmPL1ft69TxP0IMZGJ+WPFU2BFhww==", - "dev": true, "requires": { "end-of-stream": "^1.1.0", "once": "^1.3.1" @@ -15802,6 +15895,11 @@ "integrity": "sha1-f17oI66AUgfACvLfSoTsP8+lcLQ=", "dev": true }, + "throat": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/throat/-/throat-5.0.0.tgz", + "integrity": "sha512-fcwX4mndzpLQKBS1DVYhGAcYaYt7vsHNIvQV+WXMvnow5cgjPphq5CaayLaGsjRdSCKZFNGt7/GYAuXaNOiYCA==" + }, "through": { "version": "2.3.8", "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", @@ -15839,8 +15937,7 @@ "to-arraybuffer": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/to-arraybuffer/-/to-arraybuffer-1.0.1.tgz", - "integrity": "sha1-fSKbH8xjfkZsoIEYCDanqr/4P0M=", - "dev": true + "integrity": "sha1-fSKbH8xjfkZsoIEYCDanqr/4P0M=" }, "to-fast-properties": { "version": "2.0.0", @@ -16082,8 +16179,7 @@ "universalify": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/universalify/-/universalify-1.0.0.tgz", - "integrity": "sha512-rb6X1W158d7pRQBg5gkR8uPaSfiids68LTJQYOtEUhoJUWBdaQHsuT/EUduxXYxcrt4r5PJ4fuHW1MHT6p0qug==", - "dev": true + "integrity": "sha512-rb6X1W158d7pRQBg5gkR8uPaSfiids68LTJQYOtEUhoJUWBdaQHsuT/EUduxXYxcrt4r5PJ4fuHW1MHT6p0qug==" }, "unset-value": { "version": "1.0.0", @@ -16658,7 +16754,6 @@ "version": "1.9.2", "resolved": "https://registry.npmjs.org/yaml/-/yaml-1.9.2.tgz", "integrity": "sha512-HPT7cGGI0DuRcsO51qC1j9O16Dh1mZ2bnXwsi0jrSpsLz0WxOLSLXfkABVl6bZO629py3CU+OMJtpNHDLB97kg==", - "dev": true, "requires": { "@babel/runtime": "^7.9.2" } diff --git a/package.json b/package.json index e2aea7f3..8b7ae6d0 100644 --- a/package.json +++ b/package.json @@ -31,8 +31,10 @@ }, "homepage": "https://github.com/adobe/helix-data-embed#readme", "dependencies": { + "@adobe/helix-fetch": "1.6.0", "@adobe/helix-epsagon": "1.3.1", "@adobe/helix-onedrive-support": "2.3.0", + "@adobe/helix-shared": "7.3.0", "@adobe/helix-status": "7.1.3", "@adobe/openwhisk-action-logger": "2.2.0", "@adobe/openwhisk-action-utils": "4.2.2", diff --git a/src/embed.js b/src/embed.js index 6ab69b3c..5526822a 100644 --- a/src/embed.js +++ b/src/embed.js @@ -12,9 +12,11 @@ const feed = require('./matchers/feed'); const excel = require('./matchers/excel'); const google = require('./matchers/google'); +// eslint-disable-next-line camelcase +const runQuery = require('./matchers/run-query'); const matchers = [ - feed, excel, google, + feed, excel, google, runQuery, ]; function hasParams(list, params) { diff --git a/src/matchers/run-query.js b/src/matchers/run-query.js new file mode 100644 index 00000000..37ccc23f --- /dev/null +++ b/src/matchers/run-query.js @@ -0,0 +1,58 @@ +/* + * Copyright 2020 Adobe. All rights reserved. + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy + * of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS + * OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ +/* eslint-disable camelcase */ +const { fetch } = require('@adobe/helix-fetch'); +const { utils } = require('@adobe/helix-shared'); + +async function extract(url, params, log = console) { + const host = 'https://adobeioruntime.net'; + const path = '/api/v1/web/helix/helix-services/run-query@v2/'; + const query = url.split('/').pop(); + const resource = `${host}${path}${query}`; + const DEFAULT_CACHE = 'max-age=600'; + + const results = await fetch(url.startsWith(host) ? url : resource); + const statusCode = utils.propagateStatusCode(results.status); + const logLevel = utils.logLevelForStatusCode(results.status); + const cacheControl = results.headers.get('cache-control'); + + try { + if (!results.ok) { + throw new Error(await results.text()); + } + return { + statusCode, + headers: { + 'Content-Type': 'application/json', + 'Cache-Control': cacheControl || DEFAULT_CACHE, + }, + body: (await results.json()).results, + }; + } catch (e) { + log[logLevel](`data request to ${resource} failed ${e.message}`); + return { + statusCode, + headers: { + 'Content-Type': 'application/json', + 'Cache-Control': 'max-age=60', + }, + body: [], + }; + } +} + +module.exports = { + required: [], + pattern: (url) => /(^https:\/\/adobeioruntime\.net\/api\/v1\/web\/helix\/helix-services\/run-query@.*)/.test(url) + || /^\/?_query\/run-query\/.*$/.test(new URL(url).pathname), + extract, +}; diff --git a/test/index.test.js b/test/index.test.js index 6033c1dc..fde4ec60 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -21,6 +21,35 @@ describe('Integration Tests', () => { assert.equal(result.statusCode, 400); }); + it('tests index with absolute run_query url', async () => { + const EXPECTED_HEADERS = { + 'Cache-Control': 'max-age=600', + 'Content-Type': 'application/json', + }; + const { body, headers, statusCode } = await main({ + __ow_path: '/https://adobeioruntime.net/api/v1/web/helix/helix-services/run-query@2.4.11/error500', + }); + + assert.ok(Array.isArray(body)); + assert.deepEqual(EXPECTED_HEADERS, headers); + assert.equal(statusCode, 200); + }).timeout(6000); + + it('tests index with relative run_query url', async () => { + const EXPECTED_HEADERS = { + 'Cache-Control': 'max-age=600', + 'Content-Type': 'application/json', + }; + const { body, headers, statusCode } = await main({ + __ow_path: 'https://example.com/_query/run-query/error500', + __ow_query: 'fromMins=1000&toMins=0', + }); + + assert.ok(Array.isArray(body)); + assert.deepEqual(EXPECTED_HEADERS, headers); + assert.equal(statusCode, 200); + }).timeout(6000); + it('Rejects missing parameters', async () => { const result = await main(); assert.equal(result.statusCode, 400); diff --git a/test/post-deploy.test.js b/test/post-deploy.test.js index 0a049df8..70978d49 100644 --- a/test/post-deploy.test.js +++ b/test/post-deploy.test.js @@ -71,7 +71,6 @@ describe('Post-Deploy Tests', () => { .request('https://adobeioruntime.net/') .get(`${getbaseurl()}/https://adobe-my.sharepoint.com/personal/trieloff_adobe_com/_layouts/15/guestaccess.aspx?share=Edz_l4D0BghJjLkIfyZCB7sBLaBhySyT5An7fPHVS6CFuA&email=helix%40adobe.com&e=e5ziwf`) .then((response) => { - // console.log(response.body); expect(response).to.have.status(200); expect(response).to.be.json; expect(response.body).to.be.an('array').that.deep.includes({ @@ -131,4 +130,34 @@ describe('Post-Deploy Tests', () => { throw e; }); }).timeout(10000); + + it('Helix Run Query Embed', async () => { + console.log('Trying', `https://adobeioruntime.net/${getbaseurl()}/https://adobeioruntime.net/api/v1/web/helix/helix-services/run-query@v2/error500`); + + await chai + .request('https://adobeioruntime.net/') + .get(`${getbaseurl()}/https://adobeioruntime.net/api/v1/web/helix/helix-services/run-query@v2/error500?fromMins=1000&toMins=0`) + .then((response) => { + expect(response).to.be.json; + expect(response.body).to.be.an('array'); + expect(response).to.have.status(200); + }).catch((e) => { + throw e; + }); + }).timeout(10000); + + it('Helix Run Query Embed Works with Site prefixed', async () => { + console.log('Trying', `https://adobeioruntime.net/${getbaseurl()}/https://example.com/_query/run-query/error500?fromMins=1000&toMins=0`); + + await chai + .request('https://adobeioruntime.net/') + .get(`${getbaseurl()}/https://example.com/_query/run-query/error500?fromMins=1000&toMins=0`) + .then((response) => { + expect(response).to.be.json; + expect(response.body).to.be.an('array'); + expect(response).to.have.status(200); + }).catch((e) => { + throw e; + }); + }).timeout(10000); }); diff --git a/test/run-query.test.js b/test/run-query.test.js new file mode 100644 index 00000000..98cdbcf6 --- /dev/null +++ b/test/run-query.test.js @@ -0,0 +1,39 @@ +/* + * Copyright 2020 Adobe. All rights reserved. + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy + * of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS + * OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ +/* eslint-env mocha */ +const assert = require('assert'); +// eslint-disable-next-line camelcase +const runQuery = require('../src/matchers/run-query.js'); + +describe('run query tests', () => { + it('run query data embeds work', async () => { + const EXPECTED_HEADERS = { + 'Cache-Control': 'max-age=600', + 'Content-Type': 'application/json', + }; + const { body, headers, statusCode } = await runQuery.extract('https://adobeioruntime.net/api/v1/web/helix/helix-services/run-query@2.4.11/error500'); + assert.ok(Array.isArray(body)); + assert.deepEqual(EXPECTED_HEADERS, headers); + assert.equal(statusCode, 200); + }).timeout(6000); + + it('run query data embeds fail gracefully', async () => { + const EXPECTED_HEADERS = { + 'Cache-Control': 'max-age=60', + 'Content-Type': 'application/json', + }; + const { body, headers, statusCode } = await runQuery.extract('/run_query/fail'); + assert.ok(Array.isArray(body)); + assert.deepEqual(EXPECTED_HEADERS, headers); + assert.equal(statusCode, 502); + }).timeout(6000); +});