Giter Club home page Giter Club logo

Comments (2)

yohay-ma avatar yohay-ma commented on August 14, 2024

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.

jkyberneees avatar jkyberneees commented on August 14, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.