Best JavaScript code snippet using wpt
cors-upload.any.js
Source:cors-upload.any.js
1// META: title=Cross-Origin POST with preflight and FormData body should send body2// META: script=/common/get-host-info.sub.js3"use strict";4function testCorsFormDataUpload(description, path, method, form, headers, withCredentials) {5 const test = async_test(description);6 const client = new XMLHttpRequest();7 const url = corsURL(path);8 client.open(method, url, true);9 client.withCredentials = withCredentials;10 for (const key of Object.keys(headers)) {11 client.setRequestHeader(key, headers[key]);12 }13 client.send(form);14 client.onload = () => {15 test.step(() => {16 assert_equals(client.status, 200);17 assert_regexp_match(client.responseText, /Content-Disposition: form-data/);18 for (const key of form.keys()) {19 assert_regexp_match(client.responseText, new RegExp(key));20 assert_regexp_match(client.responseText, new RegExp(form.get(key)));21 }22 });23 test.done();24 };25}26function corsURL(path) {27 const url = new URL(path, location.href);28 url.hostname = get_host_info().REMOTE_HOST;29 return url.href;30}31const form = new FormData();32form.append("key", "value");33testCorsFormDataUpload(34 "Cross-Origin POST FormData body but no preflight",35 "resources/echo-content-cors.py",36 "POST",37 form,38 {},39 false40);41testCorsFormDataUpload(42 "Cross-Origin POST with preflight and FormData body",43 "resources/echo-content-cors.py",44 "POST",45 form,46 {47 Authorization: "Bearer access-token"48 },49 true...
Using AI Code Generation
1function testCorsFormDataUpload() {2 var data = new FormData();3 data.append('test', 'test');4 var xhr = new XMLHttpRequest();5 xhr.onload = function() {6 if (xhr.status == 200) {7 alert('Success!');8 } else {9 alert('Error: ' + xhr.status);10 }11 };12 xhr.onerror = function() {13 alert('Error: ' + xhr.status);14 };15 xhr.send(data);16}17function testCorsFormDataUpload() {18 var data = new FormData();19 data.append('test', 'test');20 var xhr = new XMLHttpRequest();21 xhr.onload = function() {22 if (xhr.status == 200) {23 alert('Success!');24 } else {25 alert('Error: ' + xhr.status);26 }27 };28 xhr.onerror = function() {29 alert('Error: ' + xhr.status);30 };31 xhr.send(data);32}33function testCorsFormDataUpload() {34 var data = new FormData();35 data.append('test', 'test');36 var xhr = new XMLHttpRequest();37 xhr.onload = function() {38 if (xhr.status == 200) {39 alert('Success!');40 } else {41 alert('Error: ' + xhr.status);42 }43 };44 xhr.onerror = function() {45 alert('Error: ' + xhr.status);46 };47 xhr.send(data);48}49function testCorsFormDataUpload() {50 var data = new FormData();51 data.append('test', 'test');52 var xhr = new XMLHttpRequest();53 xhr.onload = function() {54 if (xhr.status == 200) {55 alert('Success!');56 } else {57 alert('Error: ' + xhr.status);58 }59 };60 xhr.onerror = function() {61 alert('Error: ' + xhr.status);62 };
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!!