more tests

Massimo Melina committed Dec 17, 2021 at 12:05 UTC b4140325f72a853d3e58246941b44313e3200a6d
4 files changed +49 -20
src/frontend.ts
+1
@@ -12,6 +12,7 @@ export const serveFrontend = DEV ? serveProxyFrontend() : serveStaticFrontend()
12 function serveProxyFrontend() {
13 console.debug('fronted: proxied')
14 return proxy('localhost:3000', {
15 + proxyReqPathResolver: (ctx) => ctx.path.endsWith('/') ? '/' : ctx.path,
16 userResDecorator: (res, data) => replaceFrontEndRes(data.toString('utf8'))
17 })
18 }
tests/alfa.txt new
+1
@@ -0,0 +1 @@
1 +abcdef
\ No newline at end of file
tests/test.ts
+34 -17
@@ -1,22 +1,39 @@
1 -const axios = require('axios')
1 +import axios from 'axios'
2 +import { Done } from 'mocha'
3 +/*
4 +import { PORT, srv } from '../src'
5
3 -describe('file', () => {
4 - itHttp('md', '/dev-notes.md', '#')
5 - itHttp('frontend', '/', '<body>')
6 - itHttp('api.list', '/~/api/file_list', data => Array.isArray(data.list))
6 +process.chdir('..')
7 +const appStarted = new Promise(resolve =>
8 + srv.on( 'app_started', resolve) )
9 +*/
10 +describe('basics', () => {
11 + //before(async () => appStarted)
12 + it('frontend', req('/', s => s.includes('<body>')))
13 + it('api.list', req('/~/api/file_list', s => Array.isArray(s.list)))
14 + it('download', req('/f1/f2/alfa.txt', s => s.includes('abcd')))
15 + it('partial download', req('/f1/f2/alfa.txt', s => s.includes('a') && !s.includes('d'), {
16 + headers: { Range: 'bytes=0-2' }
17 + }))
18 + it('perm1', req('/for-fred', 404))
19 + it('perm2', req('/for-max/', 200))
20 + it('proxy', req('/proxy', s => s.includes('github')))
21 })
22
9 -type Tester = (x:any) => boolean
23 +type Tester = number | ((data:any, fullResponse:any) => boolean | Error)
24
11 -function itHttp(name: string, uri: string, test: string | Tester) {
12 - it(name, () =>
13 - axios.get(uri).then((res: any) => {
14 - if (typeof test === 'string') {
15 - if (!res.data.includes(test))
16 - throw 'bad content: ' + res.data
17 - }
18 - else if (typeof test === 'function')
19 - if (!test(res.data))
20 - throw 'test failed: ' + JSON.stringify(res.data)
21 - }) )
25 +function req(methodUrl: string, test:Tester, requestOptions?:any) {
26 + return (done:Done) => {
27 + const i = methodUrl.indexOf('/')
28 + const method = methodUrl.slice(0,i) || 'GET'
29 + const url = 'http://localhost'+methodUrl.slice(i)
30 + function fun(res:any) {
31 + return done(typeof test === 'number' ? (res.status || res.response.status) !== test : !test(res.data, res))
32 + }
33 + axios.request({ method, url, ...requestOptions })
34 + .then(fun, fun)
35 + .catch(err => {
36 + done(err)
37 + })
38 + }
39 }
\ No newline at end of file
vfs.yaml
+13 -3
@@ -1,4 +1,14 @@
1 -source: .
1 children:
3 - - source: /music
4 - hide: mix*
2 + - name: f1
3 + children:
4 + - name: f2
5 + children:
6 + - source: tests/alfa.txt
7 + - name: proxy
8 + source: https://raw.githubusercontent.com/nodejs/node/master/README.md
9 + - name: for-max
10 + perm:
11 + max: r
12 + - name: for-fred
13 + perm:
14 + fred: r