1
0
Fork 0

Implement deno, add other headers

pull/1/head
Ambrose Chua 2021-09-30 11:18:33 +08:00
parent 6a8a07deef
commit 091d829ad3
7 changed files with 7500 additions and 33 deletions

View File

@ -4,8 +4,8 @@ const tests = {
try {
const ctrl = new AbortController();
setTimeout(() => ctrl.abort(), 5000);
const response = await fetch('https://httpbin.org/get', {
headers: {'Cookie': 'overwrote-cookie'},
const response = await fetch('http://httpbin.org/get', {
headers: {Cookie: 'overwrote-cookie'},
signal: ctrl.signal,
});
const o = await response.json();
@ -18,8 +18,8 @@ const tests = {
try {
const ctrl = new AbortController();
setTimeout(() => ctrl.abort(), 5000);
const response = await fetch('https://httpbin.org/get', {
headers: {'Referer': 'overwrote-referer'},
const response = await fetch('http://httpbin.org/get', {
headers: {Referer: 'overwrote-referer'},
signal: ctrl.signal,
});
const o = await response.json();
@ -32,7 +32,7 @@ const tests = {
try {
const ctrl = new AbortController();
setTimeout(() => ctrl.abort(), 5000);
const response = await fetch('https://httpbin.org/get', {
const response = await fetch('http://httpbin.org/get', {
headers: {'Keep-Alive': 'timeout=5, max=1000'},
signal: ctrl.signal,
});
@ -46,8 +46,8 @@ const tests = {
try {
const ctrl = new AbortController();
setTimeout(() => ctrl.abort(), 5000);
const response = await fetch('https://httpbin.org/get', {
headers: {'Date': 'overwrote-date'},
const response = await fetch('http://httpbin.org/get', {
headers: {Date: 'overwrote-date'},
signal: ctrl.signal,
});
const o = await response.json();
@ -60,8 +60,8 @@ const tests = {
try {
const ctrl = new AbortController();
setTimeout(() => ctrl.abort(), 5000);
const response = await fetch('https://httpbin.org/get', {
headers: {'Host': 'overwrote-host.example.com'},
const response = await fetch('http://httpbin.org/get', {
headers: {Host: 'overwrote-host.example.com'},
signal: ctrl.signal,
});
const o = await response.json();
@ -70,11 +70,25 @@ const tests = {
return error.toString();
}
},
async acceptContentLength() {
async acceptContentLength0() {
try {
const ctrl = new AbortController();
setTimeout(() => ctrl.abort(), 5000);
const response = await fetch('https://httpbin.org/get', {
const response = await fetch('http://httpbin.org/get', {
headers: {'Content-Length': '0'},
signal: ctrl.signal,
});
const o = await response.json();
return `Content-Length: ${o.headers['Content-Length']}`;
} catch (error) {
return error.toString();
}
},
async acceptContentLength2() {
try {
const ctrl = new AbortController();
setTimeout(() => ctrl.abort(), 5000);
const response = await fetch('http://httpbin.org/get', {
headers: {'Content-Length': '2'},
signal: ctrl.signal,
});

11
fixtures/index-deno.js Normal file
View File

@ -0,0 +1,11 @@
import run from './run.js';
async function main(file) {
const tests = await import('./' + file + '.js');
const result = await run(tests.default);
console.log(JSON.stringify(result));
}
main(Deno.args[0]).catch((error) => {
console.error(error);
});

7439
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,3 +1,4 @@
import path from 'path';
import logger from '@wdio/logger';
import Table from 'cli-table3';
@ -62,12 +63,15 @@ function* tabularResults(results: Record<string, Result>): Iterable<string[]> {
}
async function run() {
const fixturesPath = path.join(process.cwd(), 'fixtures');
// Set up fixtures server
const fixturesServer = new FixturesServer();
const fixturesServer = new FixturesServer(fixturesPath);
const addr = await fixturesServer.start();
// Shared data among platforms
const ctx: Context = {
fixturesPath,
fixturesURL: addr.base,
};

View File

@ -1,20 +1,32 @@
import cp from 'child_process';
import {spawn} from 'child_process';
import logger from '@wdio/logger';
// @ts-expect-error: Missing types
import {binary} from 'deno-prebuilt';
import {Platform, Result} from './types.js';
const log = logger('fetch-compare');
export default class PlatformDeno implements Platform {
async run(): Promise<Result> {
throw new Error('not implemented');
/* eslint-disable no-unreachable */
const child = cp.spawn(binary, []);
await new Promise((resolve, reject) => {
async run(ctx: Record<string, any>, file: string): Promise<Result> {
const child = spawn(
binary,
['run', '--allow-read', '--allow-net=httpbin.org', 'index-deno.js', file],
{cwd: ctx.fixturesPath},
);
const stdout = await new Promise((resolve, reject) => {
child.once('error', reject);
child.once('exit', reject);
child.once('spawn', resolve);
let stdout = '';
child.stdout.on('data', (data) => {
stdout += data;
});
child.once('exit', () => {
resolve(stdout);
});
});
child.kill();
/* eslint-enable no-unreachable */
return JSON.parse(stdout);
}
}

View File

@ -1,15 +1,14 @@
import process from 'process';
import cp from 'child_process';
import {fork} from 'child_process';
import path from 'path';
import {Platform, Result} from './types.js';
export default class PlatformNode implements Platform {
async run(ctx: Record<string, any>, file: string): Promise<Result> {
const child = cp.fork(
path.join(process.cwd(), 'fixtures', 'index-node.js'),
[file],
);
const child = fork(path.join(ctx.fixturesPath, 'index-node.js'), [file], {
cwd: ctx.fixturesPath,
});
const result: Result = await new Promise((resolve, reject) => {
child.once('error', reject);
child.once('exit', reject);

View File

@ -12,9 +12,9 @@ export class FixturesServer {
private server: net.Server | null = null;
constructor() {
constructor(path: string) {
this.koa = new Koa();
this.koa.use(files(path.join(process.cwd(), 'fixtures')));
this.koa.use(files(path));
}
async start(): Promise<ServerAddress> {