@ -56,6 +56,8 @@ local-web-server is a simple command-line tool. To use it, from your project dir
-c, --compress Serve gzip-compressed resources, where applicable.
-c, --compress Serve gzip-compressed resources, where applicable.
-b, --forbid path ... A list of forbidden routes.
-b, --forbid path ... A list of forbidden routes.
-n, --no-cache Disable etag-based caching -forces loading from disk each request.
-n, --no-cache Disable etag-based caching -forces loading from disk each request.
--key file SSL key, required for https.
--cert file SSL cert, required for https.
--verbose Verbose output, useful for debugging.
--verbose Verbose output, useful for debugging.
<strong>Misc</strong>
<strong>Misc</strong>
@ -174,7 +176,9 @@ Under the hood, the property values from the `response` object are written onto
}
}
```
```
To define a **conditional response**, set a `request` object on the mock definition. The `request` value acts as a query - the response defined will only be returned if each property of the `request` query matches. For example, return an XML response *only* if the request headers include `accept: application/xml`, else return 404 Not Found.
#### Conditional Response
To define a conditional response, set a `request` object on the mock definition. The `request` value acts as a query - the response defined will only be returned if each property of the `request` query matches. For example, return an XML response *only* if the request headers include `accept: application/xml`, else return 404 Not Found.
```json
```json
{
{
@ -190,7 +194,9 @@ To define a **conditional response**, set a `request` object on the mock definit
}
}
```
```
To specify **multiple potential responses**, set an array of mock definitions to the `responses` property. The first response with a matching request query will be sent. In this example, the client will get one of two responses depending on the request method:
#### Multiple Potential Responses
To specify multiple potential responses, set an array of mock definitions to the `responses` property. The first response with a matching request query will be sent. In this example, the client will get one of two responses depending on the request method:
```json
```json
{
{
@ -217,7 +223,9 @@ To specify **multiple potential responses**, set an array of mock definitions to
}
}
```
```
The examples above all returned static data. To define a **dynamic response**, create a mock module. Specify its path in the `module` property:
#### Dynamic Response
The examples above all returned static data. To define a dynamic response, create a mock module. Specify its path in the `module` property:
```json
```json
{
{
"mocks": [
"mocks": [
@ -240,6 +248,8 @@ module.exports = {
}
}
```
```
#### Response function
For more power, define the response as a function. It will receive the [koa context](https://github.com/koajs/koa/blob/master/docs/api/context.md) as its first argument. Now you have full programmatic control over the response returned.
For more power, define the response as a function. It will receive the [koa context](https://github.com/koajs/koa/blob/master/docs/api/context.md) as its first argument. Now you have full programmatic control over the response returned.
```js
```js
module.exports = {
module.exports = {
@ -270,16 +280,107 @@ module.exports = {
}
}
```
```
Here's an example of a REST collection (users). The config:
#### RESTful Resource example
Here's an example of a REST collection (users). We'll create two routes, one for actions on the resource collection, one for individual resource actions.
@ -56,6 +56,8 @@ local-web-server is a simple command-line tool. To use it, from your project dir
-c, --compress Serve gzip-compressed resources, where applicable.
-c, --compress Serve gzip-compressed resources, where applicable.
-b, --forbid path ... A list of forbidden routes.
-b, --forbid path ... A list of forbidden routes.
-n, --no-cache Disable etag-based caching -forces loading from disk each request.
-n, --no-cache Disable etag-based caching -forces loading from disk each request.
--key file SSL key, required for https.
--cert file SSL cert, required for https.
--verbose Verbose output, useful for debugging.
--verbose Verbose output, useful for debugging.
<strong>Misc</strong>
<strong>Misc</strong>
@ -174,7 +176,9 @@ Under the hood, the property values from the `response` object are written onto
}
}
```
```
To define a **conditional response**, set a `request` object on the mock definition. The `request` value acts as a query - the response defined will only be returned if each property of the `request` query matches. For example, return an XML response *only* if the request headers include `accept: application/xml`, else return 404 Not Found.
#### Conditional Response
To define a conditional response, set a `request` object on the mock definition. The `request` value acts as a query - the response defined will only be returned if each property of the `request` query matches. For example, return an XML response *only* if the request headers include `accept: application/xml`, else return 404 Not Found.
```json
```json
{
{
@ -190,7 +194,9 @@ To define a **conditional response**, set a `request` object on the mock definit
}
}
```
```
To specify **multiple potential responses**, set an array of mock definitions to the `responses` property. The first response with a matching request query will be sent. In this example, the client will get one of two responses depending on the request method:
#### Multiple Potential Responses
To specify multiple potential responses, set an array of mock definitions to the `responses` property. The first response with a matching request query will be sent. In this example, the client will get one of two responses depending on the request method:
```json
```json
{
{
@ -217,7 +223,9 @@ To specify **multiple potential responses**, set an array of mock definitions to
}
}
```
```
The examples above all returned static data. To define a **dynamic response**, create a mock module. Specify its path in the `module` property:
#### Dynamic Response
The examples above all returned static data. To define a dynamic response, create a mock module. Specify its path in the `module` property:
```json
```json
{
{
"mocks": [
"mocks": [
@ -240,6 +248,8 @@ module.exports = {
}
}
```
```
#### Response function
For more power, define the response as a function. It will receive the [koa context](https://github.com/koajs/koa/blob/master/docs/api/context.md) as its first argument. Now you have full programmatic control over the response returned.
For more power, define the response as a function. It will receive the [koa context](https://github.com/koajs/koa/blob/master/docs/api/context.md) as its first argument. Now you have full programmatic control over the response returned.
```js
```js
module.exports = {
module.exports = {
@ -270,16 +280,107 @@ module.exports = {
}
}
```
```
Here's an example of a REST collection (users). The config:
#### RESTful Resource example
Here's an example of a REST collection (users). We'll create two routes, one for actions on the resource collection, one for individual resource actions.