This commit is contained in:
Lloyd Brookes
2016-06-20 20:32:32 +01:00
parent ef9bbf8066
commit 22612bbcf4
34 changed files with 59 additions and 88 deletions

View File

@ -0,0 +1,5 @@
{
"forbid": [
"/admin/*", "*.php"
]
}

View File

@ -0,0 +1 @@
<h1>Forbidden page</h1>

View File

@ -0,0 +1 @@
<h1>A permitted page</h1>

View File

@ -0,0 +1,5 @@
<h1>Forbidden routes</h1>
<p>
Notice you can access <a href="allowed.html">this page</a>, but not <a href="admin/blocked.html">this admin page</a> or <a href="something.php">php file</a>.
</p>

View File

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

View File

@ -0,0 +1,5 @@
{
"mime": {
"text/plain": [ "php" ]
}
}

View File

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

View File

@ -0,0 +1,8 @@
{
"mocks": [
{
"route": "/",
"module": "/mocks/delayed.js"
}
]
}

View File

@ -0,0 +1,11 @@
module.exports = {
name: 'delayed response',
response: function (ctx) {
return new Promise((resolve, reject) => {
setTimeout(() => {
ctx.body = '<h1>You waited 2s for this</h1>'
resolve()
}, 2000)
})
}
}

View File

@ -0,0 +1,58 @@
{
"mocks": [
{
"route": "/",
"response": {
"body": "<h1>Welcome to the Mock Responses example</h1>"
}
},
{
"route": "/one",
"response": {
"type": "text/plain",
"body": "<h1>Welcome to the Mock Responses example</h1>"
}
},
{
"route": "/two",
"request": { "accepts": "xml" },
"response": {
"body": "<result id='2' name='whatever' />"
}
},
{
"route": "/three",
"responses": [
{
"request": { "method": "GET" },
"response": {
"body": "<h1>Mock response for 'GET' request on /three</h1>"
}
},
{
"request": { "method": "POST" },
"response": {
"status": 400,
"body": { "message": "That method is not allowed." }
}
}
]
},
{
"route": "/stream",
"module": "/mocks/stream-self.js"
},
{
"route": "/five/:id",
"module": "/mocks/five.js"
},
{
"route": "/users",
"module": "/mocks/users.js"
},
{
"route": "/users/:id",
"module": "/mocks/user.js"
}
]
}

View File

@ -0,0 +1,6 @@
module.exports = {
name: '/five/:id?name=:name',
response: function (ctx, id) {
ctx.body = `<h1>id: ${id}, name: ${ctx.query.name}</h1>`
}
}

View File

@ -0,0 +1,8 @@
const fs = require('fs')
module.exports = {
name: 'stream response',
response: {
body: fs.createReadStream(__filename)
}
}

View File

@ -0,0 +1,41 @@
const users = require('./users.json')
/* responses for /users/:id */
const mockResponses = [
/* don't support POST here */
{ request: { method: 'POST' }, response: { status: 400 } },
/* for GET requests, return a particular user */
{
name: 'GET user',
request: { method: 'GET' },
response: function (ctx, id) {
ctx.body = users.find(user => user.id === Number(id))
}
},
/* for PUT requests, update the record */
{
name: 'PUT user',
request: { method: 'PUT' },
response: function (ctx, id) {
const updatedUser = ctx.request.body
const existingUserIndex = users.findIndex(user => user.id === Number(id))
users.splice(existingUserIndex, 1, updatedUser)
ctx.status = 200
}
},
/* DELETE request: remove the record */
{
name: 'DELETE user',
request: { method: 'DELETE' },
response: function (ctx, id) {
const existingUserIndex = users.findIndex(user => user.id === Number(id))
users.splice(existingUserIndex, 1)
ctx.status = 200
}
}
]
module.exports = mockResponses

View File

@ -0,0 +1,32 @@
const users = require('./users.json')
/* responses for /users */
const mockResponses = [
/* Respond with 400 Bad Request for PUT and DELETE - inappropriate on a collection */
{ request: { method: 'PUT' }, response: { status: 400 } },
{ request: { method: 'DELETE' }, response: { status: 400 } },
{
/* for GET requests return a subset of data, optionally filtered on 'minAge' and 'nationality' */
request: { method: 'GET' },
response: function (ctx) {
ctx.body = users.filter(user => {
const meetsMinAge = (user.age || 1000) >= (Number(ctx.query.minAge) || 0)
const requiredNationality = user.nationality === (ctx.query.nationality || user.nationality)
return meetsMinAge && requiredNationality
})
}
},
{
/* for POST requests, create a new user and return the path to the new resource */
request: { method: 'POST' },
response: function (ctx) {
const newUser = ctx.request.body
users.push(newUser)
newUser.id = users.length
ctx.status = 201
ctx.response.set('Location', `/users/${newUser.id}`)
}
}
]
module.exports = mockResponses

View File

@ -0,0 +1,5 @@
[
{ "id": 1, "name": "Lloyd", "age": 40, "nationality": "English" },
{ "id": 2, "name": "Mona", "age": 34, "nationality": "Palestinian" },
{ "id": 3, "name": "Francesco", "age": 24, "nationality": "Italian" }
]

View File

@ -0,0 +1,8 @@
{
"rewrite": [
{ "from": "/css/*", "to": "/build/styles/$1" },
{ "from": "/npm/*", "to": "http://registry.npmjs.org/$1" },
{ "from": "/broken/*", "to": "http://localhost:9999" },
{ "from": "/:user/repos/:name", "to": "https://api.github.com/repos/:user/:name" }
]
}

View File

@ -0,0 +1,4 @@
body {
font-family: monospace;
font-size: 1.3em;
}

View File

@ -0,0 +1,24 @@
<head>
<link rel="stylesheet" href="css/style.css">
</head>
<h1>Rewriting paths</h1>
<h2>Config</h2>
<pre><code>
{
"rewrite": [
{ "from": "/css/*", "to": "/build/styles/$1" },
{ "from": "/npm/*", "to": "http://registry.npmjs.org/$1" },
{ "from": "/broken/*", "to": "http://localhost:9999" },
{ "from": "/:user/repos/:name", "to": "https://api.github.com/repos/:user/:name" }
]
}
</code></pre>
<h2>Links</h2>
<ul>
<li><a href="/css/style.css">/css/style.css</li>
<li><a href="/npm/local-web-server">/npm/local-web-server</a></li>
<li><a href="/broken/">/broken/</a></li>
<li><a href="/75lb/repos/work">/75lb/repos/work</a></li>
</ul>

View File

@ -0,0 +1,7 @@
body {
background-color: #AA3939;
color: #FFE2E2
}
svg {
fill: #000
}

View File

@ -0,0 +1,10 @@
<head>
<link rel="stylesheet" href="css/style.css">
</head>
<h1>Amazing Page</h1>
<p>
With a freaky triangle..
</p>
<svg width="500" height="500">
<polygon points="250,0 0,500 500,500"></polygon>
</svg>

View File

@ -0,0 +1,7 @@
{
"name": "example",
"version": "1.0.0",
"local-web-server": {
"port": 8100
}
}

View File

@ -0,0 +1,4 @@
{
"spa": "index.html",
"no-cache": true
}

View File

@ -0,0 +1,5 @@
body {
background-color: IndianRed;
}
a { color: black }

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

View File

@ -0,0 +1,14 @@
<head>
<link rel="stylesheet" href="/css/style.css">
</head>
<h1>Single Page App</h1>
<h2>Location: <span></span></h2>
<ul>
<li><a href="/login">/login</a></li>
<li><a href="/search">/search</a></li>
</ul>
<p>Found asset: <img src="image.jpg" /></p>
<p>Missing asset (should 404): <img src="sdafsadf.jpg" /></p>
<script>
document.querySelector('h2 span').textContent = window.location.pathname
</script>