Commit 59355765 authored by Viacheslav Biriukov's avatar Viacheslav Biriukov

fix tests and change naming

parent 4790dacb
......@@ -42,7 +42,7 @@ type UpstreamHost struct {
Unhealthy bool
ExtraHeaders http.Header
CheckDown UpstreamHostDownFunc
Without string
WithoutPathPrefix string
}
// 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) {
if baseURL, err := url.Parse(host.Name); err == nil {
r.Host = baseURL.Host
if proxy == nil {
proxy = NewSingleHostReverseProxy(baseURL, host.Without)
proxy = NewSingleHostReverseProxy(baseURL, host.WithoutPathPrefix)
}
} else if proxy == nil {
return http.StatusInternalServerError, err
......
......@@ -120,7 +120,7 @@ func (u *fakeUpstream) Select() *UpstreamHost {
uri, _ := url.Parse(u.name)
return &UpstreamHost{
Name: u.name,
ReverseProxy: NewSingleHostReverseProxy(uri),
ReverseProxy: NewSingleHostReverseProxy(uri, ""),
ExtraHeaders: proxyHeaders,
}
}
......@@ -149,3 +149,9 @@ func (c *fakeConn) SetWriteDeadline(t time.Time) 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) 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 {
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
}
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}
......
......@@ -28,7 +28,7 @@ type staticUpstream struct {
Path string
Interval time.Duration
}
Without string
WithoutPathPrefix string
}
// NewStaticUpstreams parses the configuration input and sets up
......@@ -108,7 +108,7 @@ func NewStaticUpstreams(c parse.Dispenser) ([]Upstream, error) {
if !c.NextArg() {
return upstreams, c.ArgErr()
}
upstream.Without = c.Val()
upstream.WithoutPathPrefix = c.Val()
}
}
......@@ -136,10 +136,10 @@ func NewStaticUpstreams(c parse.Dispenser) ([]Upstream, error) {
return false
}
}(upstream),
Without: upstream.Without,
WithoutPathPrefix: upstream.WithoutPathPrefix,
}
if baseURL, err := url.Parse(uh.Name); err == nil {
uh.ReverseProxy = NewSingleHostReverseProxy(baseURL, uh.Without)
uh.ReverseProxy = NewSingleHostReverseProxy(baseURL, uh.WithoutPathPrefix)
} else {
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