How to use proxify method in chai

Best JavaScript code snippet using chai

index.js

Source:index.js Github

copy

Full Screen

1const http = require('http'),2 https = require('https'),3 fs = require('fs'),4 zlib = require('zlib'),5 querystring = require('querystring'),6 WebSocket = require('ws'),7 btoa = str => new Buffer.from(str).toString('base64'),8 atob = str => new Buffer.from(str, 'base64').toString('utf-8');9module.exports = class {10 // Constructor function.11 constructor(prefix = "/crosis/", config = {}) {12 this.prefix = prefix;13 this.config = config;14 this.proxifyRequestURL = (url, type) => type ? atob(url.split('_').slice(1).splice(0, 1).join()) + url.split('_').slice(2).join('_') : `_${btoa(url.split('/').splice(0, 3).join('/'))}_/${url.split('/').splice(3).join('/')}`15 if (!prefix.startsWith('/')) this.prefix = '/' + prefix;16 if (!prefix.endsWith('/')) this.prefix = prefix + '/'; 17 };18 // HTTP(S) proxy.19 http(req, res, next = () => res.end('')) {20 if (!req.url.startsWith(this.prefix)) return next();21 // Defining alternatives to `req.url` that don't contain web proxy prefix (req.path) and with the additional prop (req.pathname) not containing any hash or query params.22 req.path = req.url.replace(this.prefix.slice(1), '');23 req.pathname = req.path.split('#')[0].split('?')[0];24 25 if (req.pathname == '/client_hook' || req.pathname == '/client_hook/') return res.end(fs.readFileSync(__dirname + '/window.js', 'utf-8'));26 27 try {new URL(this.proxifyRequestURL(req.path, true))} catch {return res.end('URL Parse Error')};28 29 var proxyURL = {30 href: this.proxifyRequestURL(req.path, true),31 origin: this.proxifyRequestURL(req.path, true).split('/').splice(0, 3).join('/'),32 hostname: this.proxifyRequestURL(req.path, true).split('/').splice(0, 3).slice(2).join('/')33 },34 proxify = {},35 isBlocked = false,36 protocol = proxyURL.href.startsWith('https://') ? https : http, 37 proxyOptions = {38 headers: Object.assign({}, req.headers),39 method: req.method,40 rejectUnauthorized: false41 };42 43 if (proxyURL.href.startsWith('https://') || proxyURL.href.startsWith('http://')); else return res.end('URL Parse Error');44 45 delete proxyOptions.headers['host']; 46 47 // URL hostname blocklist.48 if (typeof this.config.blacklist == 'object' && this.config.blacklist.length != 0) this.config.blacklist.forEach(blacklisted => proxyURL.hostname == blacklisted ? isBlocked = true : isBlocked = false);49 if (isBlocked) return res.end('The URL you are trying to access is not permitted for use.')50 if (!req.path.startsWith(`/_${btoa(proxyURL.origin)}_/`)) return (res.writeHead(308, { location: this.prefix + `_${btoa(proxyURL.origin)}_/`}), res.end(''));51 52 // Proxifying "Origin" request header. Vital since some websites might have a failsafe for their API involving the "Origin" request header.53 if (proxyOptions.headers['origin']) {54 var proxified_header = this.proxifyRequestURL(`/${proxyOptions.headers['origin'].split('/').splice(3).join('/')}`.replace(this.prefix, ''), true);55 if (proxified_header.startsWith('https://') || proxified_header.startsWith('http://')) proxified_header = proxified_header.split('/').splice(0, 3).join('/');56 else proxified_header = proxyURL.origin;57 proxyOptions.headers['origin'] = proxified_header;58 }59 60 // Proxifying "Referer" request header. Vital since some websites might have a failsafe for their API involving the "Referer" request header.61 if (proxyOptions.headers['referer']) {62 63 var proxified_header = this.proxifyRequestURL('/' + proxyOptions.headers['referer'].split('/').splice(3).join('/').replace(this.prefix, ''), true);64 if (proxified_header.startsWith('https://') || proxified_header.startsWith('http://')) proxified_header = proxified_header;65 else proxified_header = proxyURL.href;66 67 proxyOptions.headers['referer'] = proxified_header;68 69 }70 71 72 if (proxyOptions.headers['cookie']) { 73 var new_cookie = [],74 cookie_array = proxyOptions.headers['cookie'].split('; ');75 76 cookie_array.forEach(cookie => {77 78 const cookie_name = cookie.split('=').splice(0, 1).join(),79 cookie_value = cookie.split('=').splice(1).join();80 81 if (proxyURL.hostname.includes(cookie_name.split('@').splice(1).join())) new_cookie.push(cookie_name.split('@').splice(0, 1).join() + '=' + cookie_value);82 83 });84 85 proxyOptions.headers['cookie'] = new_cookie.join('; ');86 };87 if (typeof this.config.localAddress == 'object' && this.config.localAddress.length != 0) proxyOptions.localAddress = this.config.localAddress[Math.floor(Math.random() * this.config.localAddress.length)];88 89 var makeRequest = protocol.request(proxyURL.href, proxyOptions, proxyResponse => {90 91 var rawData = [],92 sendData = '';93 94 proxyResponse.on('data', data => rawData.push(data)).on('end', () => {95 96 const inject_config = {97 prefix: this.prefix,98 url: proxyURL.href99 }100 101 // General URL proxifer.102 proxify.url = url => {103 104 if (url.match(/^(#|about:|data:|blob:|mailto:|javascript:|{|\*)/)) return url;105 106 if (url.startsWith('//')) url = new URL('http:' + url);107 else if (url.startsWith('/')) url = new URL(proxyURL.origin + url);108 else if (url.startsWith('https://') || url.startsWith('http://')) url = new URL(url);109 else url = new URL(proxyURL.href.split('/').slice(0, -1).join('/') + '/' + url);110 111 if (url.protocol == 'https:' || url.protocol == 'http:') return this.prefix + this.proxifyRequestURL(url.href);112 else return url.href; 113 114 };115 116 // Javascript "location" object proxifier. Will be replaced in the future with a more efficient one.117 proxify.js = buffer => buffer.toString().replace(/(,| |=|\()document.location(,| |=|\)|\.)/gi, str => { return str.replace('.location', `.alloyLocation`); })118 .replace(/(,| |=|\()window.location(,| |=|\)|\.)/gi, str => { return str.replace('.location', `.alloyLocation`); })119 .replace(/(,| |=|\()location(,| |=|\)|\.)/gi, str => { return str.replace('location', `alloyLocation`); });120 121 122 // CSS proxifier.123 proxify.css = buffer => {124 return buffer.replace(/url\("(.*?)"\)/gi, str => {125 var url = str.replace(/url\("(.*?)"\)/gi, '$1');126 return `url("${proxify.url(url)}")`;127 }).replace(/url\('(.*?)'\)/gi, str => {128 var url = str.replace(/url\('(.*?)'\)/gi, '$1');129 return `url('${proxify.url(url)}')`;130 }).replace(/url\((.*?)\)/gi, str => {131 var url = str.replace(/url\((.*?)\)/gi, '$1');132 133 if (url.startsWith(`"`) || url.startsWith(`'`)) return str;134 135 return `url("${proxify.url(url)}")`;136 }).replace(/@import (.*?)"(.*?)";/gi, str => {137 var url = str.replace(/@import (.*?)"(.*?)";/, '$2');138 return `@import "${proxify.url(url)}";`139 }).replace(/@import (.*?)'(.*?)';/gi, str => {140 var url = str.replace(/@import (.*?)'(.*?)';/, '$2');141 return `@import '${proxify.url(url)}';`142 })143 };144 145 // DOM based HTML proxifier.146 proxify.html = body => {147 148 const html = new (require('./dom')).JSDOM(body, {contentType: 'text/html'}), document = html.window.document;149 150 var base_tag = false; 151 152 if (document.querySelector('head base')) base_tag = document.querySelector('head base').getAttribute('href');153 154 // Sloppy due to having to release this fast.155 if (base_tag) {156 157 if (base_tag.includes('#') || base_tag.includes('?')) base_tag = base_tag.split('#')[0].split('?')[0];158 159 if (base_tag.startsWith('//')) base_tag = 'http:' + base_tag;160 161 if (base_tag.startsWith('https://') || base_tag.startsWith('http://')) base_tag = new URL(base_tag).href;162 else if (base_tag.startsWith('/')) base_tag = new URL(proxyURL.origin + base_tag).href;163 else base_tag = new URL(proxyURL.href.split('/').slice(0, -1).join('/') + '/' + base_tag).href;164 165 inject_config.baseURL = base_tag;166 167 };168 169 proxify.attribute = attribute => {170 if (attribute.startsWith('https://') || attribute.startsWith('http://') || attribute.startsWith('//')) return proxify.url(attribute);171 else if (base_tag) {172 if (attribute.startsWith('/')) return attribute = proxify.url(base_tag.split('/').splice(0, 3).join('/') + attribute);173 else return attribute = proxify.url(base_tag.split('/').slice(0, -1).join('/') + '/' + attribute);174 } else return proxify.url(attribute);175 };176 177 // Removing all "nonce" and "integrity" attributes. 178 document.querySelectorAll('*').forEach(node => {179 if (node.getAttribute('nonce')) node.removeAttribute('nonce');180 if (node.getAttribute('integrity')) node.removeAttribute('integrity');181 if (node.getAttribute('style')) node.setAttribute('style', proxify.css(node.getAttribute('style')));182 });183 184 // Rewriting "src" attributes on elements.185 document.querySelectorAll("script, embed, iframe, audio, video, img, input, source, track").forEach(node => {186 if (node.src) node.src = proxify.attribute(node.src);187 if (node.tagName.toLowerCase() == 'script' && node.innerHTML != '') node.innerHTML = proxify.js(node.innerHTML);188 });189 190 document.querySelectorAll("img[srcset], source[srcset]").forEach(node => {191 var arr = [];192 193 node.srcset.split(',').forEach(url => {194 url = url.trimStart().split(' ');195 url[0] = proxify.attribute(url[0]);196 arr.push(url.join(' '));197 });198 199 node.srcset = arr.join(', ')200 });201 202 // Rewriting "href" attributes on elements.203 document.querySelectorAll("a, link, area").forEach(node => {204 if (node.href) node.href = proxify.attribute(node.href);205 });206 207 document.querySelectorAll('base').forEach(node => node.href = proxify.attribute(node.href));208 209 // Rewriting "action" attribute for forms.210 document.querySelectorAll('form').forEach(node => {211 if (node.action) node.action = proxify.attribute(node.action);212 });213 214 document.querySelectorAll('style').forEach(node => {215 node.textContent = proxify.css(node.textContent);216 });217 218 // Creating injection script element.219 const inject_script = document.createElement('script');220 221 // Setting injection script attributes.222 inject_script.src = this.prefix + 'client_hook';223 inject_script.setAttribute('data-config', btoa(JSON.stringify(inject_config)));224 225 // Putting "script" element for injection in the beginning of "head" element.226 document.querySelector('head').insertBefore(inject_script, document.querySelector('head').childNodes[0])227 228 return html.serialize();229 230 };231 232 // Handling response body Content-Encoding.233 if (rawData.length != 0) switch(proxyResponse.headers['content-encoding']) {234 case 'gzip':235 sendData = zlib.gunzipSync(Buffer.concat(rawData));236 break;237 case 'deflate':238 sendData = zlib.inflateSync(Buffer.concat(rawData));239 break;240 case 'br':241 sendData = zlib.brotliDecompressSync(Buffer.concat(rawData));242 break;243 default: sendData = Buffer.concat(rawData); break;244 };245 246 // Handling response headers.247 Object.entries(proxyResponse.headers).forEach(([header_name, header_value]) => {248 if (header_name == 'set-cookie') {249 const cookie_array = [];250 header_value.forEach(cookie => cookie_array.push(cookie.replace(/Domain=(.*?);/gi, `Domain=` + req.headers['host'] + ';').replace(/(.*?)=(.*?);/, '$1' + '@' + proxyURL.hostname + `=` + '$2' + ';')));251 proxyResponse.headers[header_name] = cookie_array;252 253 };254 255 if (header_name.startsWith('content-encoding') || header_name.startsWith('x-') || header_name.startsWith('cf-') || header_name.startsWith('strict-transport-security') || header_name.startsWith('content-security-policy') || header_name.startsWith('content-length')) delete proxyResponse.headers[header_name];256 257 if (header_name == 'location') proxyResponse.headers[header_name] = proxify.url(header_value);258 });259 260 // Rewriting the response body based off of the Content-Type response header.261 if (proxyResponse.headers['content-type'] && proxyResponse.headers['content-type'].startsWith('text/html')) sendData = proxify.html(sendData.toString());262 else if (proxyResponse.headers['content-type'] && (proxyResponse.headers['content-type'].startsWith('application/javascript') || proxyResponse.headers['content-type'].startsWith('text/javascript'))) sendData = proxify.js(sendData.toString());263 else if (proxyResponse.headers['content-type'] && proxyResponse.headers['content-type'].startsWith('text/css')) sendData = proxify.css(sendData.toString());264 265 // Sending proxy response with processed headers and body.266 res.writeHead(proxyResponse.statusCode, proxyResponse.headers);267 res.end(sendData);268 269 });270 271 });272 273 makeRequest.on('error', err => res.end(err.toString()))274 275 if (!res.writableEnded) req.on('data', data => makeRequest.write(data)).on('end', () => makeRequest.end());276 277 };278 // Websocket Proxy279 ws(server) {280 new WebSocket.Server({server: server}).on('connection', (cli, req) => {281 var queryParams = querystring.parse(req.url.split('?').splice(1).join('?')), proxyURL, options = { 282 headers: {},283 followRedirects: true284 }, protocol = [];285 286 if (!queryParams.ws) return cli.close();287 288 proxyURL = atob(queryParams.ws);289 290 try { new URL(proxyURL) } catch{ return cli.close() };291 292 Object.entries(req.headers).forEach(([header_name, header_value]) => {293 if (header_name == 'sec-websocket-protocol') header_value.split(', ').forEach(proto => protocol.push(proto));294 if (header_name.startsWith('cf-') || header_name.startsWith('cdn-loop'));295 else if (!header_name.startsWith('sec-websocket')) options.headers[header_name] = header_value;296 })297 298 if (queryParams.origin) (options.origin = atob(queryParams.origin), options.headers.origin = atob(queryParams.origin)); 299 300 delete options.headers['host'];301 delete options.headers['cookie'];302 303 if (typeof this.config.localAddress == 'object' && this.config.localAddress.length != 0) options.localAddress = this.config.localAddress[Math.floor(Math.random() * this.config.localAddress.length)];304 const proxy = new WebSocket(proxyURL, protocol, options),305 before_open = [];306 307 if (proxy.readyState == 0) cli.on('message', data => before_open.push(data));308 309 cli.on('close', () => proxy.close());310 proxy.on('close', () => cli.close());311 cli.on('error', () => proxy.terminate())312 proxy.on('error', () => cli.terminate());313 314 proxy.on('open', () => {315 316 if (before_open.length != 0) before_open.forEach(data => proxy.send(data))317 318 cli.on('message', data => proxy.send(data));319 proxy.on('message', data => cli.send(data));320 321 322 });323 324 });325 };...

Full Screen

Full Screen

ModelProxyHandler.test.js

Source:ModelProxyHandler.test.js Github

copy

Full Screen

...4const Sinon = require("sinon");5const ModelProxyHandler_1 = require("../../lib/model/ModelProxyHandler");6describe('ModelProxyHandler', function () {7 describe('.get()', function () {8 it('calls and returns driver.proxify() with get if driver.shouldBeProxied() returns true', function () {9 const driver = {10 shouldBeProxied() {11 return true;12 },13 proxify() {14 return 'anything';15 }16 };17 const model = {18 driver: driver19 };20 const proxifySpy = Sinon.spy(driver, 'proxify');21 expect(ModelProxyHandler_1.ModelProxyHandler.get.call(undefined, model, 'key')).toEqual('anything');22 expect(proxifySpy.calledWith('get', model, 'key')).toBe(true);23 });24 it('returns target[key] with get if driver.shouldBeProxied() returns false', function () {25 const driver = {26 shouldBeProxied() {27 return false;28 },29 proxify() {30 return 'anything';31 }32 };33 const model = {34 driver: driver,35 key: 'value'36 };37 const proxifySpy = Sinon.spy(driver, 'proxify');38 expect(ModelProxyHandler_1.ModelProxyHandler.get.call(undefined, model, 'key')).toEqual('value');39 expect(proxifySpy.calledWith('get', model, 'key')).toBe(false);40 });41 });42 describe('.set()', function () {43 it('calls and returns driver.proxify() with get if driver.shouldBeProxied() returns true', function () {44 const driver = {45 shouldBeProxied() {46 return true;47 },48 proxify() {49 return 'anything';50 }51 };52 const model = {53 driver: driver54 };55 const proxifySpy = Sinon.spy(driver, 'proxify');56 expect(ModelProxyHandler_1.ModelProxyHandler.set.call(undefined, model, 'key', 'value')).toEqual('anything');57 expect(proxifySpy.calledWith('set', model, 'key', 'value')).toBe(true);58 });59 it('assigns value to target[key] with get if driver.shouldBeProxied() returns false', function () {60 const driver = {61 shouldBeProxied() {62 return false;63 },64 proxify() {65 return 'anything';66 }67 };68 const model = {69 driver: driver70 };71 const proxifySpy = Sinon.spy(driver, 'proxify');72 ModelProxyHandler_1.ModelProxyHandler.set.call(undefined, model, 'key', 'value');73 expect(model['key']).toEqual('value');74 expect(proxifySpy.calledWith('get', model, 'key')).toBe(false);75 });76 });...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const chai = require('chai');2const chaiHttp = require('chai-http');3const app = require('../app');4const should = chai.should();5chai.use(chaiHttp);6describe('GET /', () => {7 it('should return 200 OK', () => {8 chai.request(app)9 .get('/')10 .end((err, res) => {11 res.should.have.status(200);12 done();13 });14 });15});16const chai = require('chai');17const chaiHttp = require('chai-http');18const app = require('../app');19const should = chai.should();20chai.use(chaiHttp);21describe('GET /', () => {22 it('should return 200 OK', () => {23 chai.request(app)24 .get('/')25 .end((err, res) => {26 res.should.have.status(200);27 done();28 });29 });30});31const chai = require('chai');32const chaiHttp = require('chai-http');33const app = require('../app');34const should = chai.should();35chai.use(chaiHttp);36describe('GET /', () => {37 it('should return 200 OK', () => {38 chai.request(app)39 .get('/')40 .end((err, res) => {41 res.should.have.status(200);42 done();43 });44 });45});46- [Chai HTTP GitHub](

Full Screen

Using AI Code Generation

copy

Full Screen

1var proxify = require('chain').proxify;2var obj = {3 a: function() {4 console.log('a');5 },6 b: function() {7 console.log('b');8 }9};10var proxy = proxify(obj);11proxy.a().b();12var proxify = require('chain').proxify;13var obj = {14 a: function() {15 console.log('a');16 },17 b: function() {18 console.log('b');19 }20};21var proxy = proxify(obj);22proxy.a().b();23var proxify = require('chain').proxify;24var obj = {25 a: function() {26 console.log('a');27 },28 b: function() {29 console.log('b');30 }31};32var proxy = proxify(obj);33proxy.a().b();34var proxify = require('chain').proxify;35var obj = {36 a: function() {37 console.log('a');38 },39 b: function() {40 console.log('b');41 }42};43var proxy = proxify(obj);44proxy.a().b();45var proxify = require('chain').proxify;46var obj = {47 a: function() {48 console.log('a');49 },50 b: function() {51 console.log('b');52 }53};54var proxy = proxify(obj);55proxy.a().b();56var proxify = require('chain').proxify;57var obj = {58 a: function() {59 console.log('a');60 },61 b: function() {62 console.log('b');63 }64};65var proxy = proxify(obj);66proxy.a().b();67var proxify = require('chain').proxify;

Full Screen

Using AI Code Generation

copy

Full Screen

1var chain = require('./chain.js');2console.log(result);3var chain = require('./chain.js');4console.log(result);5var http = require('http');6var https = require('https');7var httpProxy = require('http-proxy');8var proxy = httpProxy.createProxyServer({});9var url = require('url');10var request = require('request');11var fs = require('fs');12var express = require('express');13var app = express();14var bodyParser = require('body-parser');15var port = 3000;16app.use(bodyParser.urlencoded({ extended: true }));17app.use(bodyParser.json());18var router = express.Router();19var proxyServer = http.createServer(function (req, res) {20proxy.web(req, res, { target: req.url });21});22router.get('/', function (req, res) {23res.json({ message: 'Welcome to our api!' });24});25router.post('/proxy', function (req, res) {26var url = req.body.url;27var result = proxify(url);28res.json(result);29});30app.use('/api', router);31app.listen(port);32console.log('Magic happens on port ' + port);33function proxify(url) {34var result = {};35request(url, function (error, response, body) {36if (!error && response.statusCode == 200) {37var $ = cheerio.load(body);38var title = $('title').text();39result.title = title;40var description = $('meta[name="description"]').attr('content');41result.description = description;42var keywords = $('meta[name="keywords"]').attr('content');43result.keywords = keywords;44var ogTitle = $('meta[property="og:title"]').attr('content');45result.ogTitle = ogTitle;46var ogDescription = $('meta[property="og:description"]').attr('content');47result.ogDescription = ogDescription;48var ogImage = $('meta[property="og:image"]').attr('content');49result.ogImage = ogImage;50var ogUrl = $('meta[property="og:url"]').attr('content');51result.ogUrl = ogUrl;52var ogType = $('meta[property="og:type"]').attr('content');53result.ogType = ogType;54var twitterCard = $('meta[name="twitter:card"]').attr('content');

Full Screen

Using AI Code Generation

copy

Full Screen

1var chainable = require('./chainable');2var obj = chainable.proxify({3});4obj.name = 'test2';5obj.value = 2;6console.log(obj.name);7console.log(obj.value);8* **Rafael Pires** - *Initial work* - [rafael-pires](

Full Screen

Using AI Code Generation

copy

Full Screen

1var chainable = require('chainable');2var chain = new chainable();3chain.proxify('test', function(){4 this.test = 'test';5});6var chainable = require('chainable');7var chain = new chainable();8chain.proxify('test', function(){9 this.test = 'test';10});11var chainable = require('chainable');12var chain = new chainable();13chain.proxify('test', function(){14 this.test = 'test';15});16var chainable = require('chainable');17var chain = new chainable();18chain.proxify('test', function(){19 this.test = 'test';20});21var chainable = require('chainable');22var chain = new chainable();23chain.proxify('test', function(){24 this.test = 'test';25});26var chainable = require('chainable');27var chain = new chainable();28chain.proxify('test', function(){29 this.test = 'test';30});31var chainable = require('chainable');32var chain = new chainable();33chain.proxify('test', function(){34 this.test = 'test';35});36var chainable = require('chainable');37var chain = new chainable();38chain.proxify('test', function(){39 this.test = 'test';40});

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 chai 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