You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/AdminTemplateMultipartPutBe...

72 lines
2.1 KiB
JavaScript

import { putMultipartPart } from '../public/cloud/admin-template-upload.js';
if (typeof globalThis.URL !== 'function') {
globalThis.URL = class URL {
constructor(value) {
const match = /^(https?):\/\/([^/@#]+)([^#]*)$/.exec(String(value));
if (!match) throw new TypeError('Invalid URL');
this.protocol = `${match[1]}:`;
this.hostname = match[2].split(':')[0];
this.username = '';
this.password = '';
this.hash = '';
this.href = String(value);
}
};
}
class FakeUpload {
addEventListener(name, callback) { this[name] = callback; }
}
class FakeRequest {
static etag = '"part-etag"';
static last = null;
constructor() {
this.upload = new FakeUpload();
this.listeners = {};
this.status = 200;
FakeRequest.last = this;
}
open(method, url, async) { Object.assign(this, { method, url, async }); }
addEventListener(name, callback) { this.listeners[name] = callback; }
getResponseHeader(name) {
return name.toLowerCase() === 'etag' ? FakeRequest.etag : null;
}
send(blob) {
this.blob = blob;
this.upload.progress?.({ lengthComputable: true, loaded: blob.size,
total: blob.size });
this.listeners.load?.();
}
abort() { this.listeners.abort?.(); }
}
let progress = 0;
const etag = await putMultipartPart(
'https://objects.example.test/bucket/object?partNumber=1&uploadId=test',
{ size: 4 }, loaded => { progress = loaded; }, FakeRequest
);
if (etag !== '"part-etag"' || FakeRequest.last.method !== 'PUT'
|| FakeRequest.last.withCredentials !== false
|| FakeRequest.last.blob.size !== 4 || progress !== 4) {
throw new Error('Direct multipart PUT changed its credential or progress boundary.');
}
FakeRequest.etag = '';
try {
await putMultipartPart(
'https://objects.example.test/bucket/object?partNumber=2&uploadId=test',
{ size: 4 }, null, FakeRequest
);
throw new Error('A response without an exposed ETag was accepted.');
} catch (error) {
if (!String(error.message).includes('ExposeHeaders')) throw error;
}
(globalThis.print || console.log)(
'Administrator direct multipart PUT behavior passed.'
);