fetch(feature, distance) { let lon = feature.geometry.coordinates[0], lat = feature.geometry.coordinates[1]; let search = new URLSearchParams(); search.set('location', [lon, lat].join(",")); search.set('method', 'distance'); search.set('holes', true); search.set('distance', distance); search.set('graphName', 'Voiture'); search.set('smoothing', false); return fetch(this.url + '?' + search.toString(), { headers: { "Referer": this.options.referer } }).then(response => { if (response.status >= 400) throw new Error('Bad response'); return response.json(); }).then(result => { let polygon = parse(result.wktGeometry); return this.polygonToConcaveHull(polygon); }); }
static executeKotlinCode(code, compilerVersion) { const projectJson = JSON.stringify({ "id": "", "name": "", "args": "", "compilerVersion": compilerVersion, "confType": "java", "originUrl": null, "files": [ { "name": "File.kt", "text": code, "publicId": "" } ], "readOnlyFileNames": [] }); const body = new URLSearchParams(); body.set('filename', "File.kt"); body.set('project', projectJson); return fetch(`${webDemoURL}/kotlinServer?type=run&runConf=java`, { method: 'POST', body: body.toString(), headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' } }).then(response => response.json()).then(function (data) { let output; if (data.text !== undefined) { output = data.text.replace("<outStream>", "<span class=\"standard-output\">") .replace("</outStream>", "</span>") .replace("<errStream>", "<span class=\"error-output\">") .replace("</errStream>", "</span>"); } else { output = ""; } if (data.exception != null) { data.exception.causes = getExceptionCauses(data.exception); data.exception.cause = undefined; } return { errors: data.errors["File.kt"], output: output, exception: data.exception } }) }
function toFormData(data) { const params = new URLSearchParams(); if (data) { Object.keys(data).forEach((key) => { const value = data[key]; if (value !== null && typeof value !== 'undefined') { params.set(key, value); } }); } return params.toString(); }
describe('PHP CGI', () => { before(async () => server.ready) var cgiRel = './test/cgi json.php' var cgiPath = path.join(root, cgiRel).replace(/\\/g, '/') var cgiHttpUrl = (new URL(cgiRel, 'http://localhost/')).href var cgiHttpsUrl = (new URL(cgiRel, 'https://localhost/')).href var searchParams = new URLSearchParams() var params = { hello: 'World!', foo: 'bar, baz, quox' } for (var [key, value] of Object.entries(params)) searchParams.append(key, value) var query = searchParams.toString() var cgiQueryUrl = `${cgiHttpUrl}?${query}` var postFormOptions = { method: 'POST', body: query, headers: { 'content-type': 'application/x-www-form-urlencoded' } } describe('globals', () => { describe('$_GET', () => { it('GET with no arguments has empty $_GET', async () => { var {$_GET} = await fetch(cgiHttpUrl).then(res => res.json()) assert.isArray($_GET) assert.isEmpty($_GET) }) it('GET with query arguments has them in $_GET', async () => { var {$_GET} = await fetch(cgiQueryUrl).then(res => res.json()) assert.isObject($_GET) assert.isNotEmpty($_GET) assert.equal($_GET.hello, params.hello) assert.equal($_GET.foo, params.foo) }) it('POST with query arguments has empty $_GET', async () => { var {$_GET} = await fetch(cgiHttpUrl, postFormOptions).then(res => res.json()) assert.isArray($_GET) assert.isEmpty($_GET) }) }) describe('$_POST', () => { it('GET with no arguments has empty $_POST', async () => { var {$_POST} = await fetch(cgiHttpUrl).then(res => res.json()) assert.isArray($_POST) assert.isEmpty($_POST) }) it('GET with query arguments has empty $_POST', async () => { var {$_POST} = await fetch(cgiQueryUrl).then(res => res.json()) assert.isArray($_POST) assert.isEmpty($_POST) }) it('POST with query arguments has has them in $_POST', async () => { var {$_POST} = await fetch(cgiHttpUrl, postFormOptions).then(res => res.json()) assert.isObject($_POST) assert.isNotEmpty($_POST) assert.equal($_POST.hello, params.hello) assert.equal($_POST.foo, params.foo) }) }) describe('$_REQUEST', () => { it('GET with no arguments has empty $_REQUEST', async () => { var {$_REQUEST} = await fetch(cgiHttpUrl).then(res => res.json()) assert.isArray($_REQUEST) assert.isEmpty($_REQUEST) }) it('GET with query arguments has them in $_REQUEST', async () => { var {$_REQUEST} = await fetch(cgiQueryUrl).then(res => res.json()) assert.isObject($_REQUEST) assert.isNotEmpty($_REQUEST) assert.equal($_REQUEST.hello, params.hello) assert.equal($_REQUEST.foo, params.foo) }) it('POST with query arguments has them in $_REQUEST', async () => { var {$_REQUEST} = await fetch(cgiHttpUrl, postFormOptions).then(res => res.json()) assert.isObject($_REQUEST) assert.isNotEmpty($_REQUEST) assert.equal($_REQUEST.hello, params.hello) assert.equal($_REQUEST.foo, params.foo) }) }) describe('$_SERVER', () => { it(`GET request results in $_SERVER['REQUEST_METHOD'] === 'GET'`, async () => { var {$_SERVER} = await fetch(cgiHttpUrl).then(res => res.json()) assert.equal($_SERVER['REQUEST_METHOD'], 'GET') }) it(`POST request results in $_SERVER['REQUEST_METHOD'] === 'POST'`, async () => { var {$_SERVER} = await fetch(cgiHttpUrl, {method: 'POST'}).then(res => res.json()) assert.equal($_SERVER['REQUEST_METHOD'], 'POST') }) it(`PUT request results in $_SERVER['REQUEST_METHOD'] === 'PUT'`, async () => { var {$_SERVER} = await fetch(cgiHttpUrl, {method: 'PUT'}).then(res => res.json()) assert.equal($_SERVER['REQUEST_METHOD'], 'PUT') }) it(`$_SERVER['QUERY_STRING'] has the url query sting`, async () => { var {$_SERVER} = await fetch(cgiQueryUrl).then(res => res.json()) assert.equal($_SERVER['QUERY_STRING'], query) }) it(`$_SERVER['REQUEST_URI'] also contains query`, async () => { var {$_SERVER} = await fetch(cgiQueryUrl).then(res => res.json()) assert.equal($_SERVER['REQUEST_URI'], '/test/cgi%20json.php?' + query) }) it(`$_SERVER['SCRIPT_NAME'], $_SERVER['PHP_SELF'] do not contain query`, async () => { var {$_SERVER} = await fetch(cgiQueryUrl).then(res => res.json()) assert.equal($_SERVER['SCRIPT_NAME'], '/test/cgi json.php') assert.equal($_SERVER['PHP_SELF'], '/test/cgi json.php') }) it(`$_SERVER['DOCUMENT_ROOT'], $_SERVER['CONTEXT_DOCUMENT_ROOT']`, async () => { var {$_SERVER} = await fetch(cgiQueryUrl).then(res => res.json()) assert.equal($_SERVER['DOCUMENT_ROOT'], root) assert.equal($_SERVER['CONTEXT_DOCUMENT_ROOT'], root) }) it(`$_SERVER['SCRIPT_FILENAME'] contains the full disk path to the executed file`, async () => { var {$_SERVER} = await fetch(cgiQueryUrl).then(res => res.json()) assert.equal($_SERVER['SCRIPT_FILENAME'], cgiPath) }) }) }) })