Flaska: Testing and Version 1.0.0 released
All checks were successful
continuous-integration/appveyor/branch AppVeyor build succeeded

Added JsonHandler
Added FormidableHandler
Fixed a bunch of request handling
This commit is contained in:
Jonatan Nilsson 2022-03-17 15:09:22 +00:00
parent 9c43b429c4
commit 28b4f6ea33
11 changed files with 1098 additions and 65 deletions

180
README.md
View file

@ -1,2 +1,178 @@
# bottle-node
Bottle is a micro web-framework for node. It is designed to be fast, simple and lightweight, and is distributed as a single file module with no dependencies.
# Flaska
Flaska is a micro web-framework for node. It is designed to be fast, simple and lightweight, and is distributed as a single file module with no dependencies.
Heavily inspired by koa and koa-router it takes liberties of implementing most of the common functionality required for most sites without sacrificing anything. And the fact that the footprint for this is much smaller than koa while also being more powerful and faster shows the testament that is flaska.
## Installation
```
$ npm install flaska
```
## Hello Flaska
```js
import { Flaska } from '../flaska.mjs'
const flaska = new Flaska()
flaska.get('/', function(ctx) {
ctx.body = 'Hello Flaska';
})
// flaska.listen(3000);
flaska.listenAsync(3000).then(function() {
console.log('listening on port 3000')
}, function(err) {
console.error('Error listening:', err)
})
```
## Router/Handlers
Flaska supports all the common verbs out of the box:
* `flaska.get(url, [middlewares], handler)`
* `flaska.post(url, [middlewares], handler)`
* `flaska.put(url, [middlewares], handler)`
* `flaska.delete(url, [middlewares], handler)`
* `flaska.options(url, [middlewares], handler)`
* `flaska.patch(url, [middlewares], handler)`
Example:
```
flaska.get('/path/to/url', async function(ctx) {
// Perform action
})
```
In addition, each route can have none, one or many middlewares:
```
flaska.get('/handlequery', QueryHandler(), async function(ctx) {
// Perform action
})
flaska.get('/query/and/json', [QueryHandler(), JsonHandler()], async function(ctx) {
// Perform action
})
```
You can also run global middlewares at the start or end of every request:
```
import { Flaska, QueryHandler, JsonHandler } from '../flaska.mjs'
const flaska = new Flaska()
flaska.before(QueryHandler())
flaska.before(JsonHandler())
flaska.beforeAsync(MyLoadDatabase())
flaska.after(function(ctx) {
ctx.log.info('Request finished.')
})
```
## Context, Request and Response
Each handler receives a Flaska `Context` object that encapsulates an incoming
http message and the corresponding response to that message. For those familiar
with koa will be famililar with this concept. `ctx` is often used as the parameter
name for the context object.
```js
app.before(function(ctx) {
ctx.log.info('Got request')
})
app.beforeAsync(async function(ctx) {
// Do some database fetching maybe
})
```
The context `ctx` that gets generated includes the following properties for the incoming request:
* `log`: Log handler.
* `req`: The Request's [IncomingMessage](https://nodejs.org/api/http.html#http_class_http_incomingmessage).
* `res`: The Response's [ServerResponse](https://nodejs.org/api/http.html#http_class_http_serverresponse).
* `method`: The HTTP method ('GET', 'POST', etc.).
* `url`: The full URL path.
* `search`: The URL search.
* `state`: Anonymous object for user-defined data.
* `query`: Map containing the key/value for the url search.
The following context specific variables can be specified by you the user to instruct Flaska on what to return in response:
* `status`: The status code to return in response (default 200).
* `body`: The body contents to return in response (default null).
* `type`: The content-type for the header (default null).
* `length`: The length of the body in bytes (default null).
More on this next.
## `ctx.body`
At the end of each request, flaska will read `ctx.body` and determine the best course of action based on the type of content is being sent.
### Javascript object
In cases where the response body is a normal object, Flaska will automatically `JSON.stringify` it,
set the `Content-Type` to `application/json` and set the total length in `Content-Length`. This
is provided for you at no expense on your behalf so you don't have to worry about it:
```
flaska.get('/api/test', function(ctx) {
ctx.body = {
message: 'Hello world',
}
})
```
### File stream/pipe
In cases where the response body is a pipe object (detected from the existance of `.pipe` property), flaska will automatically pipe the file for you. In addition, if a file stream is used, it will read the extension of the file being streamed and automatically fill in the mime-type for you in the `Content-Type` header.
```
flaska.get('/test.png', function(ctx) {
ctx.body = fs.createReadStream('./test/test.png')
})
```
Flaska will automatically close the file stream for you so you don't have to worry about that.
### String
In other instances, Flaska will `.toString()` the body and send it in response with the specified type or default to `text/plain` if unspecified.
```
flaska.get('/file.html', function(ctx) {
ctx.body = `
<!doctype html>
<html lang="en">
<body>
<h1>Hello world</h1>
</body>
</html>
`
ctx.type = 'text/html; charset=utf-8'
})
```
The `Context` object also provides shortcuts for methods on its `request` and `response`. In the prior
examples, `ctx.type` can be used instead of `ctx.response.type` and `ctx.accepts` can be used
instead of `ctx.request.accepts`.
## Built-in middlewares and handlers
Flaska comes with a few middlewares out of the box.
* `QueryHandler()`
Parse the search query and create a map with key->value in `ctx.query`.
* `JsonHandler()`
Parse incoming request body as json and store it in `ctx.req.body`.
* `FormidableHandler()`
Provides a wrapper to handle an incoming file upload using `Formidable@1`.

View file

@ -52,12 +52,12 @@ on_success:
echo "Release already exists, nothing to do.";
else
echo "Creating release on gitea"
RELEASE_RESULT=$(curl \
curl \
-X POST \
-H "Authorization: token $deploytoken" \
-H "Content-Type: application/json" \
https://git.nfp.is/api/v1/repos/$APPVEYOR_REPO_NAME/releases \
-d "{\"tag_name\":\"v${CURR_VER}\",\"name\":\"v${CURR_VER}\",\"body\":\"Automatic release from Appveyor from ${APPVEYOR_REPO_COMMIT} :\n\n${APPVEYOR_REPO_COMMIT_MESSAGE}\"}")
-d "{\"tag_name\":\"v${CURR_VER}\",\"name\":\"v${CURR_VER}\",\"body\":\"Automatic release from Appveyor from ${APPVEYOR_REPO_COMMIT} :\n\n${APPVEYOR_REPO_COMMIT_MESSAGE}\"}"
echo '//registry.npmjs.org/:_authToken=${npmtoken}' > ~/.npmrc
echo "Publishing new version to npm"
npm publish

File diff suppressed because one or more lines are too long

View file

@ -1,6 +1,6 @@
{
"name": "flaska",
"version": "0.9.9",
"version": "1.0.0",
"description": "Flaska is a micro web-framework for node. It is designed to be fast, simple and lightweight, and is distributed as a single file module with no dependencies.",
"main": "flaska.mjs",
"scripts": {
@ -21,7 +21,7 @@
"type": "module",
"repository": {
"type": "git",
"url": "git+https://github.com/nfp-projects/bottle-node.git"
"url": "git+https://git.nfp.is/TheThing/flaska.git"
},
"keywords": [
"web",
@ -35,11 +35,11 @@
"author": "Jonatan Nilsson",
"license": "WTFPL",
"bugs": {
"url": "https://github.com/nfp-projects/bottle-node/issues"
"url": "https://git.nfp.is/TheThing/flaska/issues"
},
"homepage": "https://github.com/nfp-projects/bottle-node#readme",
"homepage": "https://git.nfp.is/TheThing/flaska/#readme",
"devDependencies": {
"eltro": "^1.3.1",
"eltro": "^1.2.3",
"formidable": "^1.2.2"
},
"files": [

View file

@ -1,4 +1,7 @@
import fs from 'fs/promises'
import path from 'path'
import http from 'http'
import stream from 'stream'
import { URL } from 'url'
import { defaults } from './helper.mjs'
@ -7,7 +10,7 @@ export default function Client(port, opts) {
this.prefix = `http://localhost:${port}`
}
Client.prototype.customRequest = function(method = 'GET', path, body, options) {
Client.prototype.customRequest = function(method = 'GET', path, body, options = {}) {
if (path.slice(0, 4) !== 'http') {
path = this.prefix + path
}
@ -27,21 +30,30 @@ Client.prototype.customRequest = function(method = 'GET', path, body, options) {
}))
const req = http.request(opts)
if (body) {
req.write(body)
}
req.on('error', reject)
req.on('error', (err) => {
reject(err)
})
req.on('timeout', function() {
req.destroy()
reject(new Error(`Request ${method} ${path} timed out`))
})
req.on('response', res => {
res.setEncoding('utf8')
let output = ''
if (options.toPipe) {
return stream.pipeline(res, options.toPipe, function(err) {
if (err) {
return reject(err)
}
resolve()
})
} else {
res.setEncoding('utf8')
res.on('data', function (chunk) {
output += chunk.toString()
})
res.on('data', function (chunk) {
output += chunk.toString()
})
}
res.on('end', function () {
if (!output) return resolve(null)
@ -58,6 +70,14 @@ Client.prototype.customRequest = function(method = 'GET', path, body, options) {
resolve(output)
})
})
if (opts.returnRequest) {
return resolve(req)
}
if (body) {
req.write(body)
}
req.end()
return req
})

View file

@ -1,5 +1,5 @@
import { Eltro as t, assert, stub, spy } from 'eltro'
import { Flaska } from '../flaska.mjs'
import { Flaska, HttpError } from '../flaska.mjs'
import { createCtx, fakeHttp } from './helper.mjs'
const faker = fakeHttp()
@ -135,6 +135,68 @@ t.describe('_onerror', function() {
message: 'Internal Server Error',
})
})
t.test('default valid handling of HttpError', function() {
const assertStatus = 431
const assertBody = { a: 1 }
const assertError = new HttpError(assertStatus, 'should not be seen', assertBody)
let ctx = createCtx()
let flaska = new Flaska({}, faker)
flaska._onerror(assertError, ctx)
assert.strictEqual(ctx.log.error.callCount, 1)
assert.strictEqual(ctx.log.error.firstCall[0], assertError)
assert.strictEqual(ctx.status, assertStatus)
assert.strictEqual(ctx.body, assertBody)
})
t.test('default valid handling of HttpError with no body', function() {
const assertStatus = 413
const assertError = new HttpError(assertStatus, 'should not be seen')
let ctx = createCtx()
let flaska = new Flaska({}, faker)
flaska._onerror(assertError, ctx)
assert.strictEqual(ctx.log.error.callCount, 1)
assert.strictEqual(ctx.log.error.firstCall[0], assertError)
assert.strictEqual(ctx.status, assertStatus)
assert.deepStrictEqual(ctx.body, {
status: assertStatus,
message: 'Payload Too Large',
})
})
t.test('default valid handling of HttpError with missing status message', function() {
const assertStatus = 432
const assertError = new HttpError(assertStatus, 'should not be seen')
let ctx = createCtx()
let flaska = new Flaska({}, faker)
flaska._onerror(assertError, ctx)
assert.strictEqual(ctx.log.error.callCount, 1)
assert.strictEqual(ctx.log.error.firstCall[0], assertError)
assert.strictEqual(ctx.status, assertStatus)
assert.deepStrictEqual(ctx.body, {
status: assertStatus,
message: 'Internal Server Error',
})
})
})
t.describe('_onreqerror', function() {
t.test('a valid function', function() {
let flaska = new Flaska({}, faker)
assert.strictEqual(typeof(flaska._onreqerror), 'function')
})
t.test('default valid handling of aborted', function() {
const assertError = new Error('aborted')
let flaska = new Flaska({}, faker)
let ctx = createCtx()
flaska._onreqerror(assertError, ctx)
assert.strictEqual(ctx.log.info.callCount, 1)
assert.strictEqual(ctx.log.info.firstCall[0], assertError)
})
})
t.describe('#devMode()', function() {
@ -180,6 +242,54 @@ t.describe('#devMode()', function() {
assert.match(ctx.body.message, new RegExp(assertErrorMessage))
assert.ok(ctx.body.stack)
})
t.test('default valid handling of HttpError', function() {
const assertStatus = 431
const assertBody = { a: 1 }
const assertError = new HttpError(assertStatus, 'should not be seen', assertBody)
let ctx = createCtx()
let flaska = new Flaska({}, faker)
flaska.devMode()
flaska._onerror(assertError, ctx)
assert.strictEqual(ctx.log.error.callCount, 1)
assert.strictEqual(ctx.log.error.firstCall[0], assertError)
assert.strictEqual(ctx.status, assertStatus)
assert.strictEqual(ctx.body, assertBody)
})
t.test('default valid handling of HttpError with no body', function() {
const assertStatus = 413
const assertErrorMessage = 'A day'
const assertError = new HttpError(assertStatus, assertErrorMessage)
let ctx = createCtx()
let flaska = new Flaska({}, faker)
flaska.devMode()
flaska._onerror(assertError, ctx)
assert.strictEqual(ctx.log.error.callCount, 1)
assert.strictEqual(ctx.log.error.firstCall[0], assertError)
assert.strictEqual(ctx.status, assertStatus)
assert.strictEqual(ctx.body.status, assertStatus)
assert.match(ctx.body.message, /Payload Too Large/)
assert.match(ctx.body.message, new RegExp(assertErrorMessage))
})
t.test('default valid handling of HttpError with missing status message', function() {
const assertStatus = 432
const assertErrorMessage = 'Jet Coaster Ride'
const assertError = new HttpError(assertStatus, assertErrorMessage)
let ctx = createCtx()
let flaska = new Flaska({}, faker)
flaska.devMode()
flaska._onerror(assertError, ctx)
assert.strictEqual(ctx.log.error.callCount, 1)
assert.strictEqual(ctx.log.error.firstCall[0], assertError)
assert.strictEqual(ctx.status, assertStatus)
assert.strictEqual(ctx.body.status, assertStatus)
assert.match(ctx.body.message, /Internal Server Error/)
})
})
t.describe('#before()', function() {

View file

@ -47,11 +47,8 @@ t.describe('#requestStart()', function() {
assert.strictEqual(onReqError.firstCall[0], assertErrorOne)
assert.strictEqual(onReqError.firstCall[1], ctx)
assert.strictEqual(assertReq.on.secondCall[0], 'aborted')
assert.strictEqual(assertReq.on.secondCall[0], 'close')
assert.strictEqual(typeof(assertReq.on.secondCall[1]), 'function')
assert.notStrictEqual(ctx.aborted, false)
assertReq.on.secondCall[1]()
assert.strictEqual(ctx.aborted, true)
// Test abort and close

View file

@ -1,4 +1,4 @@
import { Eltro as t, spy, assert} from 'eltro'
import { Eltro as t, spy, assert, stub} from 'eltro'
import { Flaska, FlaskaRouter } from '../flaska.mjs'
import { fakeHttp, createCtx } from './helper.mjs'
@ -219,6 +219,34 @@ t.describe('#requestEnd()', function() {
let flaska = new Flaska({}, fakerHttp, fakeStream)
flaska.requestEnd(null, ctx)
})
let tests = [
['png', 'image/png'],
['jpg', 'image/jpeg'],
['bmp', 'image/bmp'],
['js', 'text/javascript'],
]
tests.forEach(function(test) {
t.test(`call pipe with file extension ${test[0]} should mimetype ${test[1]}`, function(cb) {
let onFinish = function(source, target) {
try {
assert.strictEqual(ctx.res.statusCode, 200)
assert.strictEqual(ctx.res.setHeader.firstCall[0], 'Content-Type')
assert.strictEqual(ctx.res.setHeader.firstCall[1], test[1])
assert.strictEqual(source, ctx.body)
assert.strictEqual(target, ctx.res)
cb()
} catch (err) { cb(err) }
}
const ctx = createCtx({})
ctx.body = { pipe: function() {}, path: './bla/test/temp.' + test[0] }
fakeStream.pipeline = onFinish
let flaska = new Flaska({}, fakerHttp, fakeStream)
flaska.requestEnd(null, ctx)
})
})
t.test('should return immediately if req is finished', function() {
let onFinish = function() {
@ -260,6 +288,38 @@ t.describe('#requestEnd()', function() {
})
t.describe('#requestEnded()', function() {
t.test('calls destroy if body is pipe and is not closed', function() {
const ctx = createCtx({
body: {
pipe: {},
closed: false,
destroy: spy(),
},
})
let flaska = new Flaska({}, fakerHttp)
flaska._afterCompiled = function() {}
flaska.requestEnded(ctx)
assert.ok(ctx.body.destroy.called)
})
t.test('not call destroy if body is pipe but is closed', function() {
const ctx = createCtx({
body: {
pipe: {},
closed: true,
destroy: spy(),
}
})
let flaska = new Flaska({}, fakerHttp)
flaska._afterCompiled = function() {}
flaska.requestEnded(ctx)
assert.notOk(ctx.body.destroy.called)
})
t.test('calls afterCompiled correctly', function() {
const assertError = new Error('test')
const assertCtx = createCtx()

View file

@ -1,20 +1,64 @@
import { Eltro as t, assert} from 'eltro'
import { Flaska } from '../flaska.mjs'
import fsSync from 'fs'
import fs from 'fs/promises'
import formidable from 'formidable'
import { Eltro as t, assert, stub } from 'eltro'
import { setTimeout } from 'timers/promises'
import { Flaska, JsonHandler, FormidableHandler } from '../flaska.mjs'
import Client from './client.mjs'
const port = 51024
const flaska = new Flaska({})
const log = {
fatal: stub(),
error: stub(),
warn: stub(),
info: stub(),
debug: stub(),
trace: stub(),
log: stub(),
}
const flaska = new Flaska({ log })
const client = new Client(port)
let reqBody = null
let file = null
let uploaded = []
flaska.get('/', function(ctx) {
ctx.body = { status: true }
})
flaska.post('/test', function(ctx) {
flaska.post('/json', JsonHandler(), function(ctx) {
ctx.body = { success: true }
// console.log(ctx)
reqBody = ctx.req.body
})
flaska.get('/timeout', function(ctx) {
return new Promise(function() {})
})
flaska.get('/file', function(ctx) {
file = fsSync.createReadStream('./test/test.png')
ctx.body = file
})
flaska.post('/file/upload', FormidableHandler(formidable, {
uploadDir: './test/upload'
}), function(ctx) {
uploaded.push(ctx.req.file)
ctx.body = ctx.req.file
})
flaska.get('/file/leak', function(ctx) {
file = fsSync.createReadStream('./test/test.png')
ctx.body = file
return new Promise(function() {})
})
function reset() {
log.fatal.reset()
log.error.reset()
log.warn.reset()
log.info.reset()
log.debug.reset()
log.trace.reset()
log.log.reset()
}
t.before(function() {
return flaska.listenAsync(port)
@ -28,14 +72,152 @@ t.describe('/', function() {
})
})
t.describe('/test', function() {
t.describe('/json', function() {
t.test('should return success and store body', async function() {
const assertBody = { a: '' }
for (let i = 0; i < 1010; i++) {
assertBody.a += 'aaaaaaaaaa'
}
reqBody = null
let body = await client.post('/test')
let body = await client.post('/json', assertBody)
assert.deepEqual(body, { success: true })
assert.deepStrictEqual(reqBody, assertBody)
})
t.test('should fail if body is too big', async function() {
reset()
const assertBody = { a: '' }
for (let i = 0; i < 10300; i++) {
assertBody.a += 'aaaaaaaaaa'
}
let err = await assert.isRejected(client.post('/json', assertBody))
assert.strictEqual(err.body.status, 413)
assert.ok(log.error.called)
assert.match(log.error.firstCall[0].message, /10240/)
assert.strictEqual(log.error.firstCall[0].status, 413)
})
t.test('should fail if not a valid json', async function() {
reset()
let err = await assert.isRejected(client.customRequest('POST', '/json', 'aaaa'))
assert.strictEqual(err.body.status, 400)
assert.match(err.body.message, /invalid json/i)
assert.match(err.body.message, /token a/i)
assert.strictEqual(err.body.request, 'aaaa')
assert.strictEqual(log.error.callCount, 1)
assert.match(log.error.firstCall[0].message, /invalid json/i)
assert.match(log.error.firstCall[0].message, /token a/i)
})
t.test('should handle incomplete requests correctly', async function() {
reset()
let req = await client.customRequest('POST', '/json', 'aaaa', { returnRequest: true })
req.write('part1')
await setTimeout(20)
req.destroy()
await setTimeout(20)
assert.strictEqual(log.error.callCount, 0)
assert.strictEqual(log.info.callCount, 1)
assert.strictEqual(log.info.firstCall[0].message, 'aborted')
})
})
t.describe('/timeout', function() {
t.test('server should handle timeout', async function() {
reset()
let err = await assert.isRejected(client.customRequest('GET', '/timeout', JSON.stringify({}), { timeout: 20 }))
await setTimeout(20)
assert.match(err.message, /timed out/)
assert.notOk(log.error.called)
assert.ok(log.info.called)
assert.strictEqual(log.info.firstCall[0].message, 'aborted')
})
})
t.describe('/file', function() {
t.test('server should pipe', async function() {
log.error.reset()
let target = fsSync.createWriteStream('./test_tmp.png')
await client.customRequest('GET', '/file', null, { toPipe: target })
await setTimeout(20)
assert.ok(target.closed)
assert.ok(file.closed)
let [statSource, statTarget] = await Promise.all([
fs.stat('./test/test.png'),
fs.stat('./test_tmp.png'),
])
assert.strictEqual(statSource.size, statTarget.size)
})
t.test('server should autoclose body file handles on errors', async function() {
reset()
file = null
let req = await client.customRequest('GET', '/file/leak', null, { returnRequest: true })
req.end()
while (!file) {
await setTimeout(10)
}
assert.ok(file)
assert.notOk(file.closed)
req.destroy()
await setTimeout(20)
while (!file.closed) {
await setTimeout(10)
}
assert.strictEqual(log.error.callCount, 0)
assert.strictEqual(log.info.callCount, 1)
assert.strictEqual(log.info.firstCall[0].message, 'aborted')
assert.ok(file.closed)
})
})
t.describe('/file/upload', function() {
t.test('server should upload file', async function() {
let res = await client.upload('/file/upload', './test/test.png')
let [statSource, statTarget] = await Promise.all([
fs.stat('./test/test.png'),
fs.stat(res.path),
])
assert.strictEqual(statSource.size, statTarget.size)
assert.strictEqual(statSource.size, res.size)
})
})
t.after(function() {
return flaska.closeAsync()
return Promise.all([
fs.rm('./test_tmp.png', { force: true }),
Promise.all(uploaded.map(file => fs.rm(file.path, { force: true }))),
flaska.closeAsync(),
])
})

View file

@ -1,5 +1,11 @@
import { Eltro as t, assert} from 'eltro'
import { QueryHandler, JsonHandler } from '../flaska.mjs'
import os from 'os'
import path from 'path'
import { Buffer } from 'buffer'
import { Eltro as t, assert, stub} from 'eltro'
import { QueryHandler, JsonHandler, FormidableHandler, HttpError } from '../flaska.mjs'
import { createCtx } from './helper.mjs'
import { finished } from 'stream'
import { setTimeout } from 'timers/promises'
t.describe('#QueryHandler()', function() {
let queryHandler = QueryHandler()
@ -25,24 +31,313 @@ t.describe('#QueryHandler()', function() {
t.describe('#JsonHandler()', function() {
let jsonHandler = JsonHandler()
let ctx
t.beforeEach(function() {
ctx = createCtx()
})
t.test('should return a handler', function() {
assert.strictEqual(typeof(jsonHandler), 'function')
})
t.test('should support separating query from request url', async function() {
t.test('should support fetching body from request', async function() {
const assertBody = { a: 1, temp: 'test', hello: 'world'}
let parsed = JSON.stringify(assertBody)
const ctx = {
req: [
Promise.resolve(Buffer.from(parsed.slice(0, parsed.length / 2))),
Promise.resolve(Buffer.from(parsed.slice(parsed.length / 2))),
]
}
let finished = false
let err = null
jsonHandler(ctx).catch(function(error) {
err = error
}).then(function() {
finished = true
})
assert.ok(ctx.req.on.called)
assert.strictEqual(ctx.req.on.firstCall[0], 'data')
assert.strictEqual(ctx.req.on.secondCall[0], 'end')
assert.strictEqual(finished, false)
ctx.req.on.firstCall[1](Buffer.from(parsed.slice(0, parsed.length / 2)))
assert.strictEqual(finished, false)
ctx.req.on.firstCall[1](Buffer.from(parsed.slice(parsed.length / 2)))
assert.strictEqual(finished, false)
ctx.req.on.secondCall[1]()
await setTimeout(10)
assert.strictEqual(finished, true)
assert.strictEqual(err, null)
await jsonHandler(ctx)
assert.notStrictEqual(ctx.req.body, assertBody)
assert.deepStrictEqual(ctx.req.body, assertBody)
})
t.test('should throw if buffer grows too large', async function() {
let defaultLimit = 10 * 1024
let segmentSize = 100
let finished = false
let err = null
jsonHandler(ctx).catch(function(error) {
err = error
}).then(function() {
finished = true
})
for (let i = 0; i < defaultLimit; i += segmentSize) {
ctx.req.on.firstCall[1](Buffer.alloc(segmentSize, 'a'))
}
await setTimeout(10)
assert.strictEqual(finished, true)
assert.notStrictEqual(err, null)
assert.ok(err instanceof HttpError)
assert.strictEqual(err.status, 413)
assert.match(err.message, new RegExp(100 * 103))
assert.match(err.message, new RegExp(defaultLimit))
})
t.test('should throw if buffer is not valid json', async function() {
let finished = false
let err = null
jsonHandler(ctx).catch(function(error) {
err = error
}).then(function() {
finished = true
})
ctx.req.on.firstCall[1](Buffer.alloc(10, 'a'))
ctx.req.on.firstCall[1](Buffer.alloc(10, 'a'))
ctx.req.on.secondCall[1]()
await setTimeout(10)
assert.strictEqual(finished, true)
assert.notStrictEqual(err, null)
assert.ok(err instanceof HttpError)
assert.strictEqual(err.status, 400)
assert.match(err.message, /JSON/)
assert.match(err.message, /Unexpected token a in/i)
assert.strictEqual(err.body.status, 400)
assert.match(err.body.message, /Invalid JSON/i)
assert.match(err.body.message, /Unexpected token a in/i)
assert.strictEqual(err.body.request, 'aaaaaaaaaaaaaaaaaaaa')
})
t.test('should not throw if body is empty', async function() {
let finished = false
let err = null
jsonHandler(ctx).catch(function(error) {
err = error
}).then(function() {
finished = true
})
assert.ok(ctx.req.on.called)
assert.strictEqual(ctx.req.on.firstCall[0], 'data')
assert.strictEqual(ctx.req.on.secondCall[0], 'end')
assert.strictEqual(finished, false)
ctx.req.on.secondCall[1]()
await setTimeout(10)
assert.strictEqual(finished, true)
assert.strictEqual(err, null)
assert.deepStrictEqual(ctx.req.body, {})
})
})
t.describe('#FormidableHandler()', function() {
let formidable
let incomingForm
let ctx
t.beforeEach(function() {
ctx = createCtx()
formidable = {
IncomingForm: stub(),
fsRename: stub().resolves(),
}
incomingForm = {
parse: stub().returnWith(function(req, cb) {
cb(null, {}, { file: { name: 'asdf.png' } })
})
}
formidable.IncomingForm.returns(incomingForm)
})
t.test('should call formidable with correct defaults', async function() {
let handler = FormidableHandler(formidable)
await handler(ctx)
assert.strictEqual(incomingForm.uploadDir, os.tmpdir())
assert.strictEqual(incomingForm.maxFileSize, 8 * 1024 * 1024)
assert.strictEqual(incomingForm.maxFieldsSize, 10 * 1024)
assert.strictEqual(incomingForm.maxFields, 50)
assert.strictEqual(incomingForm.parse.firstCall[0], ctx.req)
})
t.test('should apply fields and rename file before returning', async function() {
const assertFilename = 'Lets love.png'
const assertOriginalPath = 'Hitoshi/Fujima/Yuigi.png'
const assertFile = { a: 1, name: assertFilename, path: assertOriginalPath }
const assertFields = { b: 2, c: 3 }
let handler = FormidableHandler(formidable)
incomingForm.parse.returnWith(function(req, cb) {
cb(null, assertFields, { file: assertFile })
})
assert.notOk(ctx.req.body)
assert.notOk(ctx.req.file)
let prefix = new Date().toISOString().replace(/-/g, '').replace('T', '_').replace(/:/g, '').split('.')[0]
await handler(ctx)
assert.strictEqual(ctx.req.body, assertFields)
assert.strictEqual(ctx.req.file, assertFile)
assert.strictEqual(ctx.req.file.path, path.join(os.tmpdir(), ctx.req.file.filename))
assert.match(ctx.req.file.filename, new RegExp(prefix))
assert.match(ctx.req.file.filename, new RegExp(assertFilename))
assert.ok(formidable.fsRename.called)
assert.strictEqual(formidable.fsRename.firstCall[0], assertOriginalPath)
assert.strictEqual(formidable.fsRename.firstCall[1], ctx.req.file.path)
})
t.test('should throw parse error if parse fails', async function() {
const assertError = new Error('Aozora')
let handler = FormidableHandler(formidable)
incomingForm.parse.returnWith(function(req, cb) {
cb(assertError)
})
let err = await assert.isRejected(handler(ctx))
assert.strictEqual(err, assertError)
})
t.test('should throw rename error if rename fails', async function() {
const assertError = new Error('Aozora')
formidable.fsRename.rejects(assertError)
let handler = FormidableHandler(formidable)
let err = await assert.isRejected(handler(ctx))
assert.strictEqual(err, assertError)
})
t.test('should not call rename if no file is present', async function() {
const assertNotError = new Error('Aozora')
const assertFields = { a: 1 }
formidable.fsRename.rejects(assertNotError)
let handler = FormidableHandler(formidable)
incomingForm.parse.returnWith(function(req, cb) {
cb(null, assertFields, null)
})
await handler(ctx)
assert.strictEqual(ctx.req.body, assertFields)
assert.strictEqual(ctx.req.file, null)
incomingForm.parse.returnWith(function(req, cb) {
cb(null, assertFields, { file: null })
})
await handler(ctx)
assert.strictEqual(ctx.req.body, assertFields)
assert.strictEqual(ctx.req.file, null)
})
t.test('should throw filename error if filename fails', async function() {
const assertError = new Error('Dallaglio Piano')
let handler = FormidableHandler(formidable, {
filename: function() {
throw assertError
}
})
let err = await assert.isRejected(handler(ctx))
assert.strictEqual(err, assertError)
})
t.test('should default to original name of filename returns null', async function() {
const assertFilename = 'Herrscher.png'
let handler = FormidableHandler(formidable, {
filename: function() {
return null
}
})
incomingForm.parse.returnWith(function(req, cb) {
cb(null, { }, { file: { name: assertFilename } })
})
await handler(ctx)
assert.strictEqual(ctx.req.file.filename, assertFilename)
})
t.test('should support multiple filename calls in same second', async function() {
const assertFilename = 'Herrscher.png'
let handler = FormidableHandler(formidable)
await handler(ctx)
let file1 = ctx.req.file
await handler(ctx)
let file2 = ctx.req.file
assert.notStrictEqual(file1, file2)
assert.notStrictEqual(file1.filename, file2.filename)
})
t.test('should support parsing fields as json', async function() {
const assertFields = { b: '2', c: '3', e: 'asdf', f: '{"a": 1}' }
let handler = FormidableHandler(formidable, {
parseFields: true,
})
incomingForm.parse.returnWith(function(req, cb) {
cb(null, assertFields, { file: { name: 'test.png' } })
})
await handler(ctx)
assert.strictEqual(ctx.req.body, assertFields)
assert.strictEqual(ctx.req.body.b, 2)
assert.strictEqual(ctx.req.body.c, 3)
assert.strictEqual(ctx.req.body.e, 'asdf')
assert.deepStrictEqual(ctx.req.body.f, {a: 1})
})
})

0
test/upload/.gitkeep Normal file
View file