1import nock from 'nock';
2
3import { isUrlOk, validateUrl, stripPort, stripExtension } from '../url';
4
5describe(validateUrl, () => {
6  it(`guards against protocols`, () => {
7    expect(validateUrl('http://127.0.0.1:80', { protocols: ['http'] })).toBe(true);
8    expect(validateUrl('127.0.0.1:80', { requireProtocol: true })).toBe(false);
9    expect(validateUrl('http://127.0.0.1:80', { protocols: ['https'] })).toBe(false);
10    expect(validateUrl('http://127.0.0.1:80', {})).toBe(true);
11    expect(
12      validateUrl('127.0.0.1:80', { protocols: ['https', 'http'], requireProtocol: true })
13    ).toBe(false);
14    expect(validateUrl('https://expo.dev/', { protocols: ['https'] })).toBe(true);
15    expect(validateUrl('', { protocols: ['https'] })).toBe(false);
16    expect(validateUrl('hello', { protocols: ['https'] })).toBe(false);
17  });
18});
19
20describe(isUrlOk, () => {
21  it(`returns false when the URL returns non-200`, async () => {
22    const scope = nock('http://example.com').get('/').reply(504, '');
23    expect(await isUrlOk('http://example.com')).toBe(false);
24    expect(scope.isDone()).toBe(true);
25  });
26  it(`returns true when the URL returns 200`, async () => {
27    const scope = nock('http://example.com').get('/').reply(200, '');
28    expect(await isUrlOk('http://example.com')).toBe(true);
29    expect(scope.isDone()).toBe(true);
30  });
31});
32
33describe(stripPort, () => {
34  // Used in the manifest handler when the Expo Go app requests the given hostname to use.
35  it(`removes the port from a host string`, async () => {
36    expect(stripPort('localhost:8081')).toBe('localhost');
37  });
38  it(`removes the port from a URL string`, async () => {
39    expect(stripPort('http://localhost:8081/path/to.js')).toBe('localhost');
40  });
41});
42
43describe(stripExtension, () => {
44  it(`removes the extension from a file path`, async () => {
45    expect(stripExtension('/path/to/index.txt', 'txt')).toBe('/path/to/index');
46  });
47  it(`removes the extension from a URL string`, async () => {
48    expect(stripExtension('http://localhost:8081/index.js', 'js')).toBe(
49      'http://localhost:8081/index'
50    );
51  });
52});
53