From 45933ba3d093a201cbed70ead4a43a48f250b3d8 Mon Sep 17 00:00:00 2001 From: Tim Donohue Date: Fri, 22 Oct 2021 17:13:01 -0500 Subject: [PATCH 1/2] Add initial yarn test:rest-api script --- package.json | 1 + scripts/test-rest.ts | 64 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 scripts/test-rest.ts diff --git a/package.json b/package.json index db884b54a8..44833bb226 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "test:watch": "npm-run-all --parallel config:test:watch test", "test": "ng test --sourceMap=true --watch=true", "test:headless": "ng test --watch=false --sourceMap=true --browsers=ChromeHeadless --code-coverage", + "test:rest-api": "yarn run config:prod && ts-node --project ./tsconfig.ts-node.json scripts/test-rest.ts", "lint": "ng lint", "lint-fix": "ng lint --fix=true", "e2e": "ng e2e", diff --git a/scripts/test-rest.ts b/scripts/test-rest.ts new file mode 100644 index 0000000000..d5cf24eb4e --- /dev/null +++ b/scripts/test-rest.ts @@ -0,0 +1,64 @@ +import * as http from 'http'; +import * as https from 'https'; +import { environment } from '../src/environments/environment'; + +/** + * Script to test the connection with the configured REST API (in the 'rest' settings of your environment.*.ts) + * + * This script is useful to test for any Node.js connection issues with your REST API. + * + * Usage (see package.json): yarn test:rest-api + */ + +// Get root URL of configured REST API +const restUrl = environment.rest.baseUrl + '/api'; +console.log(`...Testing connection to REST API at ${restUrl}...\n`); + +// If SSL enabled, test via HTTPS, else via HTTP +if (environment.rest.ssl) { + const req = https.request(restUrl, (res) => { + console.log(`RESPONSE: ${res.statusCode} ${res.statusMessage} \n`); + res.on('data', (data) => { + checkJSONResponse(data); + }); + }); + + req.on('error', error => { + console.error('ERROR connecting to REST API\n' + error); + }); + + req.end(); +} else { + const req = http.request(restUrl, (res) => { + console.log(`RESPONSE: ${res.statusCode} ${res.statusMessage} \n`); + res.on('data', (data) => { + checkJSONResponse(data); + }); + }); + + req.on('error', error => { + console.error('ERROR connecting to REST API\n' + error); + }); + + req.end(); +} + +/** + * Check JSON response from REST API to see if it looks valid. Log useful information + * @param responseData response data + */ +function checkJSONResponse(responseData: any): any { + let parsedData; + try { + parsedData = JSON.parse(responseData); + console.log('Checking JSON returned for validity...'); + console.log(`\t"dspaceVersion" = ${parsedData.dspaceVersion}`); + console.log(`\t"dspaceUI" = ${parsedData.dspaceUI}`); + console.log(`\t"dspaceServer" = ${parsedData.dspaceServer}`); + console.log(`\t"dspaceServer" property matches UI's "rest" config? ${(parsedData.dspaceServer === environment.rest.baseUrl)}`); + console.log(`\tDoes "_links" section have values? ${!parsedData._links.length}`); + } catch (err) { + console.error('ERROR: INVALID DSPACE REST API! Response is not valid JSON!'); + console.error(`Response returned:\n${responseData}`); + } +} From d0e4055bf09d7c65b0609294aefdd6ef18af6ac2 Mon Sep 17 00:00:00 2001 From: Tim Donohue Date: Mon, 25 Oct 2021 11:27:33 -0500 Subject: [PATCH 2/2] Address feedback --- package.json | 2 +- scripts/test-rest.ts | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 44833bb226..afb89f1196 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "config:test": "ts-node --project ./tsconfig.ts-node.json scripts/set-mock-env.ts", "config:test:watch": "nodemon --config mock-nodemon.json", "config:dev:watch": "nodemon", + "config:check:rest": "yarn run config:prod && ts-node --project ./tsconfig.ts-node.json scripts/test-rest.ts", "prestart:dev": "yarn run config:dev", "prebuild": "yarn run config:dev", "pretest": "yarn run config:test", @@ -29,7 +30,6 @@ "test:watch": "npm-run-all --parallel config:test:watch test", "test": "ng test --sourceMap=true --watch=true", "test:headless": "ng test --watch=false --sourceMap=true --browsers=ChromeHeadless --code-coverage", - "test:rest-api": "yarn run config:prod && ts-node --project ./tsconfig.ts-node.json scripts/test-rest.ts", "lint": "ng lint", "lint-fix": "ng lint --fix=true", "e2e": "ng e2e", diff --git a/scripts/test-rest.ts b/scripts/test-rest.ts index d5cf24eb4e..b12a9929c2 100644 --- a/scripts/test-rest.ts +++ b/scripts/test-rest.ts @@ -56,7 +56,9 @@ function checkJSONResponse(responseData: any): any { console.log(`\t"dspaceUI" = ${parsedData.dspaceUI}`); console.log(`\t"dspaceServer" = ${parsedData.dspaceServer}`); console.log(`\t"dspaceServer" property matches UI's "rest" config? ${(parsedData.dspaceServer === environment.rest.baseUrl)}`); - console.log(`\tDoes "_links" section have values? ${!parsedData._links.length}`); + // Check for "authn" and "sites" in "_links" section as they should always exist (even if no data)! + const linksFound: string[] = Object.keys(parsedData._links); + console.log(`\tDoes "/api" endpoint have HAL links ("_links" section)? ${linksFound.includes('authn') && linksFound.includes('sites')}`); } catch (err) { console.error('ERROR: INVALID DSPACE REST API! Response is not valid JSON!'); console.error(`Response returned:\n${responseData}`);