aboutsummaryrefslogtreecommitdiff
path: root/test/integration/modules/image.spec.ts
blob: f9d5cabdb2ce0ddf797d1f74efac5889ae668236 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Integration tests for the image methods ensuring that the returned urls work.
 */
import https from 'node:https';
import { resolve as urlResolve } from 'node:url';
import { describe, expect, it } from 'vitest';
import { faker } from '../../../src';

/**
 * Checks that the given address is a working https address.
 *
 * An address is considered working, if it:
 *
 * - is a string
 * - starts with https
 * - is a proper url
 * - returns a http-200 (after redirects)
 *
 * There is a separate unit test file for checking if the returned URL matches the expectations (domain, parameters, etc.).
 *
 * @param address The address to check.
 */
async function assertWorkingUrl(address: string): Promise<void> {
  expect(address).toBeTypeOf('string');
  expect(address).toMatch(/^https:\/\//);
  expect(() => new URL(address)).not.toThrow();

  await expect(
    new Promise((resolve, reject) => {
      https
        .get(address, ({ statusCode, headers: { location } }) => {
          if (statusCode == null) {
            reject(new Error(`No StatusCode, expected 200 for '${address}'`));
          } else if (statusCode === 200) {
            resolve(true);
          } else if (statusCode >= 300 && statusCode < 400 && location) {
            const newAddress = urlResolve(address, location);
            assertWorkingUrl(newAddress)
              .then(() => resolve(true))
              .catch((error: unknown) => {
                reject(
                  new Error(`Failed to resolve redirect to '${location}'`, {
                    cause: error,
                  })
                );
              });
          } else {
            reject(
              new Error(
                `Bad StatusCode ${statusCode} expected 200 for '${address}'`
              )
            );
          }
        })
        .on('error', (error: unknown) => {
          reject(new Error(`Failed to get '${address}'`, { cause: error }));
        });
    })
  ).resolves.toBe(true);
}

describe('image', () => {
  describe('avatar', () => {
    it('should return a random avatar url', async () => {
      const actual = faker.image.avatar();
      await assertWorkingUrl(actual);
    });
  });

  describe('avatarGitHub', () => {
    it('should return a random avatar url from GitHub', async () => {
      const actual = faker.image.avatarGitHub();
      await assertWorkingUrl(actual);
    });
  });

  describe('personPortrait', () => {
    it('should return a random asset url', async () => {
      const actual = faker.image.personPortrait();
      await assertWorkingUrl(actual);
    });
  });

  describe('url', () => {
    it('should return a random image url', async () => {
      const actual = faker.image.url();
      await assertWorkingUrl(actual);
    });

    it('should return a random image url with a width', async () => {
      const actual = faker.image.url({ width: 100 });
      await assertWorkingUrl(actual);
    });

    it('should return a random image url with a height', async () => {
      const actual = faker.image.url({ height: 100 });
      await assertWorkingUrl(actual);
    });

    it('should return a random image url with a width and height', async () => {
      const actual = faker.image.url({ width: 128, height: 64 });
      await assertWorkingUrl(actual);
    });
  });

  describe('urlLoremFlickr', () => {
    it('should return a random image url from LoremFlickr', async () => {
      const actual = faker.image.urlLoremFlickr();
      await assertWorkingUrl(actual);
    });
  });

  describe('urlPicsumPhotos', () => {
    it('should return a random image url from PicsumPhotos', async () => {
      const actual = faker.image.urlPicsumPhotos();
      await assertWorkingUrl(actual);
    });
  });
});