Commit 59355765 authored by Viacheslav Biriukov's avatar Viacheslav Biriukov

fix tests and change naming

parent 4790dacb
...@@ -34,15 +34,15 @@ type UpstreamHostDownFunc func(*UpstreamHost) bool ...@@ -34,15 +34,15 @@ type UpstreamHostDownFunc func(*UpstreamHost) bool
// UpstreamHost represents a single proxy upstream // UpstreamHost represents a single proxy upstream
type UpstreamHost struct { type UpstreamHost struct {
// The hostname of this upstream host // The hostname of this upstream host
Name string Name string
ReverseProxy *ReverseProxy ReverseProxy *ReverseProxy
Conns int64 Conns int64
Fails int32 Fails int32
FailTimeout time.Duration FailTimeout time.Duration
Unhealthy bool Unhealthy bool
ExtraHeaders http.Header ExtraHeaders http.Header
CheckDown UpstreamHostDownFunc CheckDown UpstreamHostDownFunc
Without string WithoutPathPrefix string
} }
// Down checks whether the upstream host is down or not. // Down checks whether the upstream host is down or not.
...@@ -78,7 +78,7 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { ...@@ -78,7 +78,7 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
if baseURL, err := url.Parse(host.Name); err == nil { if baseURL, err := url.Parse(host.Name); err == nil {
r.Host = baseURL.Host r.Host = baseURL.Host
if proxy == nil { if proxy == nil {
proxy = NewSingleHostReverseProxy(baseURL, host.Without) proxy = NewSingleHostReverseProxy(baseURL, host.WithoutPathPrefix)
} }
} else if proxy == nil { } else if proxy == nil {
return http.StatusInternalServerError, err return http.StatusInternalServerError, err
......
...@@ -120,7 +120,7 @@ func (u *fakeUpstream) Select() *UpstreamHost { ...@@ -120,7 +120,7 @@ func (u *fakeUpstream) Select() *UpstreamHost {
uri, _ := url.Parse(u.name) uri, _ := url.Parse(u.name)
return &UpstreamHost{ return &UpstreamHost{
Name: u.name, Name: u.name,
ReverseProxy: NewSingleHostReverseProxy(uri), ReverseProxy: NewSingleHostReverseProxy(uri, ""),
ExtraHeaders: proxyHeaders, ExtraHeaders: proxyHeaders,
} }
} }
...@@ -149,3 +149,9 @@ func (c *fakeConn) SetWriteDeadline(t time.Time) error { return nil } ...@@ -149,3 +149,9 @@ func (c *fakeConn) SetWriteDeadline(t time.Time) error { return nil }
func (c *fakeConn) Close() error { return nil } func (c *fakeConn) Close() error { return nil }
func (c *fakeConn) Read(b []byte) (int, error) { return c.readBuf.Read(b) } func (c *fakeConn) Read(b []byte) (int, error) { return c.readBuf.Read(b) }
func (c *fakeConn) Write(b []byte) (int, error) { return c.writeBuf.Write(b) } func (c *fakeConn) Write(b []byte) (int, error) { return c.writeBuf.Write(b) }
func newWithoutPathPrefixTestProxy(backendAddr string) *Proxy {
return &Proxy{
Upstreams: []Upstream{&fakeUpstreamWithoutPathPrefix{from: "/api/messages", withoutPathPrefix: "/api"}},
}
}
...@@ -76,7 +76,7 @@ func NewSingleHostReverseProxy(target *url.URL, without string) *ReverseProxy { ...@@ -76,7 +76,7 @@ func NewSingleHostReverseProxy(target *url.URL, without string) *ReverseProxy {
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
} }
if without != "" { if without != "" {
req.URL.Path = strings.Replace(req.URL.Path, without, "", 1) req.URL.Path = strings.TrimPrefix(req.URL.Path, without)
} }
} }
return &ReverseProxy{Director: director} return &ReverseProxy{Director: director}
......
...@@ -28,7 +28,7 @@ type staticUpstream struct { ...@@ -28,7 +28,7 @@ type staticUpstream struct {
Path string Path string
Interval time.Duration Interval time.Duration
} }
Without string WithoutPathPrefix string
} }
// NewStaticUpstreams parses the configuration input and sets up // NewStaticUpstreams parses the configuration input and sets up
...@@ -108,7 +108,7 @@ func NewStaticUpstreams(c parse.Dispenser) ([]Upstream, error) { ...@@ -108,7 +108,7 @@ func NewStaticUpstreams(c parse.Dispenser) ([]Upstream, error) {
if !c.NextArg() { if !c.NextArg() {
return upstreams, c.ArgErr() return upstreams, c.ArgErr()
} }
upstream.Without = c.Val() upstream.WithoutPathPrefix = c.Val()
} }
} }
...@@ -136,10 +136,10 @@ func NewStaticUpstreams(c parse.Dispenser) ([]Upstream, error) { ...@@ -136,10 +136,10 @@ func NewStaticUpstreams(c parse.Dispenser) ([]Upstream, error) {
return false return false
} }
}(upstream), }(upstream),
Without: upstream.Without, WithoutPathPrefix: upstream.WithoutPathPrefix,
} }
if baseURL, err := url.Parse(uh.Name); err == nil { if baseURL, err := url.Parse(uh.Name); err == nil {
uh.ReverseProxy = NewSingleHostReverseProxy(baseURL, uh.Without) uh.ReverseProxy = NewSingleHostReverseProxy(baseURL, uh.WithoutPathPrefix)
} else { } else {
return upstreams, err return upstreams, err
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment