From d819a76ae8759743fc30bd6fdf141bfa0441dfef Mon Sep 17 00:00:00 2001 From: Paul Barry Date: Wed, 18 Oct 2023 17:37:13 -0400 Subject: [PATCH 01/10] Issue 1016: Initital attempt at using fetch over request --- .../develop.default/develop.default.spec.js | 33 +++++-------------- 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/packages/plugin-typescript/test/cases/develop.default/develop.default.spec.js b/packages/plugin-typescript/test/cases/develop.default/develop.default.spec.js index b99d5d05d..5414e6581 100644 --- a/packages/plugin-typescript/test/cases/develop.default/develop.default.spec.js +++ b/packages/plugin-typescript/test/cases/develop.default/develop.default.spec.js @@ -18,7 +18,6 @@ */ import chai from 'chai'; import path from 'path'; -import request from 'request'; import { Runner } from 'gallinago'; import { fileURLToPath, URL } from 'url'; import { runSmokeTest } from '../../../../../test/smoke-test.js'; @@ -58,37 +57,23 @@ describe('Develop Greenwood With: ', function() { describe('Develop command specific .ts behaviors', function() { let response = {}; + let data; before(async function() { - return new Promise((resolve, reject) => { - request.get({ - url: `http://127.0.0.1:${port}/main.ts` - }, (err, res) => { - if (err) { - reject(); - } - - response = res; - - resolve(); - }); - }); + response = await fetch(`http://127.0.0.1:${port}/main.ts`); + data = await response.text(); }); - it('should return a 200', function(done) { - expect(response.statusCode).to.equal(200); - - done(); + it('should return a 200', function() { + expect(response.status).to.equal(200); }); - it('should return the correct content type', function(done) { - expect(response.headers['content-type']).to.equal('text/javascript'); - done(); + it('should return the correct content type', function() { + expect(response.headers.get('content-type')).to.contain('text/javascript'); }); - it('should return an ECMAScript module', function(done) { - expect(response.body.trim().indexOf('const user')).to.equal(0); - done(); + it('should return an ECMAScript module', function() { + expect(data.trim().indexOf('const user')).to.equal(0); }); }); }); From 23f9a3907d2366e98219f11fa45c897a6761be1c Mon Sep 17 00:00:00 2001 From: Paul Barry Date: Wed, 18 Oct 2023 22:33:08 -0400 Subject: [PATCH 02/10] Issue-1016: Refactor IP to use localhost url --- .../cases/serve.default/serve.default.spec.js | 32 ++++++------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/packages/cli/test/cases/serve.default/serve.default.spec.js b/packages/cli/test/cases/serve.default/serve.default.spec.js index d30ace017..0677e8d88 100644 --- a/packages/cli/test/cases/serve.default/serve.default.spec.js +++ b/packages/cli/test/cases/serve.default/serve.default.spec.js @@ -46,7 +46,7 @@ describe('Serve Greenwood With: ', function() { const LABEL = 'Default Greenwood Configuration and Workspace'; const cliPath = path.join(process.cwd(), 'packages/cli/src/index.js'); const outputPath = fileURLToPath(new URL('.', import.meta.url)); - const hostname = 'http://127.0.0.1:8181'; + const hostname = 'http://localhost:8181'; let runner; before(function() { @@ -76,35 +76,23 @@ describe('Serve Greenwood With: ', function() { // proxies to https://jsonplaceholder.typicode.com/posts via greenwood.config.js describe('Serve command with dev proxy', function() { let response = {}; + let data; before(async function() { - return new Promise((resolve, reject) => { - request.get(`${hostname}/posts?id=7`, (err, res, body) => { - if (err) { - reject(); - } - - response = res; - response.body = JSON.parse(body); - - resolve(); - }); - }); + response = await fetch(`${hostname}/posts?id=7`); + data = await response.json(); }); - it('should return a 200 status', function(done) { - expect(response.statusCode).to.equal(200); - done(); + it('should return a 200 status', function() { + expect(response.status).to.equal(200); }); - it('should return the correct content type', function(done) { - expect(response.headers['content-type']).to.equal('application/json; charset=utf-8'); - done(); + it('should return the correct content type', function() { + expect(response.headers.get('content-type')).to.equal('application/json; charset=utf-8'); }); - it('should return the correct response body', function(done) { - expect(response.body).to.have.lengthOf(1); - done(); + it('should return the correct response body', function() { + expect(data).to.have.lengthOf(1); }); }); From 4b5bcfe2c8a5478176a559b7aa479f235e5eef5a Mon Sep 17 00:00:00 2001 From: Paul Barry Date: Wed, 18 Oct 2023 21:38:59 -0400 Subject: [PATCH 03/10] Issue-1016: Initial POST refactor to fetch --- .../develop.default/develop.default.spec.js | 42 ++++++------------- 1 file changed, 13 insertions(+), 29 deletions(-) diff --git a/packages/cli/test/cases/develop.default/develop.default.spec.js b/packages/cli/test/cases/develop.default/develop.default.spec.js index 602b1e08a..3c89be0ce 100644 --- a/packages/cli/test/cases/develop.default/develop.default.spec.js +++ b/packages/cli/test/cases/develop.default/develop.default.spec.js @@ -1470,46 +1470,30 @@ describe('Develop Greenwood With: ', function() { describe('Develop command with POST API specific behaviors for JSON', function() { const param = 'Greenwood'; let response = {}; + let data; before(async function() { - return new Promise((resolve, reject) => { - request.post({ - url: `${hostname}:${port}/api/submit-json`, - json: true, - body: { name: param } - }, (err, res, body) => { - if (err) { - reject(); - } - - response = res; - response.body = body; - - resolve(response); - }); + response = await fetch(`${hostname}:${port}/api/submit-json`, { + method: 'POST', + body: JSON.stringify({ name: param }) }); + data = await response.json(); }); - it('should return a 200 status', function(done) { - expect(response.statusCode).to.equal(200); - done(); + it('should return a 200 status', function() { + expect(response.status).to.equal(200); }); - it('should return the expected response message', function(done) { - const { message } = response.body; - - expect(message).to.equal(`Thank you ${param} for your submission!`); - done(); + it('should return the expected response message', function() { + expect(data.message).to.equal(`Thank you ${param} for your submission!`); }); - it('should return the expected content type header', function(done) { - expect(response.headers['content-type']).to.equal('application/json'); - done(); + it('should return the expected content type header', function() { + expect(response.headers.get('content-type')).to.equal('application/json'); }); - it('should return the secret header in the response', function(done) { - expect(response.headers['x-secret']).to.equal('1234'); - done(); + it('should return the secret header in the response', function() { + expect(response.headers.get('x-secret')).to.equal('1234'); }); }); From b1d197f920950d9908f5a24084dd060d9dafaf59 Mon Sep 17 00:00:00 2001 From: Paul Barry Date: Thu, 19 Oct 2023 01:01:44 -0400 Subject: [PATCH 04/10] Issue-1016: Progress on removal of request --- .../qraphql-server/graphql-server.spec.js | 72 ++++++------------- .../develop.default/develop.default.spec.js | 67 +++++------------ .../develop.default/develop.default.spec.js | 33 +++------ .../cases/serve.default/serve.default.spec.js | 49 ++++--------- 4 files changed, 65 insertions(+), 156 deletions(-) diff --git a/packages/plugin-graphql/test/cases/qraphql-server/graphql-server.spec.js b/packages/plugin-graphql/test/cases/qraphql-server/graphql-server.spec.js index ead6131e9..4fa353c24 100644 --- a/packages/plugin-graphql/test/cases/qraphql-server/graphql-server.spec.js +++ b/packages/plugin-graphql/test/cases/qraphql-server/graphql-server.spec.js @@ -15,7 +15,6 @@ * Greenwood default (src/) */ import chai from 'chai'; -import request from 'request'; import path from 'path'; import { Runner } from 'gallinago'; import { fileURLToPath, URL } from 'url'; @@ -26,12 +25,12 @@ describe('Develop Greenwood With: ', function() { const LABEL = 'GraphQL Server'; const cliPath = path.join(process.cwd(), 'packages/cli/src/index.js'); const outputPath = fileURLToPath(new URL('.', import.meta.url)); - const hostname = '127.0.0.1'; + const hostname = 'localhost'; const port = 4000; let runner; before(function() { - runner = new Runner(); + runner = new Runner(true); }); describe(LABEL, function() { @@ -56,42 +55,29 @@ describe('Develop Greenwood With: ', function() { }; before(async function() { - return new Promise((resolve, reject) => { - request.get({ - url: `http://${hostname}:${port}`, - headers: { - accept: 'text/html' - } - }, (err, res) => { - if (err) { - reject(); - } - - response.status = res.statusCode; - response.headers = res.headers; - - resolve(response); - }); + response = await fetch(`http://${hostname}:${port}`, { + headers: { + accept: 'text/html' + } }); }); - it('should return a 200 status', function(done) { + it('should return a 200 status', function() { expect(response.status).to.equal(200); - done(); }); - it('should return the correct content type', function(done) { - expect(response.headers['content-type']).to.equal('text/html'); - done(); + it('should return the correct content type', function() { + expect(response.headers.get('content-type')).to.equal('text/html'); }); }); // test a query call - describe('Develop command with GraphQL server and running a query', function() { + describe.only('Develop command with GraphQL server and running a query', function() { let response = { body: '', code: 0 }; + let data; const body = { 'operationName': null, @@ -100,38 +86,26 @@ describe('Develop Greenwood With: ', function() { }; before(async function() { - return new Promise((resolve, reject) => { - request.post({ - url: `http://${hostname}:${port}/graphql`, - json: true, - body - }, (err, res, body) => { - if (err) { - reject(); - } - - response.status = res.statusCode; - response.headers = res.headers; - response.body = body; - - resolve(response); - }); + response = await fetch(`http://${hostname}:${port}/graphql`, { + method: 'POST', + body: JSON.stringify(body), + headers: { + 'content-type': 'application/json' + } }); + data = await response.text(); }); - it('should return a 200 status', function(done) { + it('should return a 200 status', function() { expect(response.status).to.equal(200); - done(); }); - it('should return the correct content type', function(done) { - expect(response.headers['content-type']).to.equal('application/json; charset=utf-8'); - done(); + it('should return the correct content type', function() { + expect(response.headers.get('content-type')).to.equal('application/json; charset=utf-8'); }); - it('should return the expected query response', function(done) { - expect(response.body.data.config.workspace).to.equal(new URL('./src/', import.meta.url).href); - done(); + it('should return the expected query response', function() { + expect(data.config.workspace).to.equal(new URL('./src/', import.meta.url).href); }); }); }); diff --git a/packages/plugin-import-css/test/cases/develop.default/develop.default.spec.js b/packages/plugin-import-css/test/cases/develop.default/develop.default.spec.js index 601c872a3..45c06f766 100644 --- a/packages/plugin-import-css/test/cases/develop.default/develop.default.spec.js +++ b/packages/plugin-import-css/test/cases/develop.default/develop.default.spec.js @@ -24,7 +24,6 @@ */ import chai from 'chai'; import path from 'path'; -import request from 'request'; import { Runner } from 'gallinago'; import { fileURLToPath, URL } from 'url'; import { runSmokeTest } from '../../../../../test/smoke-test.js'; @@ -64,42 +63,27 @@ describe('Develop Greenwood With: ', function() { describe('Develop command specific ESM .css behaviors', function() { let response = {}; + let data; before(async function() { - return new Promise((resolve, reject) => { - request.get({ - url: `http://127.0.0.1:${port}/main.css?type=css` - }, (err, res, body) => { - if (err) { - reject(); - } - - response = res; - response.body = body; - - resolve(); - }); - }); + response = await fetch(`http://localhost:${port}/main.css?type=css`); + data = await response.text(); }); - it('should return a 200', function(done) { - expect(response.statusCode).to.equal(200); - - done(); + it('should return a 200', function() { + expect(response.status).to.equal(200); }); - it('should return the correct content type', function(done) { - expect(response.headers['content-type']).to.equal('text/javascript'); - done(); + it('should return the correct content type', function() { + expect(response.headers.get('content-type')).to.equal('text/javascript'); }); // https://github.com/ProjectEvergreen/greenwood/issues/766 // https://unpkg.com/browse/bootstrap@4.6.1/dist/css/bootstrap.css // https://unpkg.com/browse/font-awesome@4.7.0/css/font-awesome.css - it('should return an ECMASCript module', function(done) { - expect(response.body.replace('\n', '').replace(/ /g, '').trim()) + it('should return an ECMASCript module', function() { + expect(data.replace('\n', '').replace(/ /g, '').trim()) .to.equal('constcss=`*{background-image:url("/assets/background.jpg");font-family:\'Arial\'}.blockquote-footer::before{content:"\\\\2014\\\\00A0";}.fa-chevron-right:before{content:"\\\\f054";}`;exportdefaultcss;'); // eslint-disable-line max-len - done(); }); }); @@ -107,38 +91,23 @@ describe('Develop Greenwood With: ', function() { // https://unpkg.com/browse/@material/mwc-button@0.22.1/styles.css.js describe('Develop command specific ESM .css.js files behaviors (CSS in disguise)', function() { let response = {}; + let data; before(async function() { - return new Promise((resolve, reject) => { - request.get({ - url: `http://127.0.0.1:${port}/styles.css.js` - }, (err, res, body) => { - if (err) { - reject(); - } - - response = res; - response.body = body; - - resolve(); - }); - }); + response = await fetch(`http://localhost:${port}/styles.css.js`); + data = await response.text(); }); - it('should return a 200', function(done) { - expect(response.statusCode).to.equal(200); - - done(); + it('should return a 200', function() { + expect(response.status).to.equal(200); }); - it('should return the correct content type', function(done) { - expect(response.headers['content-type']).to.equal('text/javascript'); - done(); + it('should return the correct content type', function() { + expect(response.headers.get('content-type')).to.equal('text/javascript'); }); - it('should return an ECMASCript module', function(done) { - expect(response.body).to.equal('export const styles = css `.mdc-touch-target-wrapper{display:inline}`;'); - done(); + it('should return an ECMASCript module', function() { + expect(data).to.equal('export const styles = css `.mdc-touch-target-wrapper{display:inline}`;'); }); }); }); diff --git a/packages/plugin-import-json/test/cases/develop.default/develop.default.spec.js b/packages/plugin-import-json/test/cases/develop.default/develop.default.spec.js index cc694b00c..2cdadb981 100644 --- a/packages/plugin-import-json/test/cases/develop.default/develop.default.spec.js +++ b/packages/plugin-import-json/test/cases/develop.default/develop.default.spec.js @@ -25,7 +25,6 @@ */ import chai from 'chai'; import path from 'path'; -import request from 'request'; import { Runner } from 'gallinago'; import { fileURLToPath, URL } from 'url'; import { runSmokeTest } from '../../../../../test/smoke-test.js'; @@ -65,37 +64,23 @@ describe('Develop Greenwood With: ', function() { describe('Develop command specific ESM .json behaviors', function() { let response = {}; + let data; before(async function() { - return new Promise((resolve, reject) => { - request.get({ - url: `http://127.0.0.1:${port}/main.json?type=json` - }, (err, res) => { - if (err) { - reject(); - } - - response = res; - - resolve(); - }); - }); + response = await fetch(`http://localhost:${port}/main.json?type=json`); + data = await response.text(); }); - it('should return a 200', function(done) { - expect(response.statusCode).to.equal(200); - - done(); + it('should return a 200', function() { + expect(response.status).to.equal(200); }); - it('should return the correct content type', function(done) { - expect(response.headers['content-type']).to.equal('text/javascript'); - done(); + it('should return the correct content type', function() { + expect(response.headers.get('content-type')).to.equal('text/javascript'); }); - it('should return an ECMAScript module', function(done) { - expect(response.body).to.equal('export default {"status":200,"message":"got json"}'); - done(); + it('should return an ECMAScript module', function() { + expect(data).to.equal('export default {"status":200,"message":"got json"}'); }); }); }); diff --git a/packages/plugin-renderer-lit/test/cases/serve.default/serve.default.spec.js b/packages/plugin-renderer-lit/test/cases/serve.default/serve.default.spec.js index 19650b886..6dd4b3ba2 100644 --- a/packages/plugin-renderer-lit/test/cases/serve.default/serve.default.spec.js +++ b/packages/plugin-renderer-lit/test/cases/serve.default/serve.default.spec.js @@ -28,7 +28,6 @@ import fs from 'fs'; import { JSDOM } from 'jsdom'; import path from 'path'; import { getSetupFiles, getDependencyFiles, getOutputTeardownFiles } from '../../../../../test/utils.js'; -import request from 'request'; import { Runner } from 'gallinago'; import { fileURLToPath, URL } from 'url'; @@ -38,7 +37,7 @@ describe('Serve Greenwood With: ', function() { const LABEL = 'Custom Lit Renderer for SSR'; const cliPath = path.join(process.cwd(), 'packages/cli/src/index.js'); const outputPath = fileURLToPath(new URL('.', import.meta.url)); - const hostname = 'http://127.0.0.1:8080'; + const hostname = 'http://localhost:8080'; let runner; before(async function() { @@ -140,6 +139,7 @@ describe('Serve Greenwood With: ', function() { let response = {}; let artists = []; + let data; let dom; let usersPageDom; let usersPageHtml; @@ -151,50 +151,31 @@ describe('Serve Greenwood With: ', function() { aboutPageGraphData = graph.filter(page => page.route === '/artists/')[0]; - return new Promise((resolve, reject) => { - request.get(`${hostname}/artists/`, (err, res, body) => { - if (err) { - reject(); - } + response = await fetch(`${hostname}/artists/`); + data = await response.text(); + dom = new JSDOM(data); - response = res; - response.body = body; - dom = new JSDOM(body); - - request.get(`${hostname}/users/`, (err, res, body) => { - if (err) { - reject(); - } - - usersPageHtml = body; - usersPageDom = new JSDOM(body); - - resolve(); - }); - }); - }); + response = await fetch(`${hostname}/users/`); + usersPageHtml = await response.text(); + usersPageDom = new JSDOM(usersPageHtml); }); describe('Serve command with HTML route response using getBody, getTemplate and getFrontmatter', function() { - it('should return a 200 status', function(done) { - expect(response.statusCode).to.equal(200); - done(); + it('should return a 200 status', function() { + expect(response.status).to.equal(200); }); - it('should return the correct content type', function(done) { - expect(response.headers['content-type']).to.equal('text/html'); - done(); + it('should return the correct content type', function() { + expect(response.headers.get('content-type')).to.equal('text/html'); }); - it('should return a response body', function(done) { - expect(response.body).to.not.be.undefined; - done(); + it('should return a response body', function() { + expect(data).to.not.be.undefined; }); - it('the response body should be valid HTML from JSDOM', function(done) { + it('the response body should be valid HTML from JSDOM', function() { expect(dom).to.not.be.undefined; - done(); }); it('should have one