|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import {afterEach, describe, expect, it, vi} from 'vitest'; |
| 9 | +import path from 'node:path'; |
| 10 | +import {createOpenUrlContext} from '../utils'; |
| 11 | +import type {StartCLIOptions} from '../start'; |
| 12 | + |
| 13 | +vi.mock('../../../server/getHostPort', () => ({ |
| 14 | + getHostPort: async () => ({host: 'localhost', port: 3000}), |
| 15 | +})); |
| 16 | + |
| 17 | +describe('createOpenUrlContext', () => { |
| 18 | + function getCertFixture(name: string) { |
| 19 | + return path.join( |
| 20 | + __dirname, |
| 21 | + '../../../webpack/utils/__tests__/__fixtures__/getHttpsConfig', |
| 22 | + name, |
| 23 | + ); |
| 24 | + } |
| 25 | + |
| 26 | + async function getOpenUrl(cliOptions: StartCLIOptions) { |
| 27 | + const context = await createOpenUrlContext({cliOptions}); |
| 28 | + return context.getOpenUrl({baseUrl: '/baseUrl/', router: 'browser'}); |
| 29 | + } |
| 30 | + |
| 31 | + afterEach(() => { |
| 32 | + vi.unstubAllEnvs(); |
| 33 | + }); |
| 34 | + |
| 35 | + it('returns an http url by default', async () => { |
| 36 | + await expect(getOpenUrl({})).resolves.toBe( |
| 37 | + 'http://localhost:3000/baseUrl/', |
| 38 | + ); |
| 39 | + }); |
| 40 | + |
| 41 | + it('returns an https url with --https', async () => { |
| 42 | + await expect(getOpenUrl({https: true})).resolves.toBe( |
| 43 | + 'https://localhost:3000/baseUrl/', |
| 44 | + ); |
| 45 | + }); |
| 46 | + |
| 47 | + it('returns an https url with --ssl-cert and --ssl-key', async () => { |
| 48 | + await expect( |
| 49 | + getOpenUrl({ |
| 50 | + sslCert: getCertFixture('host.crt'), |
| 51 | + sslKey: getCertFixture('host.key'), |
| 52 | + }), |
| 53 | + ).resolves.toBe('https://localhost:3000/baseUrl/'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('returns an https url with DOCUSAURUS_HTTPS=true', async () => { |
| 57 | + vi.stubEnv('DOCUSAURUS_HTTPS', 'true'); |
| 58 | + await expect(getOpenUrl({})).resolves.toBe( |
| 59 | + 'https://localhost:3000/baseUrl/', |
| 60 | + ); |
| 61 | + }); |
| 62 | + |
| 63 | + it('returns an http url with DOCUSAURUS_HTTPS=false', async () => { |
| 64 | + vi.stubEnv('DOCUSAURUS_HTTPS', 'false'); |
| 65 | + await expect(getOpenUrl({})).resolves.toBe( |
| 66 | + 'http://localhost:3000/baseUrl/', |
| 67 | + ); |
| 68 | + }); |
| 69 | + |
| 70 | + it('shares the resolved HTTPS config with the dev server', async () => { |
| 71 | + const context = await createOpenUrlContext({ |
| 72 | + cliOptions: { |
| 73 | + sslCert: getCertFixture('host.crt'), |
| 74 | + sslKey: getCertFixture('host.key'), |
| 75 | + }, |
| 76 | + }); |
| 77 | + expect(context.httpsConfig).toEqual({ |
| 78 | + cert: expect.any(Buffer), |
| 79 | + key: expect.any(Buffer), |
| 80 | + }); |
| 81 | + }); |
| 82 | +}); |
0 commit comments