How to use bodyPromise method in wpt

Best JavaScript code snippet using wpt

index.js

Source:index.js Github

copy

Full Screen

1/**2 * koa-body - index.js3 * Copyright(c) 20144 * MIT Licensed5 *6 * @author Daryl Lau (@dlau)7 * @author Charlike Mike Reagent (@tunnckoCore)8 * @api private9 */10'use strict';11/**12 * Module dependencies.13 */14var buddy = require('co-body');15var forms = require('formidable');16/**17 * Expose `requestbody()`.18 */19module.exports = requestbody;20/**21 *22 * @param {Object} options23 * @see https://github.com/dlau/koa-body24 * @api public25 */26function requestbody(opts) {27 opts = opts || {};28 opts.onError = 'onError' in opts ? opts.onError : false;29 opts.patchNode = 'patchNode' in opts ? opts.patchNode : false;30 opts.patchKoa = 'patchKoa' in opts ? opts.patchKoa : true;31 opts.multipart = 'multipart' in opts ? opts.multipart : false;32 opts.urlencoded = 'urlencoded' in opts ? opts.urlencoded : true;33 opts.json = 'json' in opts ? opts.json : true;34 opts.text = 'text' in opts ? opts.text : true;35 opts.xml = 'xml' in opts ? opts.xml : true;36 opts.encoding = 'encoding' in opts ? opts.encoding : 'utf-8';37 opts.jsonLimit = 'jsonLimit' in opts ? opts.jsonLimit : '1mb';38 opts.formLimit = 'formLimit' in opts ? opts.formLimit : '56kb';39 opts.queryString = 'queryString' in opts ? opts.queryString : null;40 opts.formidable = 'formidable' in opts ? opts.formidable : {};41 opts.textLimit = 'textLimit' in opts ? opts.textLimit : '56kb';42 opts.strict = 'strict' in opts ? opts.strict : true;43 return function(ctx, next) {44 var bodyPromise;45 // so don't parse the body in strict mode46 if (!opts.strict || ["GET", "HEAD", "DELETE"].indexOf(ctx.method.toUpperCase()) === -1) {47 try {48 if (opts.json && ctx.is('json')) {49 bodyPromise = buddy.json(ctx, {50 encoding: opts.encoding,51 limit: opts.jsonLimit52 });53 } else if (opts.urlencoded && ctx.is('urlencoded')) {54 bodyPromise = buddy.form(ctx, {55 encoding: opts.encoding,56 limit: opts.formLimit,57 queryString: opts.queryString58 });59 } else if (opts.text && ctx.is('text')) {60 bodyPromise = buddy.text(ctx, {61 encoding: opts.encoding,62 limit: opts.textLimit63 });64 } else if (opts.multipart && ctx.is('multipart')) {65 bodyPromise = formy(ctx, opts.formidable);66 } else if (opts.xml && ctx.is('text/xml')) {67 bodyPromise = buddy.text(ctx, {68 encoding: opts.encoding,69 limit: opts.textLimit70 });71 }72 } catch (parsingError) {73 if (typeof opts.onError === 'function') {74 opts.onError(parsingError, ctx);75 } else {76 throw parsingError;77 }78 }79 }80 bodyPromise = bodyPromise || Promise.resolve({});81 return bodyPromise.catch(function(parsingError) {82 if (typeof opts.onError === 'function') {83 opts.onError(parsingError, ctx);84 } else {85 throw parsingError;86 }87 return next();88 })89 .then(function(body) {90 if (opts.patchNode) {91 ctx.req.body = body;92 }93 if (opts.patchKoa) {94 ctx.request.body = body;95 }96 return next();97 })98 };99}100/**101 * Donable formidable102 *103 * @param {Stream} ctx104 * @param {Object} opts105 * @return {Object}106 * @api private107 */108function formy(ctx, opts) {109 return new Promise(function(resolve, reject) {110 var fields = {};111 var files = {};112 var form = new forms.IncomingForm(opts);113 form.on('end', function() {114 return resolve({115 fields: fields,116 files: files117 });118 }).on('error', function(err) {119 return reject(err);120 }).on('field', function(field, value) {121 if (fields[field]) {122 if (Array.isArray(fields[field])) {123 fields[field].push(value);124 } else {125 fields[field] = [fields[field], value];126 }127 } else {128 fields[field] = value;129 }130 }).on('file', function(field, file) {131 if (files[field]) {132 if (Array.isArray(files[field])) {133 files[field].push(file);134 } else {135 files[field] = [files[field], file];136 }137 } else {138 files[field] = file;139 }140 });141 if (opts.onFileBegin) {142 form.on('fileBegin', opts.onFileBegin);143 }144 form.parse(ctx.req);145 });...

Full Screen

Full Screen

request-interception-referer.js

Source:request-interception-referer.js Github

copy

Full Screen

1(async function(testRunner) {2 var {page, session, dp} = await testRunner.startBlank(3 `Tests the overriden referer headers are properly reported and applied with interception`);4 session.protocol.Network.enable();5 session.protocol.Page.enable();6 await dp.Network.setExtraHTTPHeaders({headers: {'ReFeReR': 'https://127.0.0.1:8000/'}});7 await session.protocol.Network.setRequestInterception({patterns: [{urlPattern: "*"}]});8 testRunner.log('*Not* overriding referer in interception handler:');9 var {interceptionId, bodyPromise} = await sendRequestAndIntercept();10 session.protocol.Network.continueInterceptedRequest({interceptionId: interceptionId});11 testRunner.log(`response: ${await bodyPromise}`);12 testRunner.log('Overriding referer in interception handler:');13 var {interceptionId, bodyPromise} = await sendRequestAndIntercept();14 session.protocol.Network.continueInterceptedRequest({interceptionId: interceptionId, headers: {'ReFeReR': 'http://localhost:8000/'}});15 testRunner.log(`response: ${await bodyPromise}`);16 testRunner.completeTest();17 async function sendRequestAndIntercept() {18 const requestPromise = session.protocol.Network.onceRequestWillBeSent();19 const evalPromise = session.evaluateAsync(`(async function() {20 var url = '${testRunner.url('./resources/echo-headers.php?headers=HTTP_REFERER')}';21 var response = await fetch(new Request(url));22 return response.text();23 })()`);24 const interceptedRequest = (await session.protocol.Network.onceRequestIntercepted()).params;25 const request = (await requestPromise).params;26 testRunner.log(`referer in requestWillBeSent: ${request.request.headers['Referer']}`);27 testRunner.log(`referer in requestIntercepted: ${interceptedRequest.request.headers['Referer']}`);28 return {interceptionId: interceptedRequest.interceptionId, bodyPromise: evalPromise};29 }...

Full Screen

Full Screen

http.test.js

Source:http.test.js Github

copy

Full Screen

1/* eslint-env jest */2import { readBody } from '../../../utils/http'3const mockRequest = {4 on: jest.fn().mockName('on')5}6describe('utils/http', () => {7 describe('#readBody', () => {8 let bodyPromise9 let receiveData10 let endStream11 let abortRead12 beforeEach(() => {13 bodyPromise = readBody(mockRequest)14 ;([/* data event */, receiveData] = mockRequest.on.mock.calls.find(([event]) => event === 'data'))15 ;([/* end event */, endStream] = mockRequest.on.mock.calls.find(([event]) => event === 'end'))16 ;([/* error event */, abortRead] = mockRequest.on.mock.calls.find(([event]) => event === 'error'))17 })18 it('should read request stream until it ends', async () => {19 const chunks = [20 'chunk one',21 'chunk two'22 ]23 chunks.forEach(chunck => receiveData(chunck))24 endStream()25 const body = await bodyPromise26 expect(body).toBe(chunks.join(''))27 })28 it('should reject on request stream error', () => {29 const error = new Error('Test error')30 abortRead(error)31 return expect(bodyPromise).rejects.toBe(error)32 })33 })...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var webPageTest = new wpt('API_KEY');3var location = 'Dulles:Chrome';4var options = {5};6webPageTest.runTest(testUrl, options, function(err, data) {7 if (err) return console.log(err);8 console.log('Test ID: ' + data.data.testId);9 console.log('Test URL: ' + data.data.userUrl);10 var testId = data.data.testId;11 webPageTest.getTestResults(testId, function(err, data) {12 if (err) return console.log(err);13 console.log(data);14 });15});16var wpt = require('webpagetest');17var webPageTest = new wpt('API_KEY');18var location = 'Dulles:Chrome';19var options = {20};21webPageTest.runTest(testUrl, options, function(err, data) {22 if (err) return console.log(err);23 console.log('Test ID: ' + data.data.testId);24 console.log('Test URL: ' + data.data.userUrl);25 var testId = data.data.testId;26 webPageTest.getTestResults(testId, function(err, data) {27 if (err) return console.log(err);28 console.log(data);29 });30});

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org', 'A.0c3e8b0ff2d2f0a2c2b2b8c8b1a0b1b1');3var test = wpt.runTest(url, {4}, function(err, data) {5 console.log(data);6 wpt.getTestResults(data.data.testId, function(err, data) {7 console.log(data);8 });9});10var wpt = require('webpagetest');11var wpt = new WebPageTest('www.webpagetest.org', 'A.0c3e8b0ff2d2f0a2c2b2b8c8b1a0b1b1');12var test = wpt.runTest(url, {13});14test.then(function(data) {15 console.log(data);16 wpt.getTestResults(data.data.testId).then(function(data) {17 console.log(data);18 });19});20var wpt = require('webpagetest');21var wpt = new WebPageTest('www.webpagetest.org', 'A.0c3e8b0ff2d2f0a2c2b2b8c8b1a0b1b1');22var test = wpt.runTest(url, {23});24async function run() {25 var data = await test;26 console.log(data);27 var data = await wpt.getTestResults(data.data.testId);28 console.log(data);29}30run();31var wpt = require('

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful