screenshotone-validations
Version:
Validation schemes for the ScreenshotOne API requests.
266 lines (265 loc) • 14.1 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const globals_1 = require("@jest/globals");
const main_1 = __importStar(require("./main"));
(0, globals_1.describe)("request validator", () => {
(0, globals_1.test)("returns validated value for a valid request", async () => {
const { error, value } = main_1.default.take.validateGet({
url: "https://example.com",
});
(0, globals_1.expect)(error).toBeUndefined();
(0, globals_1.expect)(value.url).toBe("https://example.com");
(0, globals_1.expect)(value.format).toBe("jpg");
});
(0, globals_1.test)("returns error and validated value for an invalid request", async () => {
const { error, value } = (0, main_1.validateRequest)(main_1.default.take.getScheme, { url: "https://" });
(0, globals_1.expect)(error).toBeDefined();
(0, globals_1.expect)(error?.message).toContain("must be a valid URI");
(0, globals_1.expect)(value.url).toBe("https://");
});
(0, globals_1.test)("returns a clear error when full_page_scroll is specified without full_page", async () => {
const { error, value } = main_1.default.take.validateGet({
url: "https://example.com",
full_page: false,
full_page_scroll: true,
});
(0, globals_1.expect)(error).toEqual({
message: '"full_page_scroll" is not allowed',
});
(0, globals_1.expect)(value.full_page_scroll).toBe(true);
});
});
(0, globals_1.describe)("URL validations", () => {
(0, globals_1.test)("https://https://example.com must be invalid", async () => {
const { error } = main_1.default.take.getScheme.validate({ url: "https://https://example.com" }, main_1.default.take.validationOptions);
(0, globals_1.expect)(error).toBeDefined();
});
(0, globals_1.test)("http://https://example.com must be invalid", async () => {
const { error } = main_1.default.take.getScheme.validate({ url: "http://https://example.com" }, main_1.default.take.validationOptions);
(0, globals_1.expect)(error).toBeDefined();
});
(0, globals_1.test)("https:// must be invalid", async () => {
const { error } = main_1.default.take.getScheme.validate({ url: "https://" }, main_1.default.take.validationOptions);
(0, globals_1.expect)(error).toBeDefined();
});
(0, globals_1.test)("https://example.com must be valid", async () => {
const { error, value } = main_1.default.take.getScheme.validate({ url: "https://example.com" }, main_1.default.take.validationOptions);
(0, globals_1.expect)(error).toBeUndefined();
});
(0, globals_1.test)("https://example.com:80 must be valid", async () => {
const { error, value } = main_1.default.take.getScheme.validate({ url: "https://example.com:80" }, main_1.default.take.validationOptions);
(0, globals_1.expect)(error).toBeUndefined();
});
(0, globals_1.test)("https://1.1.1.1:80 must be valid", async () => {
const { error, value } = main_1.default.take.getScheme.validate({ url: "https://1.1.1.1:80" }, main_1.default.take.validationOptions);
(0, globals_1.expect)(error).toBeUndefined();
});
(0, globals_1.test)("https://1.1.1.1 must be valid", async () => {
const { error, value } = main_1.default.take.getScheme.validate({ url: "https://1.1.1.1" }, main_1.default.take.validationOptions);
(0, globals_1.expect)(error).toBeUndefined();
});
(0, globals_1.test)("proxy with http://example.com must be valid", async () => {
const { error, value } = main_1.default.take.getScheme.validate({ proxy: "http://example.com", url: "https://example.com" }, main_1.default.take.validationOptions);
(0, globals_1.expect)(error).toBeUndefined();
});
(0, globals_1.test)("proxy with http://example.com:8080 must be valid", async () => {
const { error, value } = main_1.default.take.getScheme.validate({ proxy: "http://example.com:8080", url: "https://example.com" }, main_1.default.take.validationOptions);
(0, globals_1.expect)(error).toBeUndefined();
});
(0, globals_1.test)("proxy with http://1.1.1.1 must be valid", async () => {
const { error, value } = main_1.default.take.getScheme.validate({ proxy: "http://1.1.1.1", url: "https://example.com" }, main_1.default.take.validationOptions);
(0, globals_1.expect)(error).toBeUndefined();
});
(0, globals_1.test)("proxy with http://1.1.1.1:8080 must be valid", async () => {
const { error, value } = main_1.default.take.getScheme.validate({ proxy: "http://1.1.1.1:8080", url: "https://example.com" }, main_1.default.take.validationOptions);
(0, globals_1.expect)(error).toBeUndefined();
});
(0, globals_1.test)("proxy with https://example.com must be invalid", async () => {
const { error } = main_1.default.take.getScheme.validate({ proxy: "https://example.com", url: "https://example.com" }, main_1.default.take.validationOptions);
(0, globals_1.expect)(error).toBeDefined();
});
(0, globals_1.test)("proxy with invalid URL must be invalid", async () => {
const { error } = main_1.default.take.getScheme.validate({ proxy: "not-a-url", url: "https://example.com" }, main_1.default.take.validationOptions);
(0, globals_1.expect)(error).toBeDefined();
});
(0, globals_1.test)("proxy with auth credentials must be valid", async () => {
const { error, value } = main_1.default.take.getScheme.validate({
proxy: "http://user:pass@example.com:8080",
url: "https://example.com",
}, main_1.default.take.validationOptions);
(0, globals_1.expect)(() => new URL(value.proxy)).not.toThrow();
(0, globals_1.expect)(error).toBeUndefined();
});
(0, globals_1.test)("proxy with unicode characters should be valid", async () => {
const { error, value } = main_1.default.take.getScheme.validate({ proxy: "http://münchen.de", url: "https://example.com" }, main_1.default.take.validationOptions);
(0, globals_1.expect)(() => new URL(value.proxy)).not.toThrow();
(0, globals_1.expect)(error).toBeUndefined();
});
(0, globals_1.test)("proxy with spaces should not be valid", async () => {
const { error, value } = main_1.default.take.getScheme.validate({ proxy: "http://proxy server.com", url: "https://example.com" }, main_1.default.take.validationOptions);
(0, globals_1.expect)(() => new URL(value.proxy)).toThrow();
(0, globals_1.expect)(error).toBeDefined();
});
(0, globals_1.test)("proxy with unescaped brackets must invalid", async () => {
const { error, value } = main_1.default.take.getScheme.validate({ proxy: "http://proxy[1].com", url: "https://example.com" }, main_1.default.take.validationOptions);
(0, globals_1.expect)(() => new URL(value.proxy)).toThrow();
(0, globals_1.expect)(error).toBeDefined();
});
});
(0, globals_1.describe)("Full page slices validations", () => {
(0, globals_1.test)("full_page_slices requires full_page", async () => {
const { error } = main_1.default.take.getScheme.validate({
url: "https://example.com",
full_page_slices: true,
response_type: "json",
}, main_1.default.take.validationOptions);
(0, globals_1.expect)(error).toBeDefined();
});
(0, globals_1.test)("full_page_slices allows by_format response type", async () => {
const { error, value } = main_1.default.take.getScheme.validate({
url: "https://example.com",
full_page: true,
full_page_slices: true,
response_type: "by_format",
}, main_1.default.take.validationOptions);
(0, globals_1.expect)(error).toBeUndefined();
(0, globals_1.expect)(value.response_type).toBe("by_format");
});
(0, globals_1.test)("full_page_slices rejects store", async () => {
const { error } = main_1.default.take.getScheme.validate({
url: "https://example.com",
full_page: true,
full_page_slices: true,
response_type: "json",
store: true,
storage_path: "test",
}, main_1.default.take.validationOptions);
(0, globals_1.expect)(error).toBeDefined();
});
(0, globals_1.test)("full_page_slice_height must be positive", async () => {
const { error } = main_1.default.take.getScheme.validate({
url: "https://example.com",
full_page: true,
full_page_slices: true,
response_type: "json",
full_page_slice_height: 0,
}, main_1.default.take.validationOptions);
(0, globals_1.expect)(error).toBeDefined();
});
(0, globals_1.test)("full_page_slice_overlap_height must be non-negative", async () => {
const { error } = main_1.default.take.getScheme.validate({
url: "https://example.com",
full_page: true,
full_page_slices: true,
response_type: "json",
full_page_slice_overlap_height: -1,
}, main_1.default.take.validationOptions);
(0, globals_1.expect)(error).toBeDefined();
});
(0, globals_1.test)("full_page_slice_overlap_height must leave at least 100px step", async () => {
const { error } = main_1.default.take.getScheme.validate({
url: "https://example.com",
full_page: true,
full_page_slices: true,
response_type: "json",
full_page_slice_height: 4000,
full_page_slice_overlap_height: 3901,
}, main_1.default.take.validationOptions);
(0, globals_1.expect)(error).toBeDefined();
});
(0, globals_1.test)("full_page_slice_overlap_height can be zero", async () => {
const { error, value } = main_1.default.take.getScheme.validate({
url: "https://example.com",
full_page: true,
full_page_slices: true,
response_type: "json",
full_page_slice_height: 4000,
full_page_slice_overlap_height: 0,
}, main_1.default.take.validationOptions);
(0, globals_1.expect)(error).toBeUndefined();
(0, globals_1.expect)(value.full_page_slice_overlap_height).toBe(0);
});
(0, globals_1.test)("full_page_slices defaults slice options", async () => {
const { error, value } = main_1.default.take.getScheme.validate({
url: "https://example.com",
full_page: true,
full_page_slices: true,
response_type: "json",
}, main_1.default.take.validationOptions);
(0, globals_1.expect)(error).toBeUndefined();
(0, globals_1.expect)(value.full_page_slice_height).toBe(4000);
(0, globals_1.expect)(value.full_page_slice_overlap_height).toBe(0);
});
});
(0, globals_1.describe)("Clip validations", () => {
(0, globals_1.test)("full_page is valid without clip options", async () => {
const { error } = main_1.default.take.getScheme.validate({
url: "https://example.com",
full_page: true,
}, main_1.default.take.validationOptions);
(0, globals_1.expect)(error).toBeUndefined();
});
(0, globals_1.test)("clip and full_page are mutually exclusive", async () => {
const { error } = main_1.default.take.getScheme.validate({
url: "https://example.com",
full_page: true,
clip_x: 0,
clip_y: 0,
clip_width: 800,
clip_height: 600,
}, main_1.default.take.validationOptions);
(0, globals_1.expect)(error).toBeDefined();
});
(0, globals_1.test)("clip options set to zero and full_page are mutually exclusive", async () => {
const { error } = main_1.default.take.getScheme.validate({
url: "https://example.com",
full_page: true,
clip_x: 0,
clip_y: 0,
clip_width: 0,
clip_height: 0,
}, main_1.default.take.validationOptions);
(0, globals_1.expect)(error).toBeDefined();
});
globals_1.test.each(["clip_x", "clip_y", "clip_width", "clip_height"])("%s and full_page are mutually exclusive", async (clipOption) => {
const { error } = main_1.default.take.getScheme.validate({
url: "https://example.com",
full_page: true,
[clipOption]: 0,
}, main_1.default.take.validationOptions);
(0, globals_1.expect)(error).toBeDefined();
});
(0, globals_1.test)("clip is valid without full_page", async () => {
const { error } = main_1.default.take.getScheme.validate({
url: "https://example.com",
clip_x: 0,
clip_y: 0,
clip_width: 800,
clip_height: 600,
}, main_1.default.take.validationOptions);
(0, globals_1.expect)(error).toBeUndefined();
});
});