Best JavaScript code snippet using wpt
ImageToolsErrorTest.ts
Source:ImageToolsErrorTest.ts
1import { Assertions, Chain, GeneralSteps, Logger, Mouse, Pipeline, UiFinder } from '@ephox/agar';2import { UnitTest } from '@ephox/bedrock';3import { Arr } from '@ephox/katamari';4import { TinyApis, TinyDom, TinyLoader } from '@ephox/mcagar';5import { Html } from '@ephox/sugar';6import Plugin from 'tinymce/plugins/imagetools/Plugin';7import ModernTheme from 'tinymce/themes/modern/Theme';8import ImageUtils from '../module/test/ImageUtils';9UnitTest.asynctest('browser.tinymce.plugins.imagetools.ImageToolsErrorTest', function () {10 const success = arguments[arguments.length - 2];11 const failure = arguments[arguments.length - 1];12 const uploadHandlerState = ImageUtils.createStateContainer();13 const corsUrl = 'http://moxiecode.cachefly.net/tinymce/v9/images/logo.png';14 Plugin();15 ModernTheme();16 const sAssertErrorMessage = function (html) {17 return Chain.asStep(TinyDom.fromDom(document.body), [18 UiFinder.cWaitFor('Could not find notification', '.mce-notification-inner'),19 Chain.mapper(Html.get),20 Assertions.cAssertHtml('Message html does not match', html)21 ]);22 };23 const sCloseErrorMessage = Chain.asStep(TinyDom.fromDom(document.body), [24 UiFinder.cWaitFor('Could not find notification', '.mce-notification > button'),25 Mouse.cClick26 ]);27 TinyLoader.setup(28 function (editor, onSuccess, onFailure) {29 const tinyApis = TinyApis(editor);30 const stepsWithTeardown = Arr.bind([31 Logger.t('incorrect service url no api key', GeneralSteps.sequence([32 uploadHandlerState.sResetState,33 tinyApis.sSetSetting('imagetools_proxy', 'http://0.0.0.0.0.0/'),34 tinyApis.sSetSetting('api_key', undefined),35 ImageUtils.sLoadImage(editor, corsUrl),36 tinyApis.sSelect('img', []),37 ImageUtils.sExecCommand(editor, 'mceImageFlipHorizontal'),38 sAssertErrorMessage('ImageProxy HTTP error: Incorrect Image Proxy URL')39 ])),40 Logger.t('incorrect service url with api key', GeneralSteps.sequence([41 uploadHandlerState.sResetState,42 tinyApis.sSetSetting('imagetools_proxy', 'http://0.0.0.0.0.0/'),43 tinyApis.sSetSetting('api_key', 'fake_key'),44 ImageUtils.sLoadImage(editor, corsUrl),45 tinyApis.sSelect('img', []),46 ImageUtils.sExecCommand(editor, 'mceImageFlipHorizontal'),47 sAssertErrorMessage('ImageProxy HTTP error: Incorrect Image Proxy URL')48 ])),49 Logger.t('403 no api key', GeneralSteps.sequence([50 uploadHandlerState.sResetState,51 tinyApis.sSetSetting('imagetools_proxy', '/custom/403'),52 tinyApis.sSetSetting('api_key', undefined),53 ImageUtils.sLoadImage(editor, corsUrl),54 tinyApis.sSelect('img', []),55 ImageUtils.sExecCommand(editor, 'mceImageFlipHorizontal'),56 sAssertErrorMessage('ImageProxy HTTP error: Rejected request')57 ])),58 Logger.t('403 with api key', GeneralSteps.sequence([59 uploadHandlerState.sResetState,60 tinyApis.sSetSetting('imagetools_proxy', '/custom/403'),61 tinyApis.sSetSetting('api_key', 'fake_key'),62 ImageUtils.sLoadImage(editor, corsUrl),63 tinyApis.sSelect('img', []),64 ImageUtils.sExecCommand(editor, 'mceImageFlipHorizontal'),65 sAssertErrorMessage('ImageProxy Service error: Invalid JSON in service error message')66 ])),67 Logger.t('403 with api key and return error data', GeneralSteps.sequence([68 uploadHandlerState.sResetState,69 tinyApis.sSetSetting('imagetools_proxy', '/custom/403data'),70 tinyApis.sSetSetting('api_key', 'fake_key'),71 ImageUtils.sLoadImage(editor, corsUrl),72 tinyApis.sSelect('img', []),73 ImageUtils.sExecCommand(editor, 'mceImageFlipHorizontal'),74 sAssertErrorMessage('ImageProxy Service error: Unknown service error')75 ])),76 Logger.t('404 no api key', GeneralSteps.sequence([77 uploadHandlerState.sResetState,78 tinyApis.sSetSetting('imagetools_proxy', '/custom/404'),79 tinyApis.sSetSetting('api_key', undefined),80 ImageUtils.sLoadImage(editor, corsUrl),81 tinyApis.sSelect('img', []),82 ImageUtils.sExecCommand(editor, 'mceImageFlipHorizontal'),83 sAssertErrorMessage('ImageProxy HTTP error: Could not find Image Proxy')84 ])),85 Logger.t('404 with api key', GeneralSteps.sequence([86 uploadHandlerState.sResetState,87 tinyApis.sSetSetting('imagetools_proxy', '/custom/404'),88 tinyApis.sSetSetting('api_key', 'fake_key'),89 ImageUtils.sLoadImage(editor, corsUrl),90 tinyApis.sSelect('img', []),91 ImageUtils.sExecCommand(editor, 'mceImageFlipHorizontal'),92 sAssertErrorMessage('ImageProxy HTTP error: Could not find Image Proxy')93 ]))94 ], function (step) {95 return [96 step,97 GeneralSteps.sequence([98 sCloseErrorMessage,99 tinyApis.sSetContent('')100 ])101 ];102 });103 Pipeline.async({}, stepsWithTeardown, onSuccess, onFailure);104 },105 {106 plugins: 'imagetools',107 automatic_uploads: false,108 skin_url: '/project/js/tinymce/skins/lightgray'109 },110 success,111 failure112 );...
cors-preflight.js
Source:cors-preflight.js
1if (this.document === undefined) {2 importScripts("/resources/testharness.js");3 importScripts("/common/utils.js");4 importScripts("../resources/utils.js");5 importScripts("/common/get-host-info.sub.js");6}7function headerNames(headers)8{9 let names = [];10 for (let header of headers)11 names.push(header[0].toLowerCase());12 return names13}14/*15 Check preflight is done16 Control if server allows method and headers and check accordingly17 Check control access headers added by UA (for method and headers)18*/19function corsPreflight(desc, corsUrl, method, allowed, headers, safeHeaders) {20 return promise_test(function(test) {21 var uuid_token = token();22 return fetch(RESOURCES_DIR + "clean-stash.py?token=" + uuid_token).then(function(response) {23 var url = corsUrl;24 var urlParameters = "?token=" + uuid_token + "&max_age=0";25 var requestInit = {"mode": "cors", "method": method};26 var requestHeaders = [];27 if (headers)28 requestHeaders.push.apply(requestHeaders, headers);29 if (safeHeaders)30 requestHeaders.push.apply(requestHeaders, safeHeaders);31 requestInit["headers"] = requestHeaders;32 if (allowed) {33 urlParameters += "&allow_methods=" + method;34 if (headers) {35 //Let's check prefligh request.36 //Server will send back headers from Access-Control-Request-Headers in x-control-request-headers37 urlParameters += "&control_request_headers"38 //Make the server allow the headers39 urlParameters += "&allow_headers=" + headerNames(headers).join("%20%2C");40 }41 return fetch(url + urlParameters, requestInit).then(function(resp) {42 assert_equals(resp.status, 200, "Response's status is 200");43 assert_equals(resp.headers.get("x-did-preflight"), "1", "Preflight request has been made");44 if (headers) {45 var actualHeaders = resp.headers.get("x-control-request-headers").toLowerCase().split(",");46 for (var i in actualHeaders)47 actualHeaders[i] = actualHeaders[i].trim();48 for (var header of headers)49 assert_in_array(header[0].toLowerCase(), actualHeaders, "Preflight asked permission for header: " + header);50 let accessControlAllowHeaders = headerNames(headers).sort().join(",");51 assert_equals(resp.headers.get("x-control-request-headers"), accessControlAllowHeaders, "Access-Control-Allow-Headers value");52 return fetch(RESOURCES_DIR + "clean-stash.py?token=" + uuid_token);53 }54 });55 } else {56 return promise_rejects(test, new TypeError(), fetch(url + urlParameters, requestInit)).then(function(){57 return fetch(RESOURCES_DIR + "clean-stash.py?token=" + uuid_token);58 });59 }60 });61 }, desc);62}63var corsUrl = get_host_info().HTTP_REMOTE_ORIGIN + dirname(location.pathname) + RESOURCES_DIR + "preflight.py";64corsPreflight("CORS [DELETE], server allows", corsUrl, "DELETE", true);65corsPreflight("CORS [DELETE], server refuses", corsUrl, "DELETE", false);66corsPreflight("CORS [PUT], server allows", corsUrl, "PUT", true);67corsPreflight("CORS [PUT], server refuses", corsUrl, "PUT", false);68corsPreflight("CORS [PATCH], server allows", corsUrl, "PATCH", true);69corsPreflight("CORS [PATCH], server refuses", corsUrl, "PATCH", false);70corsPreflight("CORS [NEW], server allows", corsUrl, "NEW", true);71corsPreflight("CORS [NEW], server refuses", corsUrl, "NEW", false);72corsPreflight("CORS [GET] [x-test-header: allowed], server allows", corsUrl, "GET", true, [["x-test-header1", "allowed"]]);73corsPreflight("CORS [GET] [x-test-header: refused], server refuses", corsUrl, "GET", false, [["x-test-header1", "refused"]]);74var headers = [75 ["x-test-header1", "allowedOrRefused"],76 ["x-test-header2", "allowedOrRefused"],77 ["X-test-header3", "allowedOrRefused"],78 ["x-test-header-b", "allowedOrRefused"],79 ["x-test-header-D", "allowedOrRefused"],80 ["x-test-header-C", "allowedOrRefused"],81 ["x-test-header-a", "allowedOrRefused"],82 ["Content-Type", "allowedOrRefused"],83];84var safeHeaders= [85 ["Accept", "*"],86 ["Accept-Language", "bzh"],87 ["Content-Language", "eu"],88];89corsPreflight("CORS [GET] [several headers], server allows", corsUrl, "GET", true, headers, safeHeaders);90corsPreflight("CORS [GET] [several headers], server refuses", corsUrl, "GET", false, headers, safeHeaders);91corsPreflight("CORS [PUT] [several headers], server allows", corsUrl, "PUT", true, headers, safeHeaders);92corsPreflight("CORS [PUT] [several headers], server refuses", corsUrl, "PUT", false, headers, safeHeaders);...
cors-preflight-referrer.js
Source:cors-preflight-referrer.js
1if (this.document === undefined) {2 importScripts("/resources/testharness.js");3 importScripts("/common/utils.js");4 importScripts("/common/get-host-info.sub.js");5 importScripts("../resources/utils.js");6}7function corsPreflightReferrer(desc, corsUrl, referrerPolicy, referrer, expectedReferrer) {8 var uuid_token = token();9 var url = corsUrl;10 var urlParameters = "?token=" + uuid_token + "&max_age=0";11 var requestInit = {"mode": "cors", "referrerPolicy": referrerPolicy};12 if (referrer)13 requestInit.referrer = referrer;14 /* Force preflight */15 requestInit["headers"] = {"x-force-preflight": ""};16 urlParameters += "&allow_headers=x-force-preflight";17 promise_test(function(test) {18 return fetch(RESOURCES_DIR + "clean-stash.py?token=" + uuid_token).then(function(resp) {19 assert_equals(resp.status, 200, "Clean stash response's status is 200");20 return fetch(url + urlParameters, requestInit).then(function(resp) {21 assert_equals(resp.status, 200, "Response's status is 200");22 assert_equals(resp.headers.get("x-did-preflight"), "1", "Preflight request has been made");23 assert_equals(resp.headers.get("x-preflight-referrer"), expectedReferrer, "Preflight's referrer is correct");24 assert_equals(resp.headers.get("x-referrer"), expectedReferrer, "Request's referrer is correct");25 assert_equals(resp.headers.get("x-control-request-headers"), "", "Access-Control-Allow-Headers value");26 });27 });28 }, desc + " and referrer: " + (referrer ? "'" + referrer + "'" : "default"));29}30var corsUrl = get_host_info().HTTP_REMOTE_ORIGIN + dirname(location.pathname) + RESOURCES_DIR + "preflight.py";31var origin = get_host_info().HTTP_ORIGIN + "/";32corsPreflightReferrer("Referrer policy: no-referrer", corsUrl, "no-referrer", undefined, "");33corsPreflightReferrer("Referrer policy: no-referrer", corsUrl, "no-referrer", "myreferrer", "");34corsPreflightReferrer("Referrer policy: \"\"", corsUrl, "", undefined, location.toString())35corsPreflightReferrer("Referrer policy: \"\"", corsUrl, "", "myreferrer", new URL("myreferrer", location).toString());36corsPreflightReferrer("Referrer policy: origin", corsUrl, "origin", undefined, origin);37corsPreflightReferrer("Referrer policy: origin", corsUrl, "origin", "myreferrer", origin);38corsPreflightReferrer("Referrer policy: origin-when-cross-origin", corsUrl, "origin-when-cross-origin", undefined, origin);39corsPreflightReferrer("Referrer policy: origin-when-cross-origin", corsUrl, "origin-when-cross-origin", "myreferrer", origin);40corsPreflightReferrer("Referrer policy: unsafe-url", corsUrl, "unsafe-url", undefined, location.toString());41corsPreflightReferrer("Referrer policy: unsafe-url", corsUrl, "unsafe-url", "myreferrer", new URL("myreferrer", location).toString());...
Using AI Code Generation
1var wpt = require('./wpt.js');2wpt.corsURL(url, function(data) {3 console.log(data);4});5var corsURL = function(url, callback) {6 var request = require('request');7 var options = {8 };9 request.get(options, function(error, response, body) {10 if (!error && response.statusCode === 200) {11 callback(body);12 }13 });14};15module.exports.corsURL = corsURL;16{ data: 17 [ { location: 'Dulles:Chrome',18 label: 'Dulles:Chrome' },19 { location: 'Dulles:Firefox',
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new WebPageTest('www.webpagetest.org', 'A.1234567890abcdef1234567890abcdef');3wpt.corsURL(url, function(err, data) {4 if (err) {5 console.log(err);6 } else {7 console.log(data);8 }9});10var wpt = require('wpt');11var wpt = new WebPageTest('www.webpagetest.org', 'A.1234567890abcdef1234567890abcdef');12wpt.getLocations(function(err, data) {13 if (err) {14 console.log(err);15 } else {16 console.log(data);17 }18});19var wpt = require('wpt');20var wpt = new WebPageTest('www.webpagetest.org', 'A.1234567890abcdef1234567890abcdef');21wpt.getTesters(function(err, data) {22 if (err) {23 console.log(err);24 } else {25 console.log(data);26 }27});28var wpt = require('wpt');29var wpt = new WebPageTest('www.webpagetest.org', 'A.1234567890abcdef1234567890abcdef');30var testID = "1234567890";31wpt.getTestStatus(testID, function(err, data) {32 if (err) {33 console.log(err);34 } else {35 console.log(data);36 }37});
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new WebPageTest('www.webpagetest.org', 'A.8a6b7c8d9e0f1g2h3i4j5k6l7m8n9o0p');3 if (err) return console.log(err);4 console.log(data);5 wpt.corsURL(data.data.jsonUrl, function(err, data) {6 if (err) return console.log(err);7 console.log(data);8 });9});10### runTest(url, options, callback)11var wpt = require('wpt');12var wpt = new WebPageTest('www.webpagetest.org', 'A.8a6b7c8d9e0f1g2h3i4j5k6l7m8n9o0p');13 if (err) return console.log(err);14 console.log(data);15});16### getTestStatus(testId, callback)17var wpt = require('wpt');18var wpt = new WebPageTest('www.webpagetest.org', 'A.8a6b7c8d9e0f1g2h3i4j5k6l7m8n9o0p');19wpt.getTestStatus('140317_4T_1P', function(err, data) {20 if (err) return console.log(err);21 console.log(data);22});23### getTestResults(testId, callback)
Using AI Code Generation
1var wpt = require('wpt-api');2var wpt = new wpt('API_KEY');3 if (err) {4 console.log('error: ' + err);5 } else {6 console.log('data: ' + data);7 }8});9var wpt = require('wpt-api');10var wpt = new wpt('API_KEY');11wpt.getLocations(function(err, data) {12 if (err) {13 console.log('error: ' + err);14 } else {15 console.log('data: ' + data);16 }17});18var wpt = require('wpt-api');19var wpt = new wpt('API_KEY');20wpt.getTesters(function(err, data) {21 if (err) {22 console.log('error: ' + err);23 } else {24 console.log('data: ' + data);25 }26});27var wpt = require('wpt-api');28var wpt = new wpt('API_KEY');29wpt.getTestStatus('test_id', function(err, data) {30 if (err) {31 console.log('error: ' + err);32 } else {33 console.log('data: ' + data);34 }35});36var wpt = require('wpt-api');37var wpt = new wpt('API_KEY');38wpt.getTestResults('test_id', function(err, data) {39 if (err) {40 console.log('error: ' + err);41 } else {42 console.log('data: ' + data);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!