UNPKG

1.86 kBPlain TextView Raw
1import fs from 'fs'
2import path from 'path'
3import CachingUrlFetcher, { REQUEST_CACHE_DIR } from '../src/CachingUrlFetcher'
4import { NetworkError } from '../src/errors'
5
6const fetcher = new CachingUrlFetcher()
7
8jest.setTimeout(30 * 60 * 1000)
9
10test('fetch:npm', async () => {
11 let result = await fetcher.fetchUrl('https://registry.npmjs.org/@stencila/dockter')
12 expect(result.name).toBe('@stencila/dockter')
13})
14
15test('fetch:cache', async () => {
16 // same as previous, but should cover cache code
17 let result = await fetcher.fetchUrl('https://registry.npmjs.org/@stencila/dockter')
18 expect(result.name).toBe('@stencila/dockter')
19})
20
21test('fetch:cache-empty-error', async () => {
22 // this will work but cache will fail internally with 'does not look like a valid storage file' error
23 fs.writeFileSync(path.join(REQUEST_CACHE_DIR, '5acbdfceeb37a779ad5c81e903bfae92'), '')
24 let result = await fetcher.fetchUrl('https://registry.npmjs.org/@stencila/dockter')
25 expect(result.name).toBe('@stencila/dockter')
26})
27
28test('fetch:cache-other-error', async () => {
29 let cacheFile = path.join(REQUEST_CACHE_DIR, '5acbdfceeb37a779ad5c81e903bfae92')
30 fs.chmodSync(cacheFile, '000')
31 await expect(
32 fetcher.fetchUrl('https://registry.npmjs.org/@stencila/dockter')
33 ).rejects.toThrow()
34 fs.chmodSync(cacheFile, '666')
35})
36
37test('fetch:404', async () => {
38 let result = await fetcher.fetchUrl('https://registry.npmjs.org/@stencila/nonExistingProject')
39 expect(result).toBeNull()
40})
41
42test('fetch:ENOTFOUND', async () => {
43 await expect(
44 fetcher.fetchUrl('notAnUrl')
45 ).rejects.toThrow(new NetworkError('There was a problem fetching notAnUrl (ENOTFOUND). Are you connected to the internet?'))
46})
47
48test('fetch:otherErr', async () => {
49 // This should throw a ParseError or similar
50 await expect(
51 fetcher.fetchUrl('google.com')
52 ).rejects.toThrow()
53})