mirror of
https://github.com/labstack/echo.git
synced 2025-04-21 12:17:04 +02:00
added ModifyResponse option to ProxyConfig (#1622)
Co-authored-by: Peter C <petoc@users.noreply.github.com>
This commit is contained in:
parent
8dd25c39ce
commit
6463bcb190
@ -45,6 +45,9 @@ type (
|
|||||||
// Examples: If custom TLS certificates are required.
|
// Examples: If custom TLS certificates are required.
|
||||||
Transport http.RoundTripper
|
Transport http.RoundTripper
|
||||||
|
|
||||||
|
// ModifyResponse defines function to modify response from ProxyTarget.
|
||||||
|
ModifyResponse func(*http.Response) error
|
||||||
|
|
||||||
rewriteRegex map[*regexp.Regexp]string
|
rewriteRegex map[*regexp.Regexp]string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,5 +20,6 @@ func proxyHTTP(tgt *ProxyTarget, c echo.Context, config ProxyConfig) http.Handle
|
|||||||
c.Set("_error", echo.NewHTTPError(http.StatusBadGateway, fmt.Sprintf("remote %s unreachable, could not forward: %v", desc, err)))
|
c.Set("_error", echo.NewHTTPError(http.StatusBadGateway, fmt.Sprintf("remote %s unreachable, could not forward: %v", desc, err)))
|
||||||
}
|
}
|
||||||
proxy.Transport = config.Transport
|
proxy.Transport = config.Transport
|
||||||
|
proxy.ModifyResponse = config.ModifyResponse
|
||||||
return proxy
|
return proxy
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
@ -109,6 +111,21 @@ func TestProxy(t *testing.T) {
|
|||||||
assert.Equal(t, "/user/jill/order/T%2FcO4lW%2Ft%2FVp%2F", req.URL.Path)
|
assert.Equal(t, "/user/jill/order/T%2FcO4lW%2Ft%2FVp%2F", req.URL.Path)
|
||||||
assert.Equal(t, http.StatusOK, rec.Code)
|
assert.Equal(t, http.StatusOK, rec.Code)
|
||||||
|
|
||||||
|
// ModifyResponse
|
||||||
|
e = echo.New()
|
||||||
|
e.Use(ProxyWithConfig(ProxyConfig{
|
||||||
|
Balancer: rrb,
|
||||||
|
ModifyResponse: func(res *http.Response) error {
|
||||||
|
res.Body = ioutil.NopCloser(bytes.NewBuffer([]byte("modified")))
|
||||||
|
res.Header.Set("X-Modified", "1")
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
rec = httptest.NewRecorder()
|
||||||
|
e.ServeHTTP(rec, req)
|
||||||
|
assert.Equal(t, "modified", rec.Body.String())
|
||||||
|
assert.Equal(t, "1", rec.Header().Get("X-Modified"))
|
||||||
|
|
||||||
// ProxyTarget is set in context
|
// ProxyTarget is set in context
|
||||||
contextObserver := func(next echo.HandlerFunc) echo.HandlerFunc {
|
contextObserver := func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||||
return func(c echo.Context) (err error) {
|
return func(c echo.Context) (err error) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user