|
| 1 | +import * as httpMock from '../../../../test/http-mock'; |
| 2 | +import { PRESET_DEP_NOT_FOUND, PRESET_INVALID_JSON } from '../util'; |
| 3 | +import * as http from '.'; |
| 4 | + |
| 5 | +const host = 'https://my.server/'; |
| 6 | +const filePath = '/test-preset.json'; |
| 7 | +const repo = 'https://my.server/test-preset.json'; |
| 8 | + |
| 9 | +describe('config/presets/http/index', () => { |
| 10 | + describe('getPreset()', () => { |
| 11 | + it('should return parsed JSON', async () => { |
| 12 | + httpMock.scope(host).get(filePath).reply(200, { foo: 'bar' }); |
| 13 | + |
| 14 | + expect(await http.getPreset({ repo })).toEqual({ foo: 'bar' }); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should return parsed JSON5', async () => { |
| 18 | + httpMock |
| 19 | + .scope('https://my.server/') |
| 20 | + .get('/test-preset.json5') |
| 21 | + .reply(200, '{ foo: "bar" } // comment'); |
| 22 | + |
| 23 | + const repo = 'https://my.server/test-preset.json5'; |
| 24 | + |
| 25 | + expect(await http.getPreset({ repo })).toEqual({ foo: 'bar' }); |
| 26 | + }); |
| 27 | + |
| 28 | + it('throws if fails to parse', async () => { |
| 29 | + httpMock.scope(host).get(filePath).reply(200, 'not json'); |
| 30 | + |
| 31 | + await expect(http.getPreset({ repo })).rejects.toThrow( |
| 32 | + PRESET_INVALID_JSON, |
| 33 | + ); |
| 34 | + }); |
| 35 | + |
| 36 | + it('throws if file not found', async () => { |
| 37 | + httpMock.scope(host).get(filePath).reply(404); |
| 38 | + |
| 39 | + await expect(http.getPreset({ repo })).rejects.toThrow( |
| 40 | + PRESET_DEP_NOT_FOUND, |
| 41 | + ); |
| 42 | + }); |
| 43 | + |
| 44 | + it('throws on malformed URL', async () => { |
| 45 | + await expect(http.getPreset({ repo: 'malformed!' })).rejects.toThrow( |
| 46 | + PRESET_DEP_NOT_FOUND, |
| 47 | + ); |
| 48 | + }); |
| 49 | + }); |
| 50 | +}); |
0 commit comments