diff options
| author | ST-DDT <[email protected]> | 2024-11-09 11:32:15 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-11-09 10:32:15 +0000 |
| commit | 9ecf99b75763ed08e19f3115c01a5e58aeaf529c (patch) | |
| tree | a39162eea4c1b81568f192fbf5e8c8049c907479 /test/integration/modules | |
| parent | 0d850758d0ea0db45a9a4c8abda5c1e09796fb44 (diff) | |
| download | faker-9ecf99b75763ed08e19f3115c01a5e58aeaf529c.tar.xz faker-9ecf99b75763ed08e19f3115c01a5e58aeaf529c.zip | |
test: verify the generated image links are working (#3127)
Diffstat (limited to 'test/integration/modules')
| -rw-r--r-- | test/integration/modules/image.spec.ts | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/test/integration/modules/image.spec.ts b/test/integration/modules/image.spec.ts new file mode 100644 index 00000000..0901b99b --- /dev/null +++ b/test/integration/modules/image.spec.ts @@ -0,0 +1,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`)); + } 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 '${location}'` + ) + ); + } + }) + .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('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); + }); + }); + + describe('urlPlaceholder', () => { + it('should return a random image url from Placeholder', async () => { + const actual = faker.image.urlPlaceholder(); + await assertWorkingUrl(actual); + }); + }); +}); |
