Browse Source

options fix.. remove redundant tests

master
Lloyd Brookes 9 years ago
parent
commit
7bb45ab0ae
  1. 7
      lib/local-web-server.js
  2. 2
      package.json
  3. 195
      test/compress/big-file.txt
  4. 22
      test/compress/compress.js
  5. 21
      test/forbid/forbid.js
  6. 1
      test/forbid/one.html
  7. 1
      test/forbid/two.php
  8. 25
      test/log/log.js
  9. 22
      test/mime/mime.js
  10. 1
      test/mime/something.php
  11. 150
      test/mock/mock.js
  12. 1
      test/mock/one.html
  13. 1
      test/proxy/file.txt
  14. 1
      test/proxy/one.html
  15. 73
      test/proxy/rewrite-proxy.js
  16. 1
      test/rewrite/one.html
  17. 19
      test/rewrite/rewrite.js
  18. 18
      test/serve-index/serve-index.js
  19. 1
      test/spa/one.txt
  20. 28
      test/spa/spa.js
  21. 1
      test/spa/two.txt
  22. 1
      test/static/file.txt
  23. 18
      test/static/static.js
  24. 16
      test/test.js

7
lib/local-web-server.js

@ -95,9 +95,11 @@ class LocalWebServer {
} }
/* combine in stored config */ /* combine in stored config */
options = Object.assign({ port: 8000 }, initOptions, stored, options.server, options.middleware, options.misc)
options = Object.assign({ port: 8000 }, initOptions, stored || {}, options.server, options.middleware, options.misc)
this.options = options this.options = options
// console.log(initOptions, stored, options)
if (options.verbose) { if (options.verbose) {
// debug.setLevel(1) // debug.setLevel(1)
} }
@ -176,6 +178,7 @@ class LocalWebServer {
listen () { listen () {
const options = this.options const options = this.options
const server = this._server = this.getServer() const server = this._server = this.getServer()
// console.log(options)
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
server.listen(options.port, () => { server.listen(options.port, () => {
onServerUp(options.port, options['static.root'], server.isHttps) onServerUp(options.port, options['static.root'], server.isHttps)
@ -254,7 +257,7 @@ function loadStack (modulePath) {
} }
function isModule (module) { function isModule (module) {
return module.prototype.middleware || module.prototype.stack
return module.prototype && (module.prototype.middleware || module.prototype.stack)
} }
module.exports = LocalWebServer module.exports = LocalWebServer

2
package.json

@ -28,7 +28,7 @@
"bin", "lib", "ssl" "bin", "lib", "ssl"
], ],
"scripts": { "scripts": {
"test": "tape test/*/*.js",
"test": "tape test/*.js",
"docs": "jsdoc2md -t jsdoc2md/api.hbs -p list lib/*.js > doc/api.md; echo", "docs": "jsdoc2md -t jsdoc2md/api.hbs -p list lib/*.js > doc/api.md; echo",
"cover": "istanbul cover ./node_modules/.bin/tape test/*.js && cat coverage/lcov.info | coveralls && rm -rf coverage; echo" "cover": "istanbul cover ./node_modules/.bin/tape test/*.js && cat coverage/lcov.info | coveralls && rm -rf coverage; echo"
}, },

195
test/compress/big-file.txt

@ -1,195 +0,0 @@
[![view on npm](http://img.shields.io/npm/v/local-web-server.svg)](https://www.npmjs.org/package/local-web-server)
[![npm module downloads per month](http://img.shields.io/npm/dm/local-web-server.svg)](https://www.npmjs.org/package/local-web-server)
[![Dependency Status](https://david-dm.org/75lb/local-web-server.svg)](https://david-dm.org/75lb/local-web-server)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](https://github.com/feross/standard)
# local-web-server
Fires up a simple, CORS-enabled, static web server on a given port. Use for local web development or file sharing (directory browsing enabled).
![local-web-server](http://75lb.github.io/local-web-server/ws.gif)
## Install
Ensure [node.js](http://nodejs.org) is installed first. Linux/Mac users may need to run the following commands with `sudo`.
### Globally
```sh
$ npm install -g local-web-server
```
### Bundled with your project
```sh
$ npm install local-web-server --save-dev
```
Then add an `start` script to your `package.json` (the standard npm approach):
```json
{
"name": "my-web-app",
"version": "1.0.0",
"scripts": {
"start": "ws"
}
}
```
This simplifies a rather specific-looking instruction set like:
```sh
$ npm install
$ npm install -g local-web-server
$ ws
```
to the following, server implementation and launch details abstracted away:
```sh
$ npm install
$ npm start
```
## Usage
```
Usage
$ ws <server options>
$ ws --config
$ ws --help
Server
-p, --port <number> Web server port
-f, --log.format <string> If a format is supplied an access log is written to stdout. If not, a statistics view is displayed. Use a
preset ('none', 'dev','combined', 'short', 'tiny' or 'logstalgia') or supply a custom format (e.g. ':method ->
:url').
-d, --directory <string> Root directory, defaults to the current directory
-c, --compress Enable gzip compression, reduces bandwidth.
-r, --refresh-rate <number> Statistics view refresh rate in ms. Defaults to 500.
Misc
-h, --help Print these usage instructions
--config Print the stored config
```
From the folder you wish to serve, run:
```sh
$ ws
serving at http://localhost:8000
```
If you wish to serve a different directory, run:
```sh
$ ws -d ~/mysite/
serving /Users/Lloyd/mysite at http://localhost:8000
```
If you wish to override the default port (8000), use `--port` or `-p`:
```sh
$ ws --port 9000
serving at http://localhost:9000
```
To add compression, reducing bandwidth, increasing page load time (by 10-15% on my Macbook Air)
```sh
$ ws --compress
```
### Logging
Passing a value to `--log.format` will write an access log to `stdout`.
Either use a built-in [morgan](https://github.com/expressjs/morgan) logger preset:
```sh
$ ws --log.format short
```
Or a custom [morgan](https://github.com/expressjs/morgan) log format:
```sh
$ ws -f ':method -> :url'
```
Or silence:
```sh
$ ws -f none
```
## Storing default options
To store per-project options, saving you the hassle of inputting them everytime, store them in the `local-web-server` property of your project's `package.json`:
```json
{
"name": "my-project",
"version": "0.11.8",
"local-web-server":{
"port": 8100
}
}
```
Or in a `.local-web-server.json` file stored in the directory you want to serve (typically the root folder of your site):
```json
{
"port": 8100,
"log.format": "tiny"
}
```
Or store global defaults in a `.local-web-server.json` file in your home directory.
```json
{
"port": 3000,
"refresh-rate": 1000
}
```
All stored defaults are overriden by options supplied at the command line.
To view your stored defaults, run:
```sh
$ ws --config
```
## mime-types
You can set additional mime-type/extension mappings, or override the defaults by setting a `mime` value in your local config. This value is passed directly to [mime.define()](https://github.com/broofa/node-mime#mimedefine). Example:
```json
{
"mime": {
"text/plain": [ "php", "pl" ]
}
}
```
## Use with Logstalgia
local-web-server is compatible with [logstalgia](http://code.google.com/p/logstalgia/).
### Install Logstalgia
On MacOSX, install with [homebrew](http://brew.sh):
```sh
$ brew install logstalgia
```
Alternatively, [download a release for your system from github](https://github.com/acaudwell/Logstalgia/releases/latest).
Then pipe the `logstalgia` output format directly into logstalgia for real-time visualisation:
```sh
$ ws -f logstalgia | logstalgia -
```
![local-web-server with logstalgia](http://75lb.github.io/local-web-server/logstagia.gif)
## Use with glTail
To use with [glTail](http://www.fudgie.org), write your log to disk using the "default" format:
```sh
$ ws -f default > web.log
```
Then specify this file in your glTail config:
```yaml
servers:
dev:
host: localhost
source: local
files: /Users/Lloyd/Documents/MySite/web.log
parser: apache
color: 0.2, 0.2, 1.0, 1.0
```
* * *
&copy; 2015 Lloyd Brookes <75pound@gmail.com>

22
test/compress/compress.js

@ -1,22 +0,0 @@
'use strict'
const test = require('tape')
const request = require('req-then')
const LocalWebServer = require('../../')
const c = require('../common')
test('compress', function (t) {
t.plan(2)
const ws = new LocalWebServer()
ws.addCompression(true)
ws.addStatic(__dirname)
const server = ws.getServer()
server.listen(8100, () => {
request('http://localhost:8100/big-file.txt', { headers: { 'Accept-Encoding': 'gzip' } })
.then(response => {
t.strictEqual(response.res.statusCode, 200)
t.strictEqual(response.res.headers['content-encoding'], 'gzip')
})
.then(server.close.bind(server))
.catch(c.fail(t))
})
})

21
test/forbid/forbid.js

@ -1,21 +0,0 @@
'use strict'
const test = require('tape')
const request = require('req-then')
const LocalWebServer = require('../../')
const c = require('../common')
test('forbid', function (t) {
t.plan(2)
const ws = new LocalWebServer()
ws.addBlacklist([ '*.php', '*.html' ])
ws.addStatic(__dirname)
const server = ws.getServer()
server.listen(8100, () => {
request('http://localhost:8100/two.php')
.then(c.checkResponse(t, 403))
.then(() => request('http://localhost:8100/one.html'))
.then(c.checkResponse(t, 403))
.then(server.close.bind(server))
.catch(c.fail(t))
})
})

1
test/forbid/one.html

@ -1 +0,0 @@
one

1
test/forbid/two.php

@ -1 +0,0 @@
<?php echo "i'm coding PHP templatez!\n" ?>

25
test/log/log.js

@ -1,25 +0,0 @@
'use strict'
const test = require('tape')
const request = require('req-then')
const LocalWebServer = require('../../')
const c = require('../common')
test('logging', function (t) {
t.plan(2)
const ws = new LocalWebServer()
const stream = require('stream').PassThrough()
stream.on('readable', () => {
let chunk = stream.read()
if (chunk) t.ok(/GET/.test(chunk.toString()))
})
ws.addLogging('common', { stream: stream })
const server = ws.getServer()
server.listen(8100, () => {
request('http://localhost:8100/')
.then(c.checkResponse(t, 404))
.then(server.close.bind(server))
.catch(c.fail(t))
})
})

22
test/mime/mime.js

@ -1,22 +0,0 @@
'use strict'
const test = require('tape')
const request = require('req-then')
const LocalWebServer = require('../../')
const c = require('../common')
test('mime override', function (t) {
t.plan(2)
const ws = new LocalWebServer()
ws.addMimeOverride({ 'text/plain': [ 'php' ] })
ws.addStatic(__dirname)
const server = ws.getServer()
server.listen(8100, () => {
request('http://localhost:8100/something.php')
.then(response => {
t.strictEqual(response.res.statusCode, 200)
t.ok(/text\/plain/.test(response.res.headers['content-type']))
})
.then(server.close.bind(server))
.catch(c.fail(t))
})
})

1
test/mime/something.php

@ -1 +0,0 @@
<?php echo "i'm coding PHP templatez!\n" ?>

150
test/mock/mock.js

@ -1,150 +0,0 @@
'use strict'
const test = require('tape')
const request = require('req-then')
const LocalWebServer = require('../../')
const c = require('../common')
test('mock: simple response', function (t) {
t.plan(2)
const ws = new LocalWebServer()
ws.addMockResponses([
{ route: '/test', response: { body: 'test' } }
])
const server = ws.getServer()
server.listen(8100, () => {
request('http://localhost:8100/test')
.then(c.checkResponse(t, 200, /test/))
.then(server.close.bind(server))
.catch(c.fail(t))
})
})
test('mock: method request filter', function (t) {
t.plan(3)
const ws = new LocalWebServer()
ws.addMockResponses([
{
route: '/test',
request: { method: 'POST' },
response: { body: 'test' }
}
])
const server = ws.getServer()
server.listen(8100, () => {
request('http://localhost:8100/test')
.then(c.checkResponse(t, 404))
.then(() => request('http://localhost:8100/test', { data: 'something' }))
.then(c.checkResponse(t, 200, /test/))
.then(server.close.bind(server))
.catch(c.fail(t))
})
})
test('mock: accepts request filter', function (t) {
t.plan(3)
const ws = new LocalWebServer()
ws.addMockResponses([
{
route: '/test',
request: { accepts: 'text' },
response: { body: 'test' }
}
])
const server = ws.getServer()
server.listen(8100, () => {
request('http://localhost:8100/test', { headers: { Accept: '*/json' } })
.then(c.checkResponse(t, 404))
.then(() => request('http://localhost:8100/test', { headers: { Accept: 'text/plain' } }))
.then(c.checkResponse(t, 200, /test/))
.then(server.close.bind(server))
})
})
test('mock: responses array', function (t) {
t.plan(4)
const ws = new LocalWebServer()
ws.addMockResponses([
{
route: '/test',
responses: [
{ request: { method: 'GET' }, response: { body: 'get' } },
{ request: { method: 'POST' }, response: { body: 'post' } }
]
}
])
const server = ws.getServer()
server.listen(8100, () => {
request('http://localhost:8100/test')
.then(c.checkResponse(t, 200, /get/))
.then(() => request('http://localhost:8100/test', { method: 'POST' }))
.then(c.checkResponse(t, 200, /post/))
.then(server.close.bind(server))
})
})
test('mock: response function', function (t) {
t.plan(4)
const ws = new LocalWebServer()
ws.addMockResponses([
{
route: '/test',
responses: [
{ request: { method: 'GET' }, response: ctx => ctx.body = 'get' },
{ request: { method: 'POST' }, response: ctx => ctx.body = 'post' }
]
}
])
const server = ws.getServer()
server.listen(8100, () => {
request('http://localhost:8100/test')
.then(c.checkResponse(t, 200, /get/))
.then(() => request('http://localhost:8100/test', { method: 'POST' }))
.then(c.checkResponse(t, 200, /post/))
.then(server.close.bind(server))
})
})
test('mock: response function args', function (t) {
t.plan(2)
const ws = new LocalWebServer()
ws.addMockResponses([
{
route: '/test/:one',
responses: [
{ request: { method: 'GET' }, response: (ctx, one) => ctx.body = one }
]
}
])
const server = ws.getServer()
server.listen(8100, () => {
request('http://localhost:8100/test/yeah')
.then(c.checkResponse(t, 200, /yeah/))
.then(server.close.bind(server))
})
})
test('mock: async response function', function (t) {
t.plan(2)
const ws = new LocalWebServer()
ws.addMockResponses([
{
route: '/test',
responses: {
response: function (ctx) {
return new Promise((resolve, reject) => {
setTimeout(() => {
ctx.body = 'test'
resolve()
}, 10)
})
}
}
}
])
const server = ws.getServer()
server.listen(8100, () => {
request('http://localhost:8100/test')
.then(c.checkResponse(t, 200, /test/))
.then(server.close.bind(server))
})
})

1
test/mock/one.html

@ -1 +0,0 @@
one

1
test/proxy/file.txt

@ -1 +0,0 @@
one

1
test/proxy/one.html

@ -1 +0,0 @@
one

73
test/proxy/rewrite-proxy.js

@ -1,73 +0,0 @@
'use strict'
const test = require('tape')
const request = require('req-then')
const LocalWebServer = require('../../')
const http = require('http')
const c = require('../common')
test('rewrite: proxy', function (t) {
t.plan(2)
const ws = new LocalWebServer()
ws.addRewrite([
{ from: '/test/*', to: 'http://registry.npmjs.org/$1' }
])
const server = ws.getServer()
server.listen(8100, () => {
request('http://localhost:8100/test/')
.then(c.checkResponse(t, 200, /db_name/))
.then(server.close.bind(server))
.catch(c.fail(t))
})
})
test('rewrite: proxy, POST', function (t) {
t.plan(1)
const ws = new LocalWebServer()
ws.addRewrite([
{ from: '/test/*', to: 'http://registry.npmjs.org/' }
])
const server = ws.getServer()
server.listen(8100, () => {
request('http://localhost:8100/test/', { data: {} })
.then(c.checkResponse(t, 405))
.then(server.close.bind(server))
.catch(c.fail(t))
})
})
test('rewrite: proxy, two url tokens', function (t) {
t.plan(2)
const ws = new LocalWebServer()
ws.addRewrite([
{ from: '/:package/:version', to: 'http://registry.npmjs.org/:package/:version' }
])
const server = ws.getServer()
server.listen(8100, () => {
request('http://localhost:8100/command-line-args/1.0.0')
.then(c.checkResponse(t, 200, /command-line-args/))
.then(server.close.bind(server))
.catch(c.fail(t))
})
})
test('rewrite: proxy with port', function (t) {
t.plan(2)
const ws1 = new LocalWebServer()
ws1.addStatic(__dirname)
const ws2 = new LocalWebServer()
ws2.addRewrite([
{ from: '/test/*', to: 'http://localhost:9000/$1' }
])
const server1 = ws1.getServer()
const server2 = ws2.getServer()
server1.listen(9000, () => {
server2.listen(8100, () => {
request('http://localhost:8100/test/file.txt')
.then(c.checkResponse(t, 200, /one/))
.then(server1.close.bind(server1))
.then(server2.close.bind(server2))
.catch(c.fail(t))
})
})
})

1
test/rewrite/one.html

@ -1 +0,0 @@
one

19
test/rewrite/rewrite.js

@ -1,19 +0,0 @@
'use strict'
const test = require('tape')
const request = require('req-then')
const LocalWebServer = require('../../')
const c = require('../common')
test('rewrite local', function (t) {
t.plan(2)
const ws = new LocalWebServer()
ws.addRewrite([ { from: '/two.html', to: '/one.html' } ])
ws.addStatic(__dirname)
const server = ws.getServer()
server.listen(8100, () => {
request('http://localhost:8100/two.html')
.then(c.checkResponse(t, 200, /one/))
.then(server.close.bind(server))
.catch(c.fail(t))
})
})

18
test/serve-index/serve-index.js

@ -1,18 +0,0 @@
'use strict'
const test = require('tape')
const request = require('req-then')
const LocalWebServer = require('../../')
const c = require('../common')
test('static', function (t) {
t.plan(3)
const ws = new LocalWebServer()
ws.addIndex(__dirname, { icons: true })
const server = ws.getServer()
server.listen(8100, () => {
request('http://localhost:8100/')
.then(c.checkResponse(t, 200, [ /listing directory/, /class="icon/ ]))
.then(server.close.bind(server))
.catch(c.fail(t))
})
})

1
test/spa/one.txt

@ -1 +0,0 @@
one

28
test/spa/spa.js

@ -1,28 +0,0 @@
'use strict'
const test = require('tape')
const request = require('req-then')
const LocalWebServer = require('../../')
const c = require('../common')
test('spa', function (t) {
t.plan(6)
const ws = new LocalWebServer()
ws.addSpa('one.txt')
ws.addStatic(__dirname)
const server = ws.getServer()
server.listen(8100, () => {
request('http://localhost:8100/asdf', { headers: { accept: 'text/html' } })
.then(c.checkResponse(t, 200, /one/))
/* html requests for missing files with extensions do not redirect to spa */
.then(() => request('http://localhost:8100/asdf.txt', { headers: { accept: 'text/html' } }))
.then(c.checkResponse(t, 404))
/* existing static file */
.then(() => request('http://localhost:8100/two.txt'))
.then(c.checkResponse(t, 200, /two/))
/* not a text/html request - does not redirect to spa */
.then(() => request('http://localhost:8100/asdf', { headers: { accept: 'application/json' } }))
.then(c.checkResponse(t, 404))
.then(server.close.bind(server))
.catch(c.fail(t))
})
})

1
test/spa/two.txt

@ -1 +0,0 @@
two

1
test/static/file.txt

@ -1 +0,0 @@
test

18
test/static/static.js

@ -1,18 +0,0 @@
'use strict'
const test = require('tape')
const request = require('req-then')
const LocalWebServer = require('../../')
const c = require('../common')
test('static', function (t) {
t.plan(2)
const ws = new LocalWebServer()
ws.addStatic(__dirname, { index: 'file.txt' })
const server = ws.getServer()
server.listen(8100, () => {
request('http://localhost:8100/')
.then(c.checkResponse(t, 200, /test/))
.then(server.close.bind(server))
.catch(c.fail(t))
})
})

16
test/test.js

@ -8,15 +8,17 @@ const path = require('path')
test('stack', function (t) { test('stack', function (t) {
t.plan(2) t.plan(2)
const ws = new LocalWebServer({ const ws = new LocalWebServer({
stack: [ path.resolve(__dirname, 'test-middleware.js') ]
stack: [ path.resolve(__dirname, 'test-middleware.js') ],
port: 8100
}) })
const server = ws.getServer()
server.listen(8100, () => {
request('http://localhost:8100/')
ws.listen()
.then(() => {
return request('http://localhost:8100/')
.then(c.checkResponse(t, 200, /1234512345/)) .then(c.checkResponse(t, 200, /1234512345/))
.then(server.close.bind(server))
.then(ws.close.bind(ws))
.catch(c.fail(t)) .catch(c.fail(t))
}) })
.catch(c.fail(t))
}) })
test('https', function (t) { test('https', function (t) {
@ -28,9 +30,9 @@ test('https', function (t) {
}) })
ws.listen() ws.listen()
.then(() => { .then(() => {
request('https://localhost:8100/')
return request('https://localhost:8100/')
.then(c.checkResponse(t, 200, /1234512345/)) .then(c.checkResponse(t, 200, /1234512345/))
.then(ws.close.bind(ws)) .then(ws.close.bind(ws))
.catch(c.fail(t))
}) })
.catch(c.fail(t))
}) })
Loading…
Cancel
Save