Comments (2)
I cannot submit a PR so here you go:
opts.test.js
/* global describe, it */
'use strict'
const request = require('supertest')
const bodyParser = require('body-parser')
const expect = require('chai').expect
const pump = require('pump')
let gateway, service, close, proxy, gHttpServer
const nock = require('nock')
nock('http://dev.com')
.get('/service/headers?age=33')
.reply(200, {
}, {
url: 'http://dev.com'
})
describe('fast-proxy smoke', () => {
it('init', async () => {
const fastProxy = require('../index')({
base: 'http://127.0.0.1:3000'
})
close = fastProxy.close
proxy = fastProxy.proxy
expect(proxy instanceof Function).to.equal(true)
expect(close instanceof Function).to.equal(true)
})
it('init & start gateway', async () => {
// init gateway
gateway = require('restana')()
gateway.all('/service/*', function (req, res) {
proxy(req, res, req.url, {
base: req.headers.base,
queryString: { age: 33 },
onResponse (req, res, stream, response) {
res.statusCode = response.statusCode
pump(stream, res)
}
})
})
gHttpServer = await gateway.start(8080)
})
it('init & start remote service', async () => {
// init remote service
service = require('restana')()
service.use(bodyParser.json())
service
.get('/service/headers', (req, res) => {
res.setHeader('url', req.url)
res.send()
})
.get('/service/302', (req, res) => {
res.statusCode = 302
res.end()
})
await service.start(3000)
})
it('should 200 on GET /servive/headers + opts', async () => {
await request(gHttpServer)
.get('/service/headers')
.expect(200)
.then((response) => {
expect(response.headers.url).to.equal('/service/headers?age=33')
})
})
it('should overwrite global base', async () => {
await request(gHttpServer)
.get('/service/headers')
.set('base', 'http://localhost:3000')
.expect(200)
.then((response) => {
expect(response.headers.url).to.equal('/service/headers?age=33')
})
})
it('should overwrite global base 2', async () => {
await request(gHttpServer)
.get('/service/headers')
.set('base', 'http://dev.com')
.then((response) => {
expect(response.headers.url).to.equal('http://dev.com')
})
})
it('should support http status in on response', async () => {
await request(gHttpServer)
.get('/service/302')
.expect(302)
})
it('close all', async () => {
close()
await gateway.close()
await service.close()
})
})
index.js line 113
onResponse(req, res, stream, response)
also add to .gitignore
# jetbrains webstorm
.idea
from fast-proxy.
Hi @yohay-ma, thanks for reporting this issue.
You are right, at the moment it is no clear how to reference the origin response code (it is actually part of the stream
object).
See example: https://github.com/jkyberneees/fast-gateway/blob/master/lib/default-hooks.js#L41
I have submitted a PR in order to preset the origin response code by default in #32, this simplifies custom onResponse
hook implementations as you just pointed out.
Best Regards.
from fast-proxy.
Related Issues (20)
- Should support undici timeout err.code HOT 4
- Server crash when aborting wrk when using fast-proxy with 0http with low server HOT 3
- Extend support for post with application/x-www-form-urlencoded HOT 3
- merge/collaborate with http2-proxy? HOT 5
- Your benchmarks are way off HOT 5
- someone can use req.url like '//10.10.10.10' to make SSRF attack, cause the host in headers is not expected. HOT 2
- lib/util.js security feature issue HOT 5
- Getting 500 `invalid connection header` using fast-proxy with undici HOT 2
- Is there an interface to use proxy in koa.js? HOT 3
- Undici crashes server HOT 4
- undici Pool not updated when using "base" option under Proxy function
- feat: support websocket proxies HOT 2
- How can I test this package locally? HOT 4
- Type for undici option does not support boolean values
- Only match beginning of URL when stripping additional forward slashes HOT 2
- Bump version? HOT 1
- [undici] Preflight requests from Safari HOT 4
- Response being written after being sent because res.sent is undefined
- Archiving this module / Do not use / New maintainer needed HOT 7
- allow for custom error handling
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fast-proxy.