Browse Source

basic test

master
Lloyd Brookes 8 years ago
parent
commit
6b7f7a9536
  1. 5
      lib/local-web-server.js
  2. 6
      package.json
  3. 22
      test/common.js
  4. 1
      test/fixture/one.txt
  5. 12
      test/test-middleware.js
  6. 54
      test/test.js

5
lib/local-web-server.js

@ -9,7 +9,7 @@ const Lws = require('lws')
* @alias module:local-web-server
*/
class LocalWebServer extends Lws {
constructor () {
constructor (options) {
const path = require('path')
const stack = [
'lws-log',
@ -28,7 +28,8 @@ class LocalWebServer extends Lws {
].map(name => {
return path.resolve(__dirname, `../node_modules/${name}`)
})
super({ stack, 'config-name': 'local-web-server' })
options = Object.assign({ stack }, options)
super(options)
}
getVersion () {

6
package.json

@ -22,7 +22,7 @@
"proxy"
],
"engines": {
"node": ">=4.0.0"
"node": ">=7.6"
},
"files": [
"bin",
@ -52,5 +52,7 @@
"lws-static": "^0.1.0",
"lws": "^1.0.0-pre.2"
},
"devDependencies": {}
"devDependencies": {
"test-runner": "^0.3.0"
}
}

22
test/common.js

@ -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

@ -0,0 +1 @@
one

12
test/test-middleware.js

@ -1,12 +0,0 @@
'use strict'
class TestMiddleware {
middleware (option) {
return function (ctx, next) {
ctx.body = '1234512345'
return next()
}
}
}
module.exports = TestMiddleware

54
test/test.js

@ -1,46 +1,20 @@
'use strict'
const test = require('tape')
const TestRunner = require('test-runner')
const request = require('req-then')
const LocalWebServer = require('../')
const c = require('./common')
const path = require('path')
const a = require('assert')
test('stack', function (t) {
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()
})
})
})
const runner = new TestRunner()
test('https', function (t) {
t.plan(2)
const ws = new LocalWebServer({
stack: [ path.resolve(__dirname, 'test-middleware.js') ],
https: true,
port: 8100,
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()
})
})
runner.test('basic', async function () {
const port = 9000 + this.index
const localWebServer = new LocalWebServer({
port: port,
'static.root': 'test/fixture',
'log.format': 'none'
})
localWebServer.start()
const response = await request(`http://localhost:${port}/one.txt`)
localWebServer.server.close()
a.strictEqual(response.data.toString(), 'one\n')
})
Loading…
Cancel
Save