function getClientIp(req) { var ip; var headers = req.headers || {}; try { ip = getForwardedFor(headers); if (!ip) { ip = (req.connection || req.socket || req).remoteAddress; ip = removeIPV6Prefix(ip); } } catch(e) {} return isLocalIp(ip) ? LOCALHOST : ip; }
server.on('connect', function(req, socket) { var tunnelUrl = 'tunnel://' + (/^[^:\/]+:\d+$/.test(req.url) ? req.url : req.headers.host); var options = parseUrl(tunnelUrl); var client = net.connect({ host: '127.0.0.1', port: options.port || 443 }, function() { socket.pipe(client).pipe(socket); socket.write('HTTP/1.1 200 Connection Established\r\nProxy-Agent: whistle/test\r\n\r\n'); }); });
util.request({ method: 'put', url: 'https://attachment.test.whistlejs.com/index2.html' }, function(res, data) { res.headers.should.have.property('content-disposition', 'attachment; filename="index2.html"'); });
test('shorthand - request get', t => { t.plan(4) sget({ method: 'GET', url: 'http://localhost:' + fastify.server.address().port }, (err, response, body) => { t.error(err) t.strictEqual(response.statusCode, 200) t.strictEqual(response.headers['content-length'], '' + body.length) t.deepEqual(JSON.parse(body), { hello: 'world' }) }) })
// Ported from https://github.com/ciaranj/node-oauth/blob/a7f8a1e21c362eb4ed2039431fb9ac2ae749f26a/lib/oauth2.js function _get (provider, accessToken, callback) { const url = provider.profileUrl const headers = provider.headers || {} if (this._useAuthorizationHeaderForGET) { headers.Authorization = this.buildAuthHeader(accessToken) // This line is required for Twitch headers['Client-ID'] = provider.clientId accessToken = null } this._request('GET', url, headers, null, accessToken, callback) }
test('shorthand - get, headers schema', t => { t.plan(1) try { fastify.get('/headers', headersSchema, function (req, reply) { reply.code(200).send(req.headers) }) t.pass() } catch (e) { t.fail() } })
test('Missing x-apigateway-context', () => { const req = generateMockReq() delete req.headers['x-apigateway-context'] eventContextMiddleware({ deleteHeaders: false })(req, mockRes, mockNext) expect(req.apiGateway).toBe(undefined) })
util.request({ method: 'post', url: 'http://values2.test.com/index.html', body: 'ssi1,ssi2,ssi3', headers: { 'content-type': 'text/plain' } }, function(res, data) { res.headers.should.have.property('x-res-test2', 'res'); data.headers.should.have.property('x-req-test', 'req'); data.body.should.be.equal('include1.html,include2.html,include3.html'); });
test('auto type shoud be text/plain', t => { t.plan(3) sget({ method: 'GET', url: 'http://localhost:' + fastify.server.address().port + '/auto-type' }, (err, response, body) => { t.error(err) t.strictEqual(response.headers['content-type'], 'text/plain') t.deepEqual(body.toString(), 'hello world!') }) })
test('redirect to `/` - 4', t => { t.plan(4) sget({ method: 'GET', url: 'http://localhost:' + fastify.server.address().port + '/redirect-code' }, (err, response, body) => { t.error(err) t.strictEqual(response.statusCode, 200) t.strictEqual(response.headers['content-type'], 'text/plain') t.deepEqual(body.toString(), 'hello world!') }) })
test('redirect to `/` - 6', t => { t.plan(4) sget({ method: 'GET', url: 'http://localhost:' + fastify.server.address().port + '/redirect-code-before-call' }, (err, response, body) => { t.error(err) t.strictEqual(response.statusCode, 200) t.strictEqual(response.headers['content-type'], 'text/plain') t.deepEqual(body.toString(), 'hello world!') }) })