Skip to content
Snippets Groups Projects
Select Git revision
  • 62d5d670bf24e077b3c3643707219493148e4fa3
  • main default protected
  • renovate/main-renovatebot-osv-offline-1.x
  • fix/36927-maven-tags
  • renovate/main-redis-5.x
  • next
  • revert-31645-feat/rename-gradle-wrapper-validation-action
  • fix/36615b-branch-reuse-no-cache
  • chore/punycode
  • refactor/pin-new-value
  • feat/36219--git-x509-signing
  • feat/structured-logger
  • hotfix/39.264.1
  • feat/skip-dangling
  • gh-readonly-queue/next/pr-36034-7a061c4ca1024a19e2c295d773d9642625d1c2be
  • hotfix/39.238.3
  • refactor/gitlab-auto-approve
  • feat/template-strings
  • gh-readonly-queue/next/pr-35654-137d934242c784e0c45d4b957362214f0eade1d7
  • fix/32307-global-extends-merging
  • fix/32307-global-extends-repositories
  • 41.37.7
  • 41.37.6
  • 41.37.5
  • 41.37.4
  • 41.37.3
  • 41.37.2
  • 41.37.1
  • 41.37.0
  • 41.36.2
  • 41.36.1
  • 41.36.0
  • 41.35.2
  • 41.35.1
  • 41.35.0
  • 41.34.1
  • 41.34.0
  • 41.33.0
  • 41.32.3
  • 41.32.2
  • 41.32.1
41 results

index.ts

Blame
  • host-rules.spec.ts 7.26 KiB
    import { PlatformId } from '../../constants';
    import { bootstrap } from '../../proxy';
    import * as hostRules from '../host-rules';
    import { dnsLookup } from './dns';
    import { applyHostRules } from './host-rules';
    
    const url = 'https://github.com';
    
    jest.mock('global-agent');
    
    describe('util/http/host-rules', () => {
      const options = {
        hostType: PlatformId.Github,
      };
    
      beforeEach(() => {
        // reset module
        jest.resetAllMocks();
    
        delete process.env.HTTP_PROXY;
    
        // clean up hostRules
        hostRules.clear();
        hostRules.add({
          hostType: PlatformId.Github,
          token: 'token',
        });
        hostRules.add({
          hostType: PlatformId.Gitea,
          password: 'password',
        });
    
        hostRules.add({
          hostType: 'npm',
          authType: 'Basic',
          token: 'XXX',
          timeout: 5000,
        });
    
        hostRules.add({
          hostType: PlatformId.Gitlab,
          token: 'abc',
        });
    
        hostRules.add({
          hostType: PlatformId.Bitbucket,
          token: 'cdef',
        });
      });
    
      afterEach(() => {
        delete process.env.HTTP_PROXY;
      });
    
      it('adds token', () => {
        expect(applyHostRules(url, { ...options })).toMatchInlineSnapshot(`
          {
            "context": {
              "authType": undefined,
            },
            "hostType": "github",
            "token": "token",
          }
        `);
      });
    
      it('adds auth', () => {
        expect(applyHostRules(url, { hostType: PlatformId.Gitea }))
          .toMatchInlineSnapshot(`
          {
            "hostType": "gitea",
            "password": "password",
            "username": undefined,
          }
        `);
      });
    
      it('adds custom auth', () => {
        expect(applyHostRules(url, { hostType: 'npm' })).toMatchInlineSnapshot(`
          {
            "context": {
              "authType": "Basic",
            },
            "hostType": "npm",
            "timeout": 5000,
            "token": "XXX",
          }
        `);
      });
    
      it('skips', () => {
        expect(applyHostRules(url, { ...options, token: 'xxx' }))
          .toMatchInlineSnapshot(`
          {
            "hostType": "github",
            "token": "xxx",
          }
        `);
      });
    
      it('uses http2', () => {
        hostRules.add({ enableHttp2: true });
        expect(applyHostRules(url, { ...options, token: 'xxx' }))
          .toMatchInlineSnapshot(`
          {
            "hostType": "github",
            "http2": true,
            "token": "xxx",
          }
        `);
      });
    
      it('uses dnsCache', () => {
        hostRules.add({ dnsCache: true });
        expect(applyHostRules(url, { ...options, token: 'xxx' })).toMatchObject({
          hostType: 'github',
          lookup: dnsLookup,
          token: 'xxx',
        });
      });
    
      it('disables http2', () => {
        process.env.HTTP_PROXY = 'http://proxy';
        bootstrap();
        hostRules.add({ enableHttp2: true });
        expect(applyHostRules(url, { ...options, token: 'xxx' }))
          .toMatchInlineSnapshot(`
          {
            "hostType": "github",
            "token": "xxx",
          }
        `);
      });
    
      it('noAuth', () => {
        expect(applyHostRules(url, { ...options, noAuth: true }))
          .toMatchInlineSnapshot(`
          {
            "hostType": "github",
            "noAuth": true,
          }
        `);
      });
    
      it('no fallback to github', () => {
        hostRules.add({
          hostType: 'github-tags',
          username: 'some2',
          password: 'xxx2',
        });
        hostRules.add({
          hostType: 'github-changelog',
          token: 'changelogtoken',
        });
        hostRules.add({
          hostType: 'pod',
          token: 'pod-token',
        });
        hostRules.add({
          hostType: 'github-releases',
          username: 'some',
          password: 'xxx',
        });
        expect(
          applyHostRules(url, { ...options, hostType: 'github-releases' })
        ).toEqual({
          hostType: 'github-releases',
          username: 'some',
          password: 'xxx',
        });
        expect(
          applyHostRules(url, { ...options, hostType: 'github-tags' })
        ).toEqual({
          hostType: 'github-tags',
          username: 'some2',
          password: 'xxx2',
        });
        expect(applyHostRules(url, { ...options, hostType: 'pod' })).toEqual({
          context: {
            authType: undefined,
          },
          hostType: 'pod',
          token: 'pod-token',
        });
        expect(
          applyHostRules(url, { ...options, hostType: 'github-changelog' })
        ).toEqual({
          context: {
            authType: undefined,
          },
          hostType: 'github-changelog',
          token: 'changelogtoken',
        });
      });
    
      it('fallback to github', () => {
        expect(
          applyHostRules(url, { ...options, hostType: 'github-tags' })
        ).toEqual({
          context: {
            authType: undefined,
          },
          hostType: 'github-tags',
          token: 'token',
        });
        expect(
          applyHostRules(url, { ...options, hostType: 'github-changelog' })
        ).toEqual({
          context: {
            authType: undefined,
          },
          hostType: 'github-changelog',
          token: 'token',
        });
        expect(applyHostRules(url, { ...options, hostType: 'pod' })).toEqual({
          context: {
            authType: undefined,
          },
          hostType: 'pod',
          token: 'token',
        });
      });
    
      it('no fallback to gitlab', () => {
        hostRules.add({
          hostType: 'gitlab-packages',
          token: 'package-token',
        });
        hostRules.add({
          hostType: 'gitlab-releases',
          token: 'release-token',
        });
        hostRules.add({
          hostType: 'gitlab-tags',
          token: 'tags-token',
        });
        expect(
          applyHostRules(url, { ...options, hostType: 'gitlab-tags' })
        ).toEqual({
          context: {
            authType: undefined,
          },
          hostType: 'gitlab-tags',
          token: 'tags-token',
        });
        expect(
          applyHostRules(url, { ...options, hostType: 'gitlab-releases' })
        ).toEqual({
          context: {
            authType: undefined,
          },
          hostType: 'gitlab-releases',
          token: 'release-token',
        });
        expect(
          applyHostRules(url, { ...options, hostType: 'gitlab-packages' })
        ).toEqual({
          context: {
            authType: undefined,
          },
          hostType: 'gitlab-packages',
          token: 'package-token',
        });
      });
    
      it('fallback to gitlab', () => {
        expect(
          applyHostRules(url, { ...options, hostType: 'gitlab-tags' })
        ).toEqual({
          context: {
            authType: undefined,
          },
          hostType: 'gitlab-tags',
          token: 'abc',
        });
        expect(
          applyHostRules(url, { ...options, hostType: 'gitlab-releases' })
        ).toEqual({
          context: {
            authType: undefined,
          },
          hostType: 'gitlab-releases',
          token: 'abc',
        });
        expect(
          applyHostRules(url, { ...options, hostType: 'gitlab-packages' })
        ).toEqual({
          context: {
            authType: undefined,
          },
          hostType: 'gitlab-packages',
          token: 'abc',
        });
        expect(
          applyHostRules(url, { ...options, hostType: 'gitlab-changelog' })
        ).toEqual({
          context: {
            authType: undefined,
          },
          hostType: 'gitlab-changelog',
          token: 'abc',
        });
      });
    
      it('no fallback to bitbucket', () => {
        hostRules.add({
          hostType: 'bitbucket-tags',
          username: 'some',
          password: 'xxx',
        });
        expect(
          applyHostRules(url, { ...options, hostType: 'bitbucket-tags' })
        ).toEqual({
          hostType: 'bitbucket-tags',
          username: 'some',
          password: 'xxx',
        });
      });
    
      it('fallback to bitbucket', () => {
        expect(
          applyHostRules(url, { ...options, hostType: 'bitbucket-tags' })
        ).toEqual({
          context: {
            authType: undefined,
          },
          hostType: 'bitbucket-tags',
          token: 'cdef',
        });
      });
    });