Skip to content
Snippets Groups Projects
Select Git revision
  • af7d6c68356fd71ac622cb41b2ca04dc2f4ea09e
  • main default protected
  • renovate/main-renovatebot-github-action-43.x
  • next
  • feat/gnupg
  • fix/36615b-branch-reuse-no-cache
  • renovate/main-redis-5.x
  • 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
  • gh-readonly-queue/next/pr-35009-046ebf7cb84ab859f7fefceb5fa53a54ce9736f8
  • 41.45.0
  • 41.44.0
  • 41.43.7
  • 41.43.6
  • 41.43.5
  • 41.43.4
  • 41.43.3
  • 41.43.2
  • 41.43.1
  • 41.43.0
  • 41.42.12
  • 41.42.11
  • 41.42.10
  • 41.42.9
  • 41.42.8
  • 41.42.7
  • 41.42.6
  • 41.42.5
  • 41.42.4
  • 41.42.3
41 results

util.spec.ts

Blame
  • util.spec.ts 3.53 KiB
    import type Request from 'got/dist/source/core';
    import { partial } from '../../../../test/util';
    import { HOST_DISABLED } from '../../../constants/error-messages';
    import { type Http, HttpError } from '../../../util/http';
    import { parseUrl } from '../../../util/url';
    import {
      checkResource,
      downloadHttpProtocol,
      downloadMavenXml,
      downloadS3Protocol,
    } from './util';
    
    function httpError({
      name,
      message = 'unknown error',
      code,
      request = {},
      response,
    }: {
      name?: string;
      message?: string;
      code?: HttpError['code'];
      request?: Partial<Request>;
      response?: Partial<Response>;
    }): HttpError {
      type Writeable<T> = { -readonly [P in keyof T]: T[P] };
    
      const err = new HttpError(
        message,
        { code },
        request as never,
      ) as Writeable<HttpError>;
    
      if (name) {
        err.name = name;
      }
    
      if (response) {
        err.response = response as never;
      }
    
      return err;
    }
    
    describe('modules/datasource/maven/util', () => {
      describe('downloadMavenXml', () => {
        it('returns empty object for unsupported protocols', async () => {
          const res = await downloadMavenXml(
            null as never, // #22198
            parseUrl('unsupported://server.com/'),
          );
          expect(res).toEqual({});
        });
    
        it('returns empty object for invalid URLs', async () => {
          const res = await downloadMavenXml(
            null as never, // #22198
            null,
          );
          expect(res).toEqual({});
        });
      });
    
      describe('downloadS3Protocol', () => {
        it('returns null for non-S3 URLs', async () => {
          // #22198
          const res = await downloadS3Protocol(parseUrl('http://not-s3.com/')!);
          expect(res).toBeNull();
        });
      });
    
      describe('downloadHttpProtocol', () => {
        it('returns empty for HOST_DISABLED error', async () => {
          const http = partial<Http>({
            get: () => Promise.reject(httpError({ message: HOST_DISABLED })),
          });
          const res = await downloadHttpProtocol(http, 'some://');
          expect(res).toBeNull();
        });
    
        it('returns empty for host error', async () => {
          const http = partial<Http>({
            get: () => Promise.reject(httpError({ code: 'ETIMEDOUT' })),
          });
          const res = await downloadHttpProtocol(http, 'some://');
          expect(res).toBeNull();
        });
    
        it('returns empty for temporary error', async () => {
          const http = partial<Http>({
            get: () => Promise.reject(httpError({ code: 'ECONNRESET' })),
          });
          const res = await downloadHttpProtocol(http, 'some://');
          expect(res).toBeNull();
        });
    
        it('returns empty for connection error', async () => {
          const http = partial<Http>({
            get: () => Promise.reject(httpError({ code: 'ECONNREFUSED' })),
          });
          const res = await downloadHttpProtocol(http, 'some://');
          expect(res).toBeNull();
        });
    
        it('returns empty for unsupported error', async () => {
          const http = partial<Http>({
            get: () =>
              Promise.reject(httpError({ name: 'UnsupportedProtocolError' })),
          });
          const res = await downloadHttpProtocol(http, 'some://');
          expect(res).toBeNull();
        });
      });
    
      describe('checkResource', () => {
        it('returns not found for unsupported protocols', async () => {
          const res = await checkResource(
            null as never, // #22198
            'unsupported://server.com/',
          );
          expect(res).toBe('not-found');
        });
    
        it('returns error for invalid URLs', async () => {
          const res = await checkResource(
            null as never, // #22198
            'not-a-valid-url',
          );
          expect(res).toBe('error');
        });
      });
    });