1 | import fs from 'fs'
|
2 | import path from 'path'
|
3 | import CachingUrlFetcher, { REQUEST_CACHE_DIR } from '../src/CachingUrlFetcher'
|
4 | import { NetworkError } from '../src/errors'
|
5 |
|
6 | const fetcher = new CachingUrlFetcher()
|
7 |
|
8 | jest.setTimeout(30 * 60 * 1000)
|
9 |
|
10 | test('fetch:npm', async () => {
|
11 | let result = await fetcher.fetchUrl('https://registry.npmjs.org/@stencila/dockter')
|
12 | expect(result.name).toBe('@stencila/dockter')
|
13 | })
|
14 |
|
15 | test('fetch:cache', async () => {
|
16 |
|
17 | let result = await fetcher.fetchUrl('https://registry.npmjs.org/@stencila/dockter')
|
18 | expect(result.name).toBe('@stencila/dockter')
|
19 | })
|
20 |
|
21 | test('fetch:cache-empty-error', async () => {
|
22 |
|
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 |
|
28 | test('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 |
|
37 | test('fetch:404', async () => {
|
38 | let result = await fetcher.fetchUrl('https://registry.npmjs.org/@stencila/nonExistingProject')
|
39 | expect(result).toBeNull()
|
40 | })
|
41 |
|
42 | test('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 |
|
48 | test('fetch:otherErr', async () => {
|
49 |
|
50 | await expect(
|
51 | fetcher.fetchUrl('google.com')
|
52 | ).rejects.toThrow()
|
53 | })
|