Best JavaScript code snippet using wpt
general.any.js
Source:general.any.js
1// META: timeout=long2// META: global=window,worker3// META: script=/common/utils.js4// META: script=/common/get-host-info.sub.js5// META: script=../request/request-error.js6const BODY_METHODS = ['arrayBuffer', 'blob', 'formData', 'json', 'text'];7// This is used to close connections that weren't correctly closed during the tests,8// otherwise you can end up running out of HTTP connections.9let requestAbortKeys = [];10function abortRequests() {11 const keys = requestAbortKeys;12 requestAbortKeys = [];13 return Promise.all(14 keys.map(key => fetch(`../resources/stash-put.py?key=${key}&value=close`))15 );16}17const hostInfo = get_host_info();18const urlHostname = hostInfo.REMOTE_HOST;19promise_test(async t => {20 const controller = new AbortController();21 const signal = controller.signal;22 controller.abort();23 const fetchPromise = fetch('../resources/data.json', { signal });24 await promise_rejects(t, "AbortError", fetchPromise);25}, "Aborting rejects with AbortError");26promise_test(async t => {27 const controller = new AbortController();28 const signal = controller.signal;29 controller.abort();30 const url = new URL('../resources/data.json', location);31 url.hostname = urlHostname;32 const fetchPromise = fetch(url, {33 signal,34 mode: 'no-cors'35 });36 await promise_rejects(t, "AbortError", fetchPromise);37}, "Aborting rejects with AbortError - no-cors");38// Test that errors thrown from the request constructor take priority over abort errors.39// badRequestArgTests is from response-error.js40for (const { args, testName } of badRequestArgTests) {41 promise_test(async t => {42 try {43 // If this doesn't throw, we'll effectively skip the test.44 // It'll fail properly in ../request/request-error.html45 new Request(...args);46 }47 catch (err) {48 const controller = new AbortController();49 controller.abort();50 // Add signal to 2nd arg51 args[1] = args[1] || {};52 args[1].signal = controller.signal;53 await promise_rejects(t, new TypeError, fetch(...args));54 }55 }, `TypeError from request constructor takes priority - ${testName}`);56}57test(() => {58 const request = new Request('');59 assert_true(Boolean(request.signal), "Signal member is present & truthy");60 assert_equals(request.signal.constructor, AbortSignal);61}, "Request objects have a signal property");62promise_test(async t => {63 const controller = new AbortController();64 const signal = controller.signal;65 controller.abort();66 const request = new Request('../resources/data.json', { signal });67 assert_true(Boolean(request.signal), "Signal member is present & truthy");68 assert_equals(request.signal.constructor, AbortSignal);69 assert_not_equals(request.signal, signal, 'Request has a new signal, not a reference');70 assert_true(request.signal.aborted, `Request's signal has aborted`);71 const fetchPromise = fetch(request);72 await promise_rejects(t, "AbortError", fetchPromise);73}, "Signal on request object");74promise_test(async t => {75 const controller = new AbortController();76 const signal = controller.signal;77 controller.abort();78 const request = new Request('../resources/data.json', { signal });79 const requestFromRequest = new Request(request);80 const fetchPromise = fetch(requestFromRequest);81 await promise_rejects(t, "AbortError", fetchPromise);82}, "Signal on request object created from request object");83promise_test(async t => {84 const controller = new AbortController();85 const signal = controller.signal;86 controller.abort();87 const request = new Request('../resources/data.json');88 const requestFromRequest = new Request(request, { signal });89 const fetchPromise = fetch(requestFromRequest);90 await promise_rejects(t, "AbortError", fetchPromise);91}, "Signal on request object created from request object, with signal on second request");92promise_test(async t => {93 const controller = new AbortController();94 const signal = controller.signal;95 controller.abort();96 const request = new Request('../resources/data.json', { signal: new AbortController().signal });97 const requestFromRequest = new Request(request, { signal });98 const fetchPromise = fetch(requestFromRequest);99 await promise_rejects(t, "AbortError", fetchPromise);100}, "Signal on request object created from request object, with signal on second request overriding another");101promise_test(async t => {102 const controller = new AbortController();103 const signal = controller.signal;104 controller.abort();105 const request = new Request('../resources/data.json', { signal });106 const fetchPromise = fetch(request, {method: 'POST'});107 await promise_rejects(t, "AbortError", fetchPromise);108}, "Signal retained after unrelated properties are overridden by fetch");109promise_test(async t => {110 const controller = new AbortController();111 const signal = controller.signal;112 controller.abort();113 const request = new Request('../resources/data.json', { signal });114 const data = await fetch(request, { signal: null }).then(r => r.json());115 assert_equals(data.key, 'value', 'Fetch fully completes');116}, "Signal removed by setting to null");117promise_test(async t => {118 const controller = new AbortController();119 const signal = controller.signal;120 controller.abort();121 const log = [];122 await Promise.all([123 fetch('../resources/data.json', { signal }).then(124 () => assert_unreached("Fetch must not resolve"),125 () => log.push('fetch-reject')126 ),127 Promise.resolve().then(() => log.push('next-microtask'))128 ]);129 assert_array_equals(log, ['fetch-reject', 'next-microtask']);130}, "Already aborted signal rejects immediately");131promise_test(async t => {132 const controller = new AbortController();133 const signal = controller.signal;134 controller.abort();135 const request = new Request('../resources/data.json', {136 signal,137 method: 'POST',138 body: 'foo',139 headers: { 'Content-Type': 'text/plain' }140 });141 await fetch(request).catch(() => {});142 assert_true(request.bodyUsed, "Body has been used");143}, "Request is still 'used' if signal is aborted before fetching");144for (const bodyMethod of BODY_METHODS) {145 promise_test(async t => {146 const controller = new AbortController();147 const signal = controller.signal;148 const log = [];149 const response = await fetch('../resources/data.json', { signal });150 controller.abort();151 const bodyPromise = response[bodyMethod]();152 await Promise.all([153 bodyPromise.catch(() => log.push(`${bodyMethod}-reject`)),154 Promise.resolve().then(() => log.push('next-microtask'))155 ]);156 await promise_rejects(t, "AbortError", bodyPromise);157 assert_array_equals(log, [`${bodyMethod}-reject`, 'next-microtask']);158 }, `response.${bodyMethod}() rejects if already aborted`);159}160promise_test(async t => {161 await abortRequests();162 const controller = new AbortController();163 const signal = controller.signal;164 const stateKey = token();165 const abortKey = token();166 requestAbortKeys.push(abortKey);167 controller.abort();168 await fetch(`../resources/infinite-slow-response.py?stateKey=${stateKey}&abortKey=${abortKey}`, { signal }).catch(() => {});169 // I'm hoping this will give the browser enough time to (incorrectly) make the request170 // above, if it intends to.171 await fetch('../resources/data.json').then(r => r.json());172 const response = await fetch(`../resources/stash-take.py?key=${stateKey}`);173 const data = await response.json();174 assert_equals(data, null, "Request hasn't been made to the server");175}, "Already aborted signal does not make request");176promise_test(async t => {177 await abortRequests();178 const controller = new AbortController();179 const signal = controller.signal;180 controller.abort();181 const fetches = [];182 for (let i = 0; i < 3; i++) {183 const abortKey = token();184 requestAbortKeys.push(abortKey);185 fetches.push(186 fetch(`../resources/infinite-slow-response.py?${i}&abortKey=${abortKey}`, { signal })187 );188 }189 for (const fetchPromise of fetches) {190 await promise_rejects(t, "AbortError", fetchPromise);191 }192}, "Already aborted signal can be used for many fetches");193promise_test(async t => {194 await abortRequests();195 const controller = new AbortController();196 const signal = controller.signal;197 await fetch('../resources/data.json', { signal }).then(r => r.json());198 controller.abort();199 const fetches = [];200 for (let i = 0; i < 3; i++) {201 const abortKey = token();202 requestAbortKeys.push(abortKey);203 fetches.push(204 fetch(`../resources/infinite-slow-response.py?${i}&abortKey=${abortKey}`, { signal })205 );206 }207 for (const fetchPromise of fetches) {208 await promise_rejects(t, "AbortError", fetchPromise);209 }210}, "Signal can be used to abort other fetches, even if another fetch succeeded before aborting");211promise_test(async t => {212 await abortRequests();213 const controller = new AbortController();214 const signal = controller.signal;215 const stateKey = token();216 const abortKey = token();217 requestAbortKeys.push(abortKey);218 await fetch(`../resources/infinite-slow-response.py?stateKey=${stateKey}&abortKey=${abortKey}`, { signal });219 const beforeAbortResult = await fetch(`../resources/stash-take.py?key=${stateKey}`).then(r => r.json());220 assert_equals(beforeAbortResult, "open", "Connection is open");221 controller.abort();222 // The connection won't close immediately, but it should close at some point:223 const start = Date.now();224 while (true) {225 // Stop spinning if 10 seconds have passed226 if (Date.now() - start > 10000) throw Error('Timed out');227 const afterAbortResult = await fetch(`../resources/stash-take.py?key=${stateKey}`).then(r => r.json());228 if (afterAbortResult == 'closed') break;229 }230}, "Underlying connection is closed when aborting after receiving response");231promise_test(async t => {232 await abortRequests();233 const controller = new AbortController();234 const signal = controller.signal;235 const stateKey = token();236 const abortKey = token();237 requestAbortKeys.push(abortKey);238 const url = new URL(`../resources/infinite-slow-response.py?stateKey=${stateKey}&abortKey=${abortKey}`, location);239 url.hostname = urlHostname;240 await fetch(url, {241 signal,242 mode: 'no-cors'243 });244 const stashTakeURL = new URL(`../resources/stash-take.py?key=${stateKey}`, location);245 stashTakeURL.hostname = urlHostname;246 const beforeAbortResult = await fetch(stashTakeURL).then(r => r.json());247 assert_equals(beforeAbortResult, "open", "Connection is open");248 controller.abort();249 // The connection won't close immediately, but it should close at some point:250 const start = Date.now();251 while (true) {252 // Stop spinning if 10 seconds have passed253 if (Date.now() - start > 10000) throw Error('Timed out');254 const afterAbortResult = await fetch(stashTakeURL).then(r => r.json());255 if (afterAbortResult == 'closed') break;256 }257}, "Underlying connection is closed when aborting after receiving response - no-cors");258for (const bodyMethod of BODY_METHODS) {259 promise_test(async t => {260 await abortRequests();261 const controller = new AbortController();262 const signal = controller.signal;263 const stateKey = token();264 const abortKey = token();265 requestAbortKeys.push(abortKey);266 const response = await fetch(`../resources/infinite-slow-response.py?stateKey=${stateKey}&abortKey=${abortKey}`, { signal });267 const beforeAbortResult = await fetch(`../resources/stash-take.py?key=${stateKey}`).then(r => r.json());268 assert_equals(beforeAbortResult, "open", "Connection is open");269 const bodyPromise = response[bodyMethod]();270 controller.abort();271 await promise_rejects(t, "AbortError", bodyPromise);272 const start = Date.now();273 while (true) {274 // Stop spinning if 10 seconds have passed275 if (Date.now() - start > 10000) throw Error('Timed out');276 const afterAbortResult = await fetch(`../resources/stash-take.py?key=${stateKey}`).then(r => r.json());277 if (afterAbortResult == 'closed') break;278 }279 }, `Fetch aborted & connection closed when aborted after calling response.${bodyMethod}()`);280}281promise_test(async t => {282 await abortRequests();283 const controller = new AbortController();284 const signal = controller.signal;285 const stateKey = token();286 const abortKey = token();287 requestAbortKeys.push(abortKey);288 const response = await fetch(`../resources/infinite-slow-response.py?stateKey=${stateKey}&abortKey=${abortKey}`, { signal });289 const reader = response.body.getReader();290 controller.abort();291 await promise_rejects(t, "AbortError", reader.read());292 await promise_rejects(t, "AbortError", reader.closed);293 // The connection won't close immediately, but it should close at some point:294 const start = Date.now();295 while (true) {296 // Stop spinning if 10 seconds have passed297 if (Date.now() - start > 10000) throw Error('Timed out');298 const afterAbortResult = await fetch(`../resources/stash-take.py?key=${stateKey}`).then(r => r.json());299 if (afterAbortResult == 'closed') break;300 }301}, "Stream errors once aborted. Underlying connection closed.");302promise_test(async t => {303 await abortRequests();304 const controller = new AbortController();305 const signal = controller.signal;306 const stateKey = token();307 const abortKey = token();308 requestAbortKeys.push(abortKey);309 const response = await fetch(`../resources/infinite-slow-response.py?stateKey=${stateKey}&abortKey=${abortKey}`, { signal });310 const reader = response.body.getReader();311 await reader.read();312 controller.abort();313 await promise_rejects(t, "AbortError", reader.read());314 await promise_rejects(t, "AbortError", reader.closed);315 // The connection won't close immediately, but it should close at some point:316 const start = Date.now();317 while (true) {318 // Stop spinning if 10 seconds have passed319 if (Date.now() - start > 10000) throw Error('Timed out');320 const afterAbortResult = await fetch(`../resources/stash-take.py?key=${stateKey}`).then(r => r.json());321 if (afterAbortResult == 'closed') break;322 }323}, "Stream errors once aborted, after reading. Underlying connection closed.");324promise_test(async t => {325 await abortRequests();326 const controller = new AbortController();327 const signal = controller.signal;328 const response = await fetch(`../resources/empty.txt`, { signal });329 // Read whole response to ensure close signal has sent.330 await response.clone().text();331 const reader = response.body.getReader();332 controller.abort();333 const item = await reader.read();334 assert_true(item.done, "Stream is done");335}, "Stream will not error if body is empty. It's closed with an empty queue before it errors.");336promise_test(async t => {337 const controller = new AbortController();338 const signal = controller.signal;339 controller.abort();340 let cancelReason;341 const body = new ReadableStream({342 pull(controller) {343 controller.enqueue(new Uint8Array([42]));344 },345 cancel(reason) {346 cancelReason = reason;347 }348 });349 const fetchPromise = fetch('../resources/empty.txt', {350 body, signal,351 method: 'POST',352 headers: {353 'Content-Type': 'text/plain'354 }355 });356 assert_true(!!cancelReason, 'Cancel called sync');357 assert_equals(cancelReason.constructor, DOMException);358 assert_equals(cancelReason.name, 'AbortError');359 await promise_rejects(t, "AbortError", fetchPromise);360 const fetchErr = await fetchPromise.catch(e => e);361 assert_equals(cancelReason, fetchErr, "Fetch rejects with same error instance");362}, "Readable stream synchronously cancels with AbortError if aborted before reading");363test(() => {364 const controller = new AbortController();365 const signal = controller.signal;366 controller.abort();367 const request = new Request('.', { signal });368 const requestSignal = request.signal;369 const clonedRequest = request.clone();370 assert_equals(requestSignal, request.signal, "Original request signal the same after cloning");371 assert_true(request.signal.aborted, "Original request signal aborted");372 assert_not_equals(clonedRequest.signal, request.signal, "Cloned request has different signal");373 assert_true(clonedRequest.signal.aborted, "Cloned request signal aborted");374}, "Signal state is cloned");375test(() => {376 const controller = new AbortController();377 const signal = controller.signal;378 const request = new Request('.', { signal });379 const clonedRequest = request.clone();380 const log = [];381 request.signal.addEventListener('abort', () => log.push('original-aborted'));382 clonedRequest.signal.addEventListener('abort', () => log.push('clone-aborted'));383 controller.abort();384 assert_array_equals(log, ['clone-aborted', 'original-aborted'], "Abort events fired in correct order");385 assert_true(request.signal.aborted, 'Signal aborted');386 assert_true(clonedRequest.signal.aborted, 'Signal aborted');...
general.js
Source:general.js
1const BODY_METHODS = ['arrayBuffer', 'blob', 'formData', 'json', 'text'];2if (self.importScripts) {3 // Load scripts if being run from a worker4 importScripts(5 '/resources/testharness.js',6 '/common/utils.js'7 );8}9// This is used to close connections that weren't correctly closed during the tests,10// otherwise you can end up running out of HTTP connections.11let requestAbortKeys = [];12function abortRequests() {13 const keys = requestAbortKeys;14 requestAbortKeys = [];15 return Promise.all(16 keys.map(key => fetch(`../resources/stash-put.py?key=${key}&value=close`))17 );18}19// Add the global name to the test name20function contextualTestName(name) {21 return `${self.constructor.name}: ${name}`;22}23promise_test(async t => {24 const controller = new AbortController();25 const signal = controller.signal;26 controller.abort();27 const fetchPromise = fetch('../resources/data.json', { signal });28 await promise_rejects(t, "AbortError", fetchPromise);29}, contextualTestName("Aborting rejects with AbortError"));30promise_test(async t => {31 const controller = new AbortController();32 const signal = controller.signal;33 controller.abort();34 const url = new URL('../resources/data.json', location);35 url.hostname = 'www1.' + url.hostname;36 const fetchPromise = fetch(url, {37 signal,38 mode: 'no-cors'39 });40 await promise_rejects(t, "AbortError", fetchPromise);41}, contextualTestName("Aborting rejects with AbortError - no-cors"));42test(() => {43 const request = new Request('');44 assert_true(Boolean(request.signal), "Signal member is present & truthy");45 assert_equals(request.signal.constructor, AbortSignal);46}, contextualTestName("Request objects have a signal property"));47promise_test(async t => {48 const controller = new AbortController();49 const signal = controller.signal;50 controller.abort();51 const request = new Request('../resources/data.json', { signal });52 assert_true(Boolean(request.signal), "Signal member is present & truthy");53 assert_equals(request.signal.constructor, AbortSignal);54 assert_not_equals(request.signal, signal, 'Request has a new signal, not a reference');55 const fetchPromise = fetch(request);56 await promise_rejects(t, "AbortError", fetchPromise);57}, contextualTestName("Signal on request object"));58promise_test(async t => {59 const controller = new AbortController();60 const signal = controller.signal;61 controller.abort();62 const request = new Request('../resources/data.json', { signal });63 const requestFromRequest = new Request(request);64 const fetchPromise = fetch(requestFromRequest);65 await promise_rejects(t, "AbortError", fetchPromise);66}, contextualTestName("Signal on request object created from request object"));67promise_test(async t => {68 const controller = new AbortController();69 const signal = controller.signal;70 controller.abort();71 const request = new Request('../resources/data.json');72 const requestFromRequest = new Request(request, { signal });73 const fetchPromise = fetch(requestFromRequest);74 await promise_rejects(t, "AbortError", fetchPromise);75}, contextualTestName("Signal on request object created from request object, with signal on second request"));76promise_test(async t => {77 const controller = new AbortController();78 const signal = controller.signal;79 controller.abort();80 const request = new Request('../resources/data.json', { signal: new AbortController().signal });81 const requestFromRequest = new Request(request, { signal });82 const fetchPromise = fetch(requestFromRequest);83 await promise_rejects(t, "AbortError", fetchPromise);84}, contextualTestName("Signal on request object created from request object, with signal on second request overriding another"));85promise_test(async t => {86 const controller = new AbortController();87 const signal = controller.signal;88 controller.abort();89 const request = new Request('../resources/data.json', { signal });90 const fetchPromise = fetch(request, {method: 'POST'});91 await promise_rejects(t, "AbortError", fetchPromise);92}, contextualTestName("Signal retained after unrelated properties are overridden by fetch"));93promise_test(async t => {94 const controller = new AbortController();95 const signal = controller.signal;96 controller.abort();97 const request = new Request('../resources/data.json', { signal });98 const data = await fetch(request, { signal: null }).then(r => r.json());99 assert_equals(data.key, 'value', 'Fetch fully completes');100}, contextualTestName("Signal removed by setting to null"));101promise_test(async t => {102 const controller = new AbortController();103 const signal = controller.signal;104 controller.abort();105 const log = [];106 await Promise.all([107 fetch('../resources/data.json', { signal }).then(108 () => assert_unreached("Fetch must not resolve"),109 () => log.push('fetch-reject')110 ),111 Promise.resolve().then(() => log.push('next-microtask'))112 ]);113 assert_array_equals(log, ['fetch-reject', 'next-microtask']);114}, contextualTestName("Already aborted signal rejects immediately"));115promise_test(async t => {116 const controller = new AbortController();117 const signal = controller.signal;118 controller.abort();119 const request = new Request('../resources/data.json', {120 signal,121 method: 'POST',122 body: 'foo',123 headers: { 'Content-Type': 'text/plain' }124 });125 await fetch(request).catch(() => {});126 assert_true(request.bodyUsed, "Body has been used");127}, contextualTestName("Request is still 'used' if signal is aborted before fetching"));128for (const bodyMethod of BODY_METHODS) {129 promise_test(async t => {130 const controller = new AbortController();131 const signal = controller.signal;132 const log = [];133 const response = await fetch('../resources/data.json', { signal });134 controller.abort();135 const bodyPromise = response[bodyMethod]();136 await Promise.all([137 bodyPromise.catch(() => log.push(`${bodyMethod}-reject`)),138 Promise.resolve().then(() => log.push('next-microtask'))139 ]);140 await promise_rejects(t, "AbortError", bodyPromise);141 assert_array_equals(log, [`${bodyMethod}-reject`, 'next-microtask']);142 }, contextualTestName(`response.${bodyMethod}() rejects if already aborted`));143}144promise_test(async t => {145 await abortRequests();146 const controller = new AbortController();147 const signal = controller.signal;148 const stateKey = token();149 const abortKey = token();150 requestAbortKeys.push(abortKey);151 controller.abort();152 await fetch(`../resources/infinite-slow-response.py?stateKey=${stateKey}&abortKey=${abortKey}`, { signal }).catch(() => {});153 // I'm hoping this will give the browser enough time to (incorrectly) make the request154 // above, if it intends to.155 await fetch('../resources/data.json').then(r => r.json());156 const response = await fetch(`../resources/stash-take.py?key=${stateKey}`);157 const data = await response.json();158 assert_equals(data, null, "Request hasn't been made to the server");159}, contextualTestName("Already aborted signal does not make request"));160promise_test(async t => {161 await abortRequests();162 const controller = new AbortController();163 const signal = controller.signal;164 controller.abort();165 const fetches = [];166 for (let i = 0; i < 3; i++) {167 const abortKey = token();168 requestAbortKeys.push(abortKey);169 fetches.push(170 fetch(`../resources/infinite-slow-response.py?${i}&abortKey=${abortKey}`, { signal })171 );172 }173 for (const fetchPromise of fetches) {174 await promise_rejects(t, "AbortError", fetchPromise);175 }176}, contextualTestName("Already aborted signal can be used for many fetches"));177promise_test(async t => {178 await abortRequests();179 const controller = new AbortController();180 const signal = controller.signal;181 await fetch('../resources/data.json', { signal }).then(r => r.json());182 controller.abort();183 const fetches = [];184 for (let i = 0; i < 3; i++) {185 const abortKey = token();186 requestAbortKeys.push(abortKey);187 fetches.push(188 fetch(`../resources/infinite-slow-response.py?${i}&abortKey=${abortKey}`, { signal })189 );190 }191 for (const fetchPromise of fetches) {192 await promise_rejects(t, "AbortError", fetchPromise);193 }194}, contextualTestName("Signal can be used to abort other fetches, even if another fetch succeeded before aborting"));195promise_test(async t => {196 await abortRequests();197 const controller = new AbortController();198 const signal = controller.signal;199 const stateKey = token();200 const abortKey = token();201 requestAbortKeys.push(abortKey);202 await fetch(`../resources/infinite-slow-response.py?stateKey=${stateKey}&abortKey=${abortKey}`, { signal });203 const beforeAbortResult = await fetch(`../resources/stash-take.py?key=${stateKey}`).then(r => r.json());204 assert_equals(beforeAbortResult, "open", "Connection is open");205 controller.abort();206 // The connection won't close immediately, but it should close at some point:207 const start = Date.now();208 while (true) {209 // Stop spinning if 10 seconds have passed210 if (Date.now() - start > 10000) throw Error('Timed out');211 const afterAbortResult = await fetch(`../resources/stash-take.py?key=${stateKey}`).then(r => r.json());212 if (afterAbortResult == 'closed') break;213 }214}, contextualTestName("Underlying connection is closed when aborting after receiving response"));215promise_test(async t => {216 await abortRequests();217 const controller = new AbortController();218 const signal = controller.signal;219 const stateKey = token();220 const abortKey = token();221 requestAbortKeys.push(abortKey);222 const url = new URL(`../resources/infinite-slow-response.py?stateKey=${stateKey}&abortKey=${abortKey}`, location);223 url.hostname = 'www1.' + url.hostname;224 await fetch(url, {225 signal,226 mode: 'no-cors'227 });228 const stashTakeURL = new URL(`../resources/stash-take.py?key=${stateKey}`);229 stashTakeURL.hostname = 'www1.' + stashTakeURL.hostname;230 const beforeAbortResult = await fetch(stashTakeURL).then(r => r.json());231 assert_equals(beforeAbortResult, "open", "Connection is open");232 controller.abort();233 // The connection won't close immediately, but it should close at some point:234 const start = Date.now();235 while (true) {236 // Stop spinning if 10 seconds have passed237 if (Date.now() - start > 10000) throw Error('Timed out');238 const afterAbortResult = await fetch(stashTakeURL).then(r => r.json());239 if (afterAbortResult == 'closed') break;240 }241}, contextualTestName("Underlying connection is closed when aborting after receiving response - no-cors"));242for (const bodyMethod of BODY_METHODS) {243 promise_test(async t => {244 await abortRequests();245 const controller = new AbortController();246 const signal = controller.signal;247 const stateKey = token();248 const abortKey = token();249 requestAbortKeys.push(abortKey);250 const response = await fetch(`../resources/infinite-slow-response.py?stateKey=${stateKey}&abortKey=${abortKey}`, { signal });251 const beforeAbortResult = await fetch(`../resources/stash-take.py?key=${stateKey}`).then(r => r.json());252 assert_equals(beforeAbortResult, "open", "Connection is open");253 const bodyPromise = response[bodyMethod]();254 controller.abort();255 await promise_rejects(t, "AbortError", bodyPromise);256 const start = Date.now();257 while (true) {258 // Stop spinning if 10 seconds have passed259 if (Date.now() - start > 10000) throw Error('Timed out');260 const afterAbortResult = await fetch(`../resources/stash-take.py?key=${stateKey}`).then(r => r.json());261 if (afterAbortResult == 'closed') break;262 }263 }, contextualTestName(`Fetch aborted & connection closed when aborted after calling response.${bodyMethod}()`));264}265promise_test(async t => {266 await abortRequests();267 const controller = new AbortController();268 const signal = controller.signal;269 const stateKey = token();270 const abortKey = token();271 requestAbortKeys.push(abortKey);272 const response = await fetch(`../resources/infinite-slow-response.py?stateKey=${stateKey}&abortKey=${abortKey}`, { signal });273 const reader = response.body.getReader();274 controller.abort();275 await promise_rejects(t, "AbortError", reader.read());276 await promise_rejects(t, "AbortError", reader.closed);277 // The connection won't close immediately, but it should close at some point:278 const start = Date.now();279 while (true) {280 // Stop spinning if 10 seconds have passed281 if (Date.now() - start > 10000) throw Error('Timed out');282 const afterAbortResult = await fetch(`../resources/stash-take.py?key=${stateKey}`).then(r => r.json());283 if (afterAbortResult == 'closed') break;284 }285}, contextualTestName("Stream errors once aborted. Underlying connection closed."));286promise_test(async t => {287 await abortRequests();288 const controller = new AbortController();289 const signal = controller.signal;290 const stateKey = token();291 const abortKey = token();292 requestAbortKeys.push(abortKey);293 const response = await fetch(`../resources/infinite-slow-response.py?stateKey=${stateKey}&abortKey=${abortKey}`, { signal });294 const reader = response.body.getReader();295 await reader.read();296 controller.abort();297 await promise_rejects(t, "AbortError", reader.read());298 await promise_rejects(t, "AbortError", reader.closed);299 // The connection won't close immediately, but it should close at some point:300 const start = Date.now();301 while (true) {302 // Stop spinning if 10 seconds have passed303 if (Date.now() - start > 10000) throw Error('Timed out');304 const afterAbortResult = await fetch(`../resources/stash-take.py?key=${stateKey}`).then(r => r.json());305 if (afterAbortResult == 'closed') break;306 }307}, contextualTestName("Stream errors once aborted, after reading. Underlying connection closed."));308promise_test(async t => {309 await abortRequests();310 const controller = new AbortController();311 const signal = controller.signal;312 const response = await fetch(`../resources/empty.txt`, { signal });313 const reader = response.body.getReader();314 controller.abort();315 const item = await reader.read();316 assert_true(item.done, "Stream is done");317}, contextualTestName("Stream will not error if body is empty. It's closed with an empty queue before it errors."));318test(t => {319 const controller = new AbortController();320 const signal = controller.signal;321 controller.abort();322 let cancelReason;323 const body = new ReadableStream({324 pull(controller) {325 controller.enqueue(new Uint8Array([42]));326 },327 cancel(reason) {328 cancelReason = reason;329 }330 });331 fetch('../resources/empty.txt', {332 body, signal,333 method: 'POST',334 headers: {335 'Content-Type': 'text/plain'336 }337 });338 assert_true(!!cancelReason, 'Cancel called sync');339 assert_equals(cancelReason.constructor, DOMException);340 assert_equals(cancelReason.name, 'AbortError');...
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3wpt.abortTest('1234567890', function(err, data) {4 if (err) {5 console.log('Error: ' + err);6 } else {7 console.log(data);8 }9});10var wpt = require('webpagetest');11var wpt = new WebPageTest('www.webpagetest.org');12wpt.abortTest('1234567890', function(err, data) {13 if (err) {14 console.log('Error: ' + err);15 } else {16 console.log(data);17 }18});19var wpt = require('webpagetest');20var wpt = new WebPageTest('www.webpagetest.org');21wpt.getLocations(function(err, data) {22 if (err) {23 console.log('Error: ' + err);24 } else {25 console.log(data);26 }27});28var wpt = require('webpagetest');29var wpt = new WebPageTest('www.webpagetest.org');30wpt.getTesters(function(err, data) {31 if (err) {32 console.log('Error: ' + err);33 } else {34 console.log(data);35 }36});37var wpt = require('webpagetest');38var wpt = new WebPageTest('www.webpagetest.org');39wpt.getTesters(function(err, data) {40 if (err) {41 console.log('Error: ' + err);42 } else {43 console.log(data);44 }45});46var wpt = require('webpagetest');47var wpt = new WebPageTest('www.webpagetest.org');48wpt.getTesters(function(err, data) {49 if (err) {50 console.log('Error: ' + err);51 } else {52 console.log(data);53 }54});55var wpt = require('webpagetest');56var wpt = new WebPageTest('www.webpagetest.org');
Using AI Code Generation
1var wpt = require('wpt');2var wptObj = new wpt('API_KEY');3wptObj.abortKey('test_key', function(err, data) {4 if (err) {5 console.log(err);6 } else {7 console.log(data);8 }9});
Using AI Code Generation
1wpt.abortKey('yourAbortKey');2const WebPageTest = require('webpagetest');3const wpt = new WebPageTest('www.webpagetest.org', 'API_KEY');4 if (err) return console.error(err);5 const testId = data.data.testId;6 wpt.getTestResults(testId, (err, data) => {7 if (err) return console.error(err);8 console.log(data);9 });10});11const WebPageTest = require('webpagetest');12const wpt = new WebPageTest('www.webpagetest.org', 'API_KEY');13const options = {14 videoParams: {15 },16 lighthouseConfig: {17 settings: {18 throttling: {19 },20 },21 },22};23 if (err) return console.error(err);24 const testId = data.data.testId;25 wpt.getTestResults(testId, (err, data) => {26 if (err) return console.error(err);27 console.log(data);28 });29});30const WebPageTest = require('webpagetest');31const wpt = new WebPageTest('www.webpagetest.org', 'API_KEY');
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org', 'A.5c5a9a9d8a5f1b0c1a0f3b5e2f3a3a3');3wpt.abortTest('130412_2Q_3d3e0b4c4f4a8f4c4d0b7b9e9e9f8e8', function(err, data) {4 if (err) {5 console.log(err);6 } else {7 console.log(data);8 }9});
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!!