Skip to content

Commit f933403

Browse files
committed
Fixed README.md and added 10 more tests
1 parent 8c79994 commit f933403

File tree

2 files changed

+196
-4
lines changed

2 files changed

+196
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ This comes in handy if you're dealing with simulating a file system, for example
221221
These are the methods used by the package (they can all return a `Promise` or be asynchronous):
222222

223223
```js
224-
await handler(request, response, null, {
224+
await handler(request, response, undefined, {
225225
stat(path) {},
226226
createReadStream(path) {},
227227
readdir(path) {}

test/integration.js

Lines changed: 195 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ const fs = require('fs-extra');
1111
// Utilities
1212
const handler = require('../');
1313

14-
const getUrl = config => {
14+
const getUrl = (config, handlers) => {
1515
const server = micro(async (request, response) => {
16-
await handler(request, response, config);
16+
await handler(request, response, config, handlers);
1717
});
1818

1919
return listen(server);
@@ -199,7 +199,7 @@ test('set `trailingSlash` config property to `false`', async t => {
199199
t.is(location, target);
200200
});
201201

202-
test('set `rewrites` config property to wildcard paths', async t => {
202+
test('set `rewrites` config property to wildcard path', async t => {
203203
const destination = '.yarnrc';
204204
const related = path.join(process.cwd(), destination);
205205
const content = await fs.readFile(related, 'utf8');
@@ -216,3 +216,195 @@ test('set `rewrites` config property to wildcard paths', async t => {
216216

217217
t.is(text, content);
218218
});
219+
220+
test('set `rewrites` config property to one-star wildcard path', async t => {
221+
const destination = '.yarnrc';
222+
const related = path.join(process.cwd(), destination);
223+
const content = await fs.readFile(related, 'utf8');
224+
225+
const url = await getUrl({
226+
rewrites: [{
227+
source: 'face/*/mask',
228+
destination
229+
}]
230+
});
231+
232+
const response = await fetch(`${url}/face/delete/mask`);
233+
const text = await response.text();
234+
235+
t.is(text, content);
236+
});
237+
238+
test('set `rewrites` config property to path segment', async t => {
239+
const related = path.join(process.cwd(), 'package.json');
240+
const content = await fs.readJSON(related);
241+
242+
const url = await getUrl({
243+
rewrites: [{
244+
source: 'face/:id',
245+
destination: ':id.json'
246+
}]
247+
});
248+
249+
const response = await fetch(`${url}/face/package`);
250+
const json = await response.json();
251+
252+
t.deepEqual(json, content);
253+
});
254+
255+
test('set `redirects` config property to wildcard path', async t => {
256+
const destination = 'testing';
257+
258+
const url = await getUrl({
259+
redirects: [{
260+
source: 'face/**',
261+
destination
262+
}]
263+
});
264+
265+
const response = await fetch(`${url}/face/mask`, {
266+
redirect: 'manual',
267+
follow: 0
268+
});
269+
270+
const location = response.headers.get('location');
271+
t.is(location, `${url}/${destination}`);
272+
});
273+
274+
test('set `redirects` config property to one-star wildcard path', async t => {
275+
const destination = 'testing';
276+
277+
const url = await getUrl({
278+
redirects: [{
279+
source: 'face/*/ideal',
280+
destination
281+
}]
282+
});
283+
284+
const response = await fetch(`${url}/face/mask/ideal`, {
285+
redirect: 'manual',
286+
follow: 0
287+
});
288+
289+
const location = response.headers.get('location');
290+
t.is(location, `${url}/${destination}`);
291+
});
292+
293+
test('set `redirects` config property to path segment', async t => {
294+
const url = await getUrl({
295+
redirects: [{
296+
source: 'face/:segment',
297+
destination: 'mask/:segment'
298+
}]
299+
});
300+
301+
const response = await fetch(`${url}/face/me`, {
302+
redirect: 'manual',
303+
follow: 0
304+
});
305+
306+
const location = response.headers.get('location');
307+
t.is(location, `${url}/mask/me`);
308+
});
309+
310+
test('set `redirects` config property to wildcard path and `trailingSlash` to `true`', async t => {
311+
const target = '/face/mask';
312+
313+
const url = await getUrl({
314+
trailingSlash: true,
315+
redirects: [{
316+
source: 'face/**',
317+
destination: 'testing'
318+
}]
319+
});
320+
321+
const response = await fetch(url + target, {
322+
redirect: 'manual',
323+
follow: 0
324+
});
325+
326+
const location = response.headers.get('location');
327+
t.is(location, `${url + target}/`);
328+
});
329+
330+
test('set `redirects` config property to wildcard path and `trailingSlash` to `false`', async t => {
331+
const target = '/face/mask';
332+
333+
const url = await getUrl({
334+
trailingSlash: false,
335+
redirects: [{
336+
source: 'face/**',
337+
destination: 'testing'
338+
}]
339+
});
340+
341+
const response = await fetch(`${url + target}/`, {
342+
redirect: 'manual',
343+
follow: 0
344+
});
345+
346+
const location = response.headers.get('location');
347+
t.is(location, url + target);
348+
});
349+
350+
test('pass custom handlers', async t => {
351+
const name = '.yarnrc';
352+
353+
// eslint-disable-next-line no-undefined
354+
const url = await getUrl(undefined, {
355+
stat: fs.stat,
356+
createReadStream: fs.createReadStream
357+
});
358+
359+
const response = await fetch(`${url}/${name}`);
360+
const text = await response.text();
361+
const content = await fs.readFile(path.join(process.cwd(), name), 'utf8');
362+
363+
t.is(text, content);
364+
});
365+
366+
test('set `headers` to wildcard headers', async t => {
367+
const key = 'Cache-Control';
368+
const value = 'max-age=7200';
369+
370+
const list = [{
371+
source: '*.md',
372+
headers: [{
373+
key,
374+
value
375+
}]
376+
}];
377+
378+
const url = await getUrl({
379+
headers: list
380+
});
381+
382+
const response = await fetch(`${url}/README.md`);
383+
const cacheControl = response.headers.get(key);
384+
385+
t.is(cacheControl, value);
386+
});
387+
388+
test('set `headers` to fixed headers and check default headers', async t => {
389+
const key = 'Cache-Control';
390+
const value = 'max-age=7200';
391+
392+
const list = [{
393+
source: 'package.json',
394+
headers: [{
395+
key,
396+
value
397+
}]
398+
}];
399+
400+
const url = await getUrl({
401+
headers: list
402+
});
403+
404+
const {headers} = await fetch(`${url}/package.json`);
405+
const cacheControl = headers.get(key);
406+
const type = headers.get('content-type');
407+
408+
t.is(cacheControl, value);
409+
t.is(type, 'application/json');
410+
});

0 commit comments

Comments
 (0)