1
0
mirror of https://github.com/oceanprotocol/commons.git synced 2023-03-15 18:03:00 +01:00

testing tweaks

This commit is contained in:
Matthias Kretschmann 2019-03-25 11:52:24 +01:00
parent c49fc85017
commit 2a0401be7f
Signed by: m
GPG Key ID: 606EEEF3C479A91F
6 changed files with 39 additions and 30 deletions

View File

@ -27,7 +27,7 @@ npm start
``` ```
This will run client and server in development mode.<br> This will run client and server in development mode.<br>
Open [http://localhost:3000](http://localhost:3000) to view the client in the browser. The server is Open [http://localhost:3000](http://localhost:3000) to view the client in the browser.
The page will reload if you make edits to files in either `./client` or `./server`. The page will reload if you make edits to files in either `./client` or `./server`.

View File

@ -8,9 +8,9 @@
"build": "./scripts/build.sh", "build": "./scripts/build.sh",
"test": "npm run lint && scripts/test.sh", "test": "npm run lint && scripts/test.sh",
"format:js": "prettier --parser typescript --write '**/*.{js,jsx,ts,tsx}'", "format:js": "prettier --parser typescript --write '**/*.{js,jsx,ts,tsx}'",
"format:css": "prettier-stylelint --write --quiet '**/*.{css,scss}'", "format:css": "prettier-stylelint --ignore-path .gitignore --write --quiet '**/*.{css,scss}'",
"format": "npm run format:js && npm run format:css", "format": "npm run format:js && npm run format:css",
"lint:css": "stylelint './**/*.{css,scss}'", "lint:css": "stylelint --ignore-path .gitignore './**/*.{css,scss}'",
"lint:js": "eslint --ignore-path .gitignore --ignore-path .prettierignore --ext .ts,.tsx .", "lint:js": "eslint --ignore-path .gitignore --ignore-path .prettierignore --ext .ts,.tsx .",
"lint:fix": "eslint --fix --ignore-path .gitignore --ignore-path .prettierignore --ext .ts,.tsx .", "lint:fix": "eslint --fix --ignore-path .gitignore --ignore-path .prettierignore --ext .ts,.tsx .",
"lint": "npm run lint:js && npm run lint:css" "lint": "npm run lint:js && npm run lint:css"

0
scripts/test.sh Normal file → Executable file
View File

View File

@ -1,5 +1,7 @@
module.exports = { const config = {
app: { app: {
port: 4000 port: 4000
} }
} }
export default config

View File

@ -8,12 +8,31 @@ import bodyParser from 'body-parser'
import UrlCheckRouter from './routes/UrlCheckRouter' import UrlCheckRouter from './routes/UrlCheckRouter'
// config // config
const config = require('./config/config') import config from './config/config'
// debug // debug
const log = debug('server:index') const log = debug('server:index')
const app = express() const app = express()
function onListening(): void {
log('Server thread started')
}
function onError(error: NodeJS.ErrnoException): void {
if (error.syscall !== 'listen') throw error
switch (error.code) {
case 'EACCES':
log('Required elevated privileges')
process.exit(1)
case 'EADDRINUSE':
log('Port is already in use')
process.exit(1)
default:
throw error
}
}
app.use((req, res, next) => { app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*') res.header('Access-Control-Allow-Origin', '*')
res.header( res.header(
@ -32,33 +51,15 @@ app.use(bodyParser.urlencoded({ extended: false }))
app.use(compression()) app.use(compression())
// routes // routes
app.use('/api/v1/urlcheck', UrlCheckRouter) app.use('/api/v1/urlcheck', UrlCheckRouter)
/// catch 404 /// catch 404
app.use((req, res, next) => { app.use((req, res, next) => {
res.status(404).send() res.status(404).send()
}) })
// listen // listen
const server = app.listen(config.app.port) const server = app.listen(config.app.port)
server.on('listening', onListening) server.on('listening', onListening)
server.on('error', onError) server.on('error', onError)
function onListening(): void {
log('Server thread started')
}
function onError(error: NodeJS.ErrnoException): void {
if (error.syscall !== 'listen') throw error
switch (error.code) {
case 'EACCES':
log('Required elevated privileges')
process.exit(1)
break
case 'EADDRINUSE':
log('Port is already in use')
process.exit(1)
break
default:
throw error
}
}
export default server export default server

View File

@ -2,12 +2,12 @@ import { Router, Request, Response, NextFunction } from 'express'
import request from 'request' import request from 'request'
export class UrlCheckRouter { export class UrlCheckRouter {
router: Router public router: Router
/** /**
* Initialize the UrlCheckRouter * Initialize the UrlCheckRouter
*/ */
constructor() { public constructor() {
this.router = Router() this.router = Router()
} }
@ -25,22 +25,28 @@ export class UrlCheckRouter {
if (response.statusCode.toString().startsWith('2')) { if (response.statusCode.toString().startsWith('2')) {
const result: any = {} const result: any = {}
result.found = true result.found = true
if (response.headers['content-length']) { if (response.headers['content-length']) {
result.contentLength = result.contentLength =
response.headers['content-length'] response.headers['content-length']
} }
if (response.headers['content-type']) { if (response.headers['content-type']) {
const typeAndCharset = response.headers[ const typeAndCharset = response.headers[
'content-type' 'content-type'
].split(';') ].split(';')
result.contentType = typeAndCharset[0]
result.contentType = typeAndCharset[0] // eslint-disable-line prefer-destructuring
if (typeAndCharset[1]) { if (typeAndCharset[1]) {
/* eslint-disable prefer-destructuring */
result.contentCharset = typeAndCharset[1].split( result.contentCharset = typeAndCharset[1].split(
'=' '='
)[1] )[1]
/* eslint-enable prefer-destructuring */
} }
} }
return res.send({ status: 'success', result: result }) return res.send({ status: 'success', result })
} }
return res.send({ status: 'error', message: error }) return res.send({ status: 'error', message: error })
} }
@ -51,7 +57,7 @@ export class UrlCheckRouter {
* Take each handler, and attach to one of the Express.Router's * Take each handler, and attach to one of the Express.Router's
* endpoints. * endpoints.
*/ */
init() { public init() {
this.router.post('/', this.checkUrl) this.router.post('/', this.checkUrl)
} }
} }