Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function (req) {
spy()
expect(req.method).to.equal('GET')
return new Request(req.url + '?foo=bar')
}
]
fetchUpstream(urlOrRequest, init) {
let request = urlOrRequest instanceof Request ? urlOrRequest : new Request(urlOrRequest, init);
const url = new URL(request.url);
const originalHost = url.host;
if (originalHost === this.origin) {
url.host = this.upstreamHost;
request = new Request(url, request);
request.headers.set("Host", originalHost);
}
if (init && init.cf) {
for (var key in init.cf) {
var val = init.cf[key];
key = key.split(/(?=[A-Z])/).join('-').toUpperCase();
request.headers.set(`CF-${key}`, val);
}
}
return fetch(request);
}
async _fetch(endPoint, fields) {
let body = new URLSearchParams(Object.assign(this.blog.toJSON(), fields));
if (this.isTest) body.set('is_test', '1');
let req = new fetch.Request(endPoint.href, {
method: 'POST',
headers: {'content-type': 'application/x-www-form-urlencoded', 'user-agent': this.userAgent},
body
});
this.emit('request', req);
let res;
try { res = await fetch(req); }
catch (err) { throw new ClientError(err.message, endPoint); }
this.emit('response', req, res);
if (!res.ok) throw new ClientError('An error occurred while querying the end point', endPoint);
if (res.headers.has('x-akismet-debug-help')) throw new ClientError(res.headers.get('x-akismet-debug-help'), endPoint);
return res.text();
private async _fetch(endPoint: URL, fields: JsonMap): Promise {
const body = new URLSearchParams(Object.assign(this.blog.toJSON(), fields));
if (this.isTest) body.set('is_test', '1');
const req = new Request(endPoint.href, {
body,
headers: {'content-type': 'application/x-www-form-urlencoded', 'user-agent': this.userAgent},
method: 'POST'
});
this.emit(Client.eventRequest, req);
let res: Response;
try { res = await fetch(req); }
catch (err) { throw new ClientError(err.message, endPoint); }
this.emit(Client.eventResponse, req, res);
if (!res.ok) throw new ClientError('An error occurred while querying the end point', endPoint);
if (res.headers.has('x-akismet-debug-help')) throw new ClientError(res.headers.get('x-akismet-debug-help')!, endPoint);
return res.text();
import fetch from 'node-fetch';
const Request = fetch.Request;
const Response = fetch.Response;
const Headers = fetch.Headers;
import Stream from 'stream';
import FetchMock from './lib/index';
import http from 'http';
import { setUrlImplementation } from './lib/request-utils';
import {URL} from 'whatwg-url'
setUrlImplementation(URL);
FetchMock.global = global;
FetchMock.statusTextMap = http.STATUS_CODES;
FetchMock.Stream = Stream;
FetchMock.config = Object.assign(FetchMock.config, {
Promise: Promise,
Request: Request,
function fetchService(serviceId) {
const url = new URL(`/v3/rest/services/${serviceId}`, MASHERY_HOST);
const request = new fetch.Request(url.toString(), {
headers: RequestHeaders
});
return fetchMashery(request);
}
async function callDropbox(url: string, body: Object): Promise {
const params = {
method: 'POST',
headers: {
'Authorization': '',
'Content-type': 'application/json',
},
body: JSON.stringify(body),
};
if (args['access-token']) {
params.headers['Authorization'] = `Bearer ${args['access-token']}`;
}
const q = new Request(url, params);
const r = await fetch(q);
if (r.status !== 200) {
throw new Error(await r.text());
}
return await r.json();
}
function fetchAllServiceEndpoints(serviceId) {
const url = new URL(`/v3/rest/services/${serviceId}/endpoints`, MASHERY_HOST);
const request = new fetch.Request(url.toString(), {
headers: RequestHeaders
});
return fetchMashery(request);
}