Rewrite "Location" header in apiserver proxy.
So that redirects work correctly.
This commit is contained in:
@@ -63,15 +63,18 @@ func TestProxyTransport(t *testing.T) {
|
||||
proxyHost: "foo.com",
|
||||
proxyPathPrepend: "/proxy/minion/minion1:8080",
|
||||
}
|
||||
|
||||
table := map[string]struct {
|
||||
type Item struct {
|
||||
input string
|
||||
sourceURL string
|
||||
transport *proxyTransport
|
||||
output string
|
||||
contentType string
|
||||
forwardedURI string
|
||||
}{
|
||||
redirect string
|
||||
redirectWant string
|
||||
}
|
||||
|
||||
table := map[string]Item{
|
||||
"normal": {
|
||||
input: `<pre><a href="kubelet.log">kubelet.log</a><a href="/google.log">google.log</a></pre>`,
|
||||
sourceURL: "http://myminion.com/logs/log.log",
|
||||
@@ -136,9 +139,30 @@ func TestProxyTransport(t *testing.T) {
|
||||
contentType: "text/html",
|
||||
forwardedURI: "/proxy/minion/minion1:10250/any/path/",
|
||||
},
|
||||
"redirect rel": {
|
||||
sourceURL: "http://myminion.com/redirect",
|
||||
transport: testTransport,
|
||||
redirect: "/redirected/target/",
|
||||
redirectWant: "http://foo.com/proxy/minion/minion1:10250/redirected/target/",
|
||||
forwardedURI: "/proxy/minion/minion1:10250/redirect",
|
||||
},
|
||||
"redirect abs same host": {
|
||||
sourceURL: "http://myminion.com/redirect",
|
||||
transport: testTransport,
|
||||
redirect: "http://myminion.com/redirected/target/",
|
||||
redirectWant: "http://foo.com/proxy/minion/minion1:10250/redirected/target/",
|
||||
forwardedURI: "/proxy/minion/minion1:10250/redirect",
|
||||
},
|
||||
"redirect abs other host": {
|
||||
sourceURL: "http://myminion.com/redirect",
|
||||
transport: testTransport,
|
||||
redirect: "http://example.com/redirected/target/",
|
||||
redirectWant: "http://example.com/redirected/target/",
|
||||
forwardedURI: "/proxy/minion/minion1:10250/redirect",
|
||||
},
|
||||
}
|
||||
|
||||
for name, item := range table {
|
||||
testItem := func(name string, item *Item) {
|
||||
// Canonicalize the html so we can diff.
|
||||
item.input = fmtHTML(item.input)
|
||||
item.output = fmtHTML(item.output)
|
||||
@@ -156,34 +180,51 @@ func TestProxyTransport(t *testing.T) {
|
||||
}
|
||||
|
||||
// Send response.
|
||||
if item.redirect != "" {
|
||||
http.Redirect(w, r, item.redirect, http.StatusMovedPermanently)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", item.contentType)
|
||||
fmt.Fprint(w, item.input)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
// Replace source URL with our test server address.
|
||||
sourceURL := parseURLOrDie(item.sourceURL)
|
||||
serverURL := parseURLOrDie(server.URL)
|
||||
item.input = strings.Replace(item.input, sourceURL.Host, serverURL.Host, -1)
|
||||
item.redirect = strings.Replace(item.redirect, sourceURL.Host, serverURL.Host, -1)
|
||||
sourceURL.Host = serverURL.Host
|
||||
|
||||
req, err := http.NewRequest("GET", sourceURL.String(), nil)
|
||||
if err != nil {
|
||||
t.Errorf("%v: Unexpected error: %v", name, err)
|
||||
continue
|
||||
return
|
||||
}
|
||||
resp, err := item.transport.RoundTrip(req)
|
||||
if err != nil {
|
||||
t.Errorf("%v: Unexpected error: %v", name, err)
|
||||
continue
|
||||
return
|
||||
}
|
||||
if item.redirect != "" {
|
||||
// Check that redirect URLs get rewritten properly.
|
||||
if got, want := resp.Header.Get("Location"), item.redirectWant; got != want {
|
||||
t.Errorf("%v: Location header = %q, want %q", name, got, want)
|
||||
}
|
||||
return
|
||||
}
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
t.Errorf("%v: Unexpected error: %v", name, err)
|
||||
continue
|
||||
return
|
||||
}
|
||||
if e, a := item.output, string(body); e != a {
|
||||
t.Errorf("%v: expected %v, but got %v", name, e, a)
|
||||
}
|
||||
server.Close()
|
||||
}
|
||||
|
||||
for name, item := range table {
|
||||
testItem(name, &item)
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user