basic test
This commit is contained in:
@ -9,7 +9,7 @@ const Lws = require('lws')
|
|||||||
* @alias module:local-web-server
|
* @alias module:local-web-server
|
||||||
*/
|
*/
|
||||||
class LocalWebServer extends Lws {
|
class LocalWebServer extends Lws {
|
||||||
constructor () {
|
constructor (options) {
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const stack = [
|
const stack = [
|
||||||
'lws-log',
|
'lws-log',
|
||||||
@ -28,7 +28,8 @@ class LocalWebServer extends Lws {
|
|||||||
].map(name => {
|
].map(name => {
|
||||||
return path.resolve(__dirname, `../node_modules/${name}`)
|
return path.resolve(__dirname, `../node_modules/${name}`)
|
||||||
})
|
})
|
||||||
super({ stack, 'config-name': 'local-web-server' })
|
options = Object.assign({ stack }, options)
|
||||||
|
super(options)
|
||||||
}
|
}
|
||||||
|
|
||||||
getVersion () {
|
getVersion () {
|
||||||
|
@ -22,7 +22,7 @@
|
|||||||
"proxy"
|
"proxy"
|
||||||
],
|
],
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=4.0.0"
|
"node": ">=7.6"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"bin",
|
"bin",
|
||||||
@ -52,5 +52,7 @@
|
|||||||
"lws-static": "^0.1.0",
|
"lws-static": "^0.1.0",
|
||||||
"lws": "^1.0.0-pre.2"
|
"lws": "^1.0.0-pre.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {}
|
"devDependencies": {
|
||||||
|
"test-runner": "^0.3.0"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,22 +0,0 @@
|
|||||||
'use strict'
|
|
||||||
const arrayify = require('array-back')
|
|
||||||
|
|
||||||
exports.checkResponse = checkResponse
|
|
||||||
exports.fail = fail
|
|
||||||
|
|
||||||
function checkResponse (t, status, bodyTests) {
|
|
||||||
return function (response) {
|
|
||||||
if (status) t.strictEqual(response.res.statusCode, status)
|
|
||||||
if (bodyTests) {
|
|
||||||
arrayify(bodyTests).forEach(body => {
|
|
||||||
t.ok(body.test(response.data), 'correct data')
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function fail (t) {
|
|
||||||
return function (err) {
|
|
||||||
t.fail('failed: ' + err.stack)
|
|
||||||
}
|
|
||||||
}
|
|
1
test/fixture/one.txt
Normal file
1
test/fixture/one.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
one
|
@ -1,12 +0,0 @@
|
|||||||
'use strict'
|
|
||||||
|
|
||||||
class TestMiddleware {
|
|
||||||
middleware (option) {
|
|
||||||
return function (ctx, next) {
|
|
||||||
ctx.body = '1234512345'
|
|
||||||
return next()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = TestMiddleware
|
|
52
test/test.js
52
test/test.js
@ -1,46 +1,20 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
const test = require('tape')
|
const TestRunner = require('test-runner')
|
||||||
const request = require('req-then')
|
const request = require('req-then')
|
||||||
const LocalWebServer = require('../')
|
const LocalWebServer = require('../')
|
||||||
const c = require('./common')
|
const a = require('assert')
|
||||||
const path = require('path')
|
|
||||||
|
|
||||||
test('stack', function (t) {
|
const runner = new TestRunner()
|
||||||
t.plan(2)
|
|
||||||
const ws = new LocalWebServer({
|
|
||||||
stack: [ path.resolve(__dirname, 'test-middleware.js') ],
|
|
||||||
port: 8100,
|
|
||||||
testMode: true
|
|
||||||
})
|
|
||||||
ws.server.on('listening', () => {
|
|
||||||
return request('http://localhost:8100/')
|
|
||||||
.then(c.checkResponse(t, 200, /1234512345/))
|
|
||||||
.then(ws.server.close.bind(ws.server))
|
|
||||||
.catch(err => {
|
|
||||||
t.fail(err.message)
|
|
||||||
ws.server.close()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
test('https', function (t) {
|
runner.test('basic', async function () {
|
||||||
t.plan(2)
|
const port = 9000 + this.index
|
||||||
const ws = new LocalWebServer({
|
const localWebServer = new LocalWebServer({
|
||||||
stack: [ path.resolve(__dirname, 'test-middleware.js') ],
|
port: port,
|
||||||
https: true,
|
'static.root': 'test/fixture',
|
||||||
port: 8100,
|
'log.format': 'none'
|
||||||
testMode: true
|
|
||||||
})
|
|
||||||
const url = require('url')
|
|
||||||
const reqOptions = url.parse('https://localhost:8100/')
|
|
||||||
reqOptions.rejectUnauthorized = false
|
|
||||||
ws.server.on('listening', () => {
|
|
||||||
return request(reqOptions)
|
|
||||||
.then(c.checkResponse(t, 200, /1234512345/))
|
|
||||||
.then(ws.server.close.bind(ws.server))
|
|
||||||
.catch(err => {
|
|
||||||
t.fail(err.message)
|
|
||||||
ws.server.close()
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
localWebServer.start()
|
||||||
|
const response = await request(`http://localhost:${port}/one.txt`)
|
||||||
|
localWebServer.server.close()
|
||||||
|
a.strictEqual(response.data.toString(), 'one\n')
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user