Best JavaScript code snippet using wpt
blob_url.test.ts
Source:blob_url.test.ts
1import * as assert from "assert";2import { BlobURL } from "../lib/BlobURL";3import { BlockBlobURL } from "../lib/BlockBlobURL";4import { ContainerURL } from "../lib/ContainerURL";5import {6 BlobType, LeaseDurationType, LeaseStateType, LeaseStatusType, ListBlobsIncludeItem7} from "../lib/generated/models";8import { getBSU, getUniqueName, sleep } from "./utils";9describe("BlobURL", () => {10 const serviceURL = getBSU();11 let containerName: string = getUniqueName("container");12 let containerURL = ContainerURL.fromServiceURL(serviceURL, containerName);13 let blobName: string = getUniqueName("blob");14 let blobURL = BlobURL.fromContainerURL(containerURL, blobName);15 let blockBlobURL = BlockBlobURL.fromBlobURL(blobURL);16 const content = "Hello World";17 beforeEach(async () => {18 containerName = getUniqueName("container");19 containerURL = ContainerURL.fromServiceURL(serviceURL, containerName);20 await containerURL.create();21 blobName = getUniqueName("blob");22 blobURL = BlobURL.fromContainerURL(containerURL, blobName);23 blockBlobURL = BlockBlobURL.fromBlobURL(blobURL);24 await blockBlobURL.upload(content, content.length);25 });26 afterEach(async () => {27 await containerURL.delete();28 });29 it("download with with default parameters", async () => {30 const result = await blobURL.download();31 // TODO: when will bodyAsText has value? It's undefined here32 assert.deepStrictEqual(33 result.readableStreamBody!.read(content.length).toString(),34 content35 );36 });37 it("download all parameters set", async () => {38 const result = await blobURL.download({39 range: `bytes=${0}-${0}`,40 rangeGetContentMD5: true41 });42 // TODO: when will bodyAsText has value? It's undefined here43 assert.deepStrictEqual(44 result.readableStreamBody!.read(1).toString(),45 content[0]46 );47 });48 it("setMetadata with new metadata set", async () => {49 const metadata = {50 a: "a",51 b: "b"52 };53 await blobURL.setMetadata({ metadata });54 const result = await blobURL.getProperties();55 assert.deepStrictEqual(result.metadata, metadata);56 });57 it("setMetadata with cleaning up metadata", async () => {58 const metadata = {59 a: "a",60 b: "b"61 };62 await blobURL.setMetadata({ metadata });63 const result = await blobURL.getProperties();64 assert.deepStrictEqual(result.metadata, metadata);65 await blobURL.setMetadata();66 const result2 = await blobURL.getProperties();67 assert.deepStrictEqual(result2.metadata, {});68 });69 it("setHTTPHeaders with default parameters", async () => {70 await blobURL.setHTTPHeaders({});71 const result = await blobURL.getProperties();72 assert.deepStrictEqual(result.blobType, BlobType.BlockBlob);73 assert.ok(result.lastModified);74 assert.deepStrictEqual(result.metadata, {});75 assert.ok(!result.cacheControl);76 assert.ok(!result.contentType);77 assert.ok(!result.contentMD5);78 assert.ok(!result.contentEncoding);79 assert.ok(!result.contentLanguage);80 assert.ok(!result.contentDisposition);81 });82 it("setHTTPHeaders with all parameters set", async () => {83 const headers = {84 blobCacheControl: "blobCacheControl",85 blobContentDisposition: "blobContentDisposition",86 blobContentEncoding: "blobContentEncoding",87 blobContentLanguage: "blobContentLanguage",88 blobContentMD5: new Buffer("blobContentMD5", "base64"),89 blobContentType: "blobContentType"90 };91 await blobURL.setHTTPHeaders({92 blobHTTPHeaders: headers93 });94 const result = await blobURL.getProperties();95 assert.deepStrictEqual(result.blobType, BlobType.BlockBlob);96 assert.ok(result.lastModified);97 assert.deepStrictEqual(result.metadata, {});98 assert.deepStrictEqual(result.cacheControl, headers.blobCacheControl);99 assert.deepStrictEqual(result.contentType, headers.blobContentType);100 assert.deepStrictEqual(result.contentMD5, headers.blobContentMD5);101 assert.deepStrictEqual(result.contentEncoding, headers.blobContentEncoding);102 assert.deepStrictEqual(result.contentLanguage, headers.blobContentLanguage);103 assert.deepStrictEqual(104 result.contentDisposition,105 headers.blobContentDisposition106 );107 });108 it("acquireLease", async () => {109 const guid = "ca761232ed4211cebacd00aa0057b223";110 const duration = 30;111 await blobURL.acquireLease(guid, duration);112 const result = await blobURL.getProperties();113 assert.equal(result.leaseDuration, LeaseDurationType.Fixed);114 assert.equal(result.leaseState, LeaseStateType.Leased);115 assert.equal(result.leaseStatus, LeaseStatusType.Locked);116 await blobURL.releaseLease(guid);117 });118 it("releaseLease", async () => {119 const guid = "ca761232ed4211cebacd00aa0057b223";120 const duration = -1;121 await blobURL.acquireLease(guid, duration);122 const result = await blobURL.getProperties();123 assert.equal(result.leaseDuration, LeaseDurationType.Infinite);124 assert.equal(result.leaseState, LeaseStateType.Leased);125 assert.equal(result.leaseStatus, LeaseStatusType.Locked);126 await blobURL.releaseLease(guid);127 });128 it("renewLease", async () => {129 const guid = "ca761232ed4211cebacd00aa0057b223";130 const duration = 15;131 await blobURL.acquireLease(guid, duration);132 const result = await blobURL.getProperties();133 assert.equal(result.leaseDuration, LeaseDurationType.Fixed);134 assert.equal(result.leaseState, LeaseStateType.Leased);135 assert.equal(result.leaseStatus, LeaseStatusType.Locked);136 await sleep(16 * 1000);137 const result2 = await blobURL.getProperties();138 assert.ok(!result2.leaseDuration);139 assert.equal(result2.leaseState, LeaseStateType.Expired);140 assert.equal(result2.leaseStatus, LeaseStatusType.Unlocked);141 await blobURL.renewLease(guid);142 const result3 = await blobURL.getProperties();143 assert.equal(result3.leaseDuration, LeaseDurationType.Fixed);144 assert.equal(result3.leaseState, LeaseStateType.Leased);145 assert.equal(result3.leaseStatus, LeaseStatusType.Locked);146 await blobURL.releaseLease(guid);147 });148 it("changeLease", async () => {149 const guid = "ca761232ed4211cebacd00aa0057b223";150 const duration = 15;151 await blobURL.acquireLease(guid, duration);152 const result = await blobURL.getProperties();153 assert.equal(result.leaseDuration, LeaseDurationType.Fixed);154 assert.equal(result.leaseState, LeaseStateType.Leased);155 assert.equal(result.leaseStatus, LeaseStatusType.Locked);156 const newGuid = "3c7e72ebb4304526bc53d8ecef03798f";157 await blobURL.changeLease(guid, newGuid);158 await blobURL.getProperties();159 await blobURL.releaseLease(newGuid);160 });161 it("breakLease", async () => {162 const guid = "ca761232ed4211cebacd00aa0057b223";163 const duration = 15;164 await blobURL.acquireLease(guid, duration);165 const result = await blobURL.getProperties();166 assert.equal(result.leaseDuration, LeaseDurationType.Fixed);167 assert.equal(result.leaseState, LeaseStateType.Leased);168 assert.equal(result.leaseStatus, LeaseStatusType.Locked);169 await blobURL.breakLease(3);170 const result2 = await blobURL.getProperties();171 assert.ok(!result2.leaseDuration);172 assert.equal(result2.leaseState, LeaseStateType.Breaking);173 assert.equal(result2.leaseStatus, LeaseStatusType.Locked);174 await sleep(3 * 1000);175 const result3 = await blobURL.getProperties();176 assert.ok(!result3.leaseDuration);177 assert.equal(result3.leaseState, LeaseStateType.Broken);178 assert.equal(result3.leaseStatus, LeaseStatusType.Unlocked);179 });180 it("delete", async () => {181 await blobURL.delete();182 });183 // The following code illustrates deleting a snapshot after creating one184 it("delete snapshot", async () => {185 const result = await blobURL.createSnapshot();186 assert.ok(result.snapshot);187 const blobSnapshotURL = blobURL.withSnapshot(result.snapshot!);188 await blobSnapshotURL.getProperties();189 await blobSnapshotURL.delete();190 await blobURL.delete();191 const result2 = await containerURL.listBlobFlatSegment(undefined, {192 include: [ListBlobsIncludeItem.Snapshots]193 });194 // Verify that the snapshot is deleted195 assert.equal(result2.segment.blobItems!.length, 0);196 });197 it("createSnapshot", async () => {198 const result = await blobURL.createSnapshot();199 assert.ok(result.snapshot);200 const blobSnapshotURL = blobURL.withSnapshot(result.snapshot!);201 await blobSnapshotURL.getProperties();202 const result3 = await containerURL.listBlobFlatSegment(undefined, {203 include: [ListBlobsIncludeItem.Snapshots]204 });205 // As a snapshot doesn't have leaseStatus and leaseState properties but origin blob has,206 // let assign them to undefined both for other properties' easy comparison207 // tslint:disable-next-line:max-line-length208 result3.segment.blobItems![0].properties.leaseState = result3.segment.blobItems![1].properties.leaseState = undefined;209 // tslint:disable-next-line:max-line-length210 result3.segment.blobItems![0].properties.leaseStatus = result3.segment.blobItems![1].properties.leaseStatus = undefined;211 assert.deepStrictEqual(212 result3.segment.blobItems![0].properties,213 result3.segment.blobItems![1].properties214 );215 assert.ok(216 result3.segment.blobItems![0].snapshot ||217 result3.segment.blobItems![1].snapshot218 );219 });220 it("undelete", async () => {221 const properties = await serviceURL.getProperties();222 if (!properties.deleteRetentionPolicy!.enabled) {223 await serviceURL.setProperties({224 deleteRetentionPolicy: {225 days: 7,226 enabled: true227 }228 });229 await sleep(15 * 1000);230 }231 await blobURL.delete();232 const result = await containerURL.listBlobFlatSegment(undefined, {233 include: [ListBlobsIncludeItem.Deleted]234 });235 assert.ok(result.segment.blobItems![0].deleted);236 await blobURL.undelete();237 const result2 = await containerURL.listBlobFlatSegment(undefined, {238 include: [ListBlobsIncludeItem.Deleted]239 });240 assert.ok(!result2.segment.blobItems![0].deleted);241 });242 it("startCopyFromURL", async () => {243 const newBlobURL = BlobURL.fromContainerURL(244 containerURL,245 getUniqueName("copiedblob")246 );247 const result = await newBlobURL.startCopyFromURL(blobURL.url);248 assert.ok(result.copyId);249 const properties1 = await blobURL.getProperties();250 const properties2 = await newBlobURL.getProperties();251 assert.deepStrictEqual(properties1.contentMD5, properties2.contentMD5);252 assert.deepStrictEqual(properties2.copyId, result.copyId);253 assert.deepStrictEqual(properties2.copySource, blobURL.url);254 });255 it("abortCopyFromURL should failed for a completed copy operation", async () => {256 const newBlobURL = BlobURL.fromContainerURL(257 containerURL,258 getUniqueName("copiedblob")259 );260 const result = await newBlobURL.startCopyFromURL(blobURL.url);261 assert.ok(result.copyId);262 sleep(1 * 1000);263 try {264 await newBlobURL.abortCopyFromURL(result.copyId!);265 assert.fail(266 "AbortCopyFromURL should be failed and throw exception for an completed copy operation."267 );268 } catch (err) {269 assert.ok(true);270 }271 });272 it("setTier", async () => {273 // TODO: Should add test cases for setTier274 assert.fail("Should add test cases for setTier");275 });...
download_manager.js
Source:download_manager.js
1/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */2/* Copyright 2013 Mozilla Foundation3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16/* globals URL, PDFJS */17'use strict';18var DownloadManager = (function DownloadManagerClosure() {19 function download(blobUrl, filename) {20 var a = document.createElement('a');21 if (a.click) {22 // Use a.click() if available. Otherwise, Chrome might show23 // "Unsafe JavaScript attempt to initiate a navigation change24 // for frame with URL" and not open the PDF at all.25 // Supported by (not mentioned = untested):26 // - Firefox 6 - 19 (4- does not support a.click, 5 ignores a.click)27 // - Chrome 19 - 26 (18- does not support a.click)28 // - Opera 9 - 12.1529 // - Internet Explorer 6 - 1030 // - Safari 6 (5.1- does not support a.click)31 a.href = blobUrl;32 a.target = '_parent';33 // Use a.download if available. This increases the likelihood that34 // the file is downloaded instead of opened by another PDF plugin.35 if ('download' in a) {36 a.download = filename;37 }38 // <a> must be in the document for IE and recent Firefox versions.39 // (otherwise .click() is ignored)40 (document.body || document.documentElement).appendChild(a);41 a.click();42 a.parentNode.removeChild(a);43 } else {44 if (window.top === window &&45 blobUrl.split('#')[0] === window.location.href.split('#')[0]) {46 // If _parent == self, then opening an identical URL with different47 // location hash will only cause a navigation, not a download.48 var padCharacter = blobUrl.indexOf('?') === -1 ? '?' : '&';49 blobUrl = blobUrl.replace(/#|$/, padCharacter + '$&');50 }51 window.open(blobUrl, '_parent');52 }53 }54 function DownloadManager() {}55 DownloadManager.prototype = {56 downloadUrl: function DownloadManager_downloadUrl(url, filename) {57 if (!PDFJS.isValidUrl(url, true)) {58 return; // restricted/invalid URL59 }60 download(url + '#pdfjs.action=download', filename);61 },62 downloadData: function DownloadManager_downloadData(data, filename,63 contentType) {64 if (navigator.msSaveBlob) { // IE10 and above65 return navigator.msSaveBlob(new Blob([data], { type: contentType }),66 filename);67 }68 var blobUrl = PDFJS.createObjectURL(data, contentType);69 download(blobUrl, filename);70 },71 download: function DownloadManager_download(blob, url, filename) {72 if (!URL) {73 // URL.createObjectURL is not supported74 this.downloadUrl(url, filename);75 return;76 }77 if (navigator.msSaveBlob) {78 // IE10 / IE1179 if (!navigator.msSaveBlob(blob, filename)) {80 this.downloadUrl(url, filename);81 }82 return;83 }84 var blobUrl = URL.createObjectURL(blob);85 download(blobUrl, filename);86 }87 };88 return DownloadManager;...
browser_blobURLIsolation.js
Source:browser_blobURLIsolation.js
1/**2 * Bug 1264573 - A test case for blob url isolation.3 */4const TEST_PAGE = "http://mochi.test:8888/browser/browser/components/" +5 "originattributes/test/browser/file_firstPartyBasic.html";6const SCRIPT_WORKER_BLOBIFY = "worker_blobify.js";7const SCRIPT_WORKER_DEBLOBIFY = "worker_deblobify.js";8function page_blobify(browser, input) {9 return ContentTask.spawn(browser, input, function(input) {10 return { blobURL: content.URL.createObjectURL(new content.Blob([input])) };11 });12}13function page_deblobify(browser, blobURL) {14 return ContentTask.spawn(browser, blobURL, function* (blobURL) {15 if ("error" in blobURL) {16 return blobURL;17 }18 blobURL = blobURL.blobURL;19 function blobURLtoBlob(blobURL) {20 return new content.Promise(function (resolve) {21 let xhr = new content.XMLHttpRequest();22 xhr.open("GET", blobURL, true);23 xhr.onload = function () {24 resolve(xhr.response);25 };26 xhr.onerror = function () {27 resolve("xhr error");28 };29 xhr.responseType = "blob";30 xhr.send();31 });32 }33 function blobToString(blob) {34 return new content.Promise(function (resolve) {35 let fileReader = new content.FileReader();36 fileReader.onload = function () {37 resolve(fileReader.result);38 };39 fileReader.readAsText(blob);40 });41 }42 let blob = yield blobURLtoBlob(blobURL);43 if (blob == "xhr error") {44 return "xhr error";45 }46 return yield blobToString(blob);47 });48}49function workerIO(browser, scriptFile, message) {50 return ContentTask.spawn(browser, {scriptFile, message}, function* (args) {51 let worker = new content.Worker(args.scriptFile);52 let promise = new content.Promise(function(resolve) {53 let listenFunction = function(event) {54 worker.removeEventListener("message", listenFunction, false);55 worker.terminate();56 resolve(event.data);57 };58 worker.addEventListener("message", listenFunction, false);59 });60 worker.postMessage(args.message);61 return yield promise;62 });63}64let worker_blobify = (browser, input) => workerIO(browser, SCRIPT_WORKER_BLOBIFY, input);65let worker_deblobify = (browser, blobURL) => workerIO(browser, SCRIPT_WORKER_DEBLOBIFY, blobURL);66function doTest(blobify, deblobify) {67 let blobURL = null;68 return function* (browser) {69 if (blobURL === null) {70 let input = Math.random().toString();71 blobURL = yield blobify(browser, input);72 return input;73 }74 let result = yield deblobify(browser, blobURL);75 blobURL = null;76 return result;77 }78}79let tests = [];80for (let blobify of [page_blobify, worker_blobify]) {81 for (let deblobify of [page_deblobify, worker_deblobify]) {82 tests.push(doTest(blobify, deblobify));83 }84}...
Using AI Code Generation
1function testBlobURL() {2 var blob = new Blob(['<a id="a"><b id="b">hey!</b></a>'], {type: 'text/html'});3 var url = URL.createObjectURL(blob);4 var xhr = new XMLHttpRequest();5 xhr.open('GET', url);6 xhr.onload = function() {7 var html = xhr.response;8 postMessage(html);9 };10 xhr.send();11}12function testFetch() {13 var blob = new Blob(['<a id="a"><b id="b">hey!</b></a>'], {type: 'text/html'});14 var url = URL.createObjectURL(blob);15 fetch(url).then(function(response) {16 return response.text();17 }).then(function(html) {18 postMessage(html);19 });20}21function testFetch() {22 var blob = new Blob(['<a id="a"><b id="b">hey!</b></a>'], {type: 'text/html'});23 var url = URL.createObjectURL(blob);24 fetch(url).then(function(response) {25 return response.text();26 }).then(function(html) {27 postMessage(html);28 });29}30function testFetch() {31 var blob = new Blob(['<a id="a"><b id="b">hey!</b></a>'], {type: 'text/html'});32 var url = URL.createObjectURL(blob);33 fetch(url).then(function(response) {34 return response.text();35 }).then(function(html) {36 postMessage(html);37 });38}39function testFetch() {40 var blob = new Blob(['<a id="a"><b id="b">hey!</b></a>'], {type: 'text/html'});41 var url = URL.createObjectURL(blob);42 fetch(url).then(function(response) {43 return response.text();44 }).then(function(html) {45 postMessage(html);46 });47}48function testFetch() {49 var blob = new Blob(['<a id="a"><b id="b">hey!</b></a>'],
Using AI Code Generation
1var blob = new Blob(['<html><body><p>hello world</p></body></html>'], {type: 'text/html'});2var blobURL = URL.createObjectURL(blob);3var wpt = new WebPageTest('www.webpagetest.org', 'A.8d4a4f9a9b9d7e8c8e0a7d3b3c3a7a3a');4wpt.runTest(blobURL, {5}, function(err, data) {6 if (err) return console.error(err);7 console.log(data);8});9var wpt = new WebPageTest('www.webpagetest.org', 'A.8d4a4f9a9b9d7e8c8e0a7d3b3c3a7a3a');10wpt.runTest('test.html', {11}, function(err, data) {12 if (err) return console.error(err);13 console.log(data);14});15### WebPageTest(host, apiKey, [options])16 * `timeout` - The timeout (in
Using AI Code Generation
1self.addEventListener('install', function(event) {2 console.log('installing');3 event.waitUntil(4 caches.open('v1').then(function(cache) {5 return cache.addAll([6 ]);7 })8 );9});10self.addEventListener('fetch', function(event) {11 console.log('fetching');12 event.respondWith(13 caches.match(event.request).then(function(response) {14 return response || fetch(event.request);15 })16 );17});18self.addEventListener('activate', function(event) {19 console.log('activating');20 var cacheWhitelist = ['v2'];21 event.waitUntil(22 caches.keys().then(function(cacheNames) {23 return Promise.all(24 cacheNames.map(function(cacheNames) {25 if (cacheWhitelist.indexOf(cacheNames) === -1) {26 return caches.delete(cacheNames);27 }28 })29 );30 })31 );32});
Using AI Code Generation
1var blobURL = new URL("resources/echo.py?content=PASS", location.href).href;2var blob = new Blob([`importScripts("${blobURL}");`], {type: "text/javascript"});3var worker = new Worker(URL.createObjectURL(blob));4worker.onmessage = t.step_func_done(function(e) {5 assert_equals(e.data, "PASS");6});
Using AI Code Generation
1var texturize = require('./texturize.js');2var text = 'This is a test of the emergency broadcast system. This is only a test.';3console.log(texturize(text));4var texturize_regex = require('./texturize_regex.js');5var text = 'This is a test of the emergency broadcast system. This is only a test.';6console.log(texturize_regex(text));7var texturize_regex = require('./texturize_regex.js');8var text = 'This is a test of the emergency broadcast system. This is only a test.';9console.log(texturize_regex(text));10var texturize_regex = require('./texturize_regex.js');11var text = 'This is a test of the emergency broadcast system. This is only a test.';12console.log(texturize_regex(text));13var texturize_regex = require('./texturize_regex.js');14var text = 'This is a test of the emergency broadcast system. This is only a test.';15console.log(texturize_regex(text));16var texturize_regex = require('./texturize_regex.js');17var text = 'This is a test of the emergency broadcast system. This is only a test.';18console.log(texturize_regex(text));19var texturize_regex = require('./texturize_regex.js');20var text = 'This is a test of the emergency broadcast system. This is only a test.';21console.log(texturize_regex(text));22var texturize_regex = require('./texturize_regex.js');23var text = 'This is a test of the emergency broadcast system. This is only a test.';24console.log(texturize_regex(text));25var texturize_regex = require('./texturize_regex.js');26var text = 'This is a test of the emergency broadcast system. This is only a test.';27console.log(texturize_regex(text));
Using AI Code Generation
1var wpt = require('webpagetest');2var options = {key: 'A.9a9f7b8a0a0a0a0a0a0a0a0a0a0a0a0a'};3var webpagetest = new wpt('www.webpagetest.org', options);4 console.log(data);5 console.log(err);6});7var wpt = require('webpagetest');8var options = {key: 'A.9a9f7b8a0a0a0a0a0a0a0a0a0a0a0a0a'};9var webpagetest = new wpt('www.webpagetest.org', options);10 console.log(data);11 console.log(err);12});13var wpt = require('webpagetest');14var options = {key: 'A.9a9f7b8a0a0a0a0a0a0a0a0a0a0a0a0a'};15var webpagetest = new wpt('www.webpagetest.org', options);16 console.log(data);17 console.log(err);18});19var wpt = require('webpagetest');20var options = {key: 'A.9a9f7b8a0a0a0a0a0a0a0a0a0a0a0a0a'};21var webpagetest = new wpt('www.webpagetest.org', options);22 console.log(data);23 console.log(err);24});25var wpt = require('webpagetest');26var options = {key: 'A.9a9f7b8a0a0a0a0a0a0a0a0a0a0a0a0a'};27var webpagetest = new wpt('www
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!!