Replace custom proxy with httputil.ReverseProxy for kubecfg/kubectl.

Fixes #1149 - kubecfg proxy "411 Length Required" error on POST/PUT.
This commit is contained in:
alex
2014-10-21 18:52:18 +01:00
parent d5377e4a39
commit fb2b15a797
8 changed files with 254 additions and 128 deletions

View File

@@ -17,43 +17,90 @@ limitations under the License.
package kubectl
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"path/filepath"
"strings"
"testing"
)
func TestFileServing(t *testing.T) {
data := "This is test data"
const (
fname = "test.txt"
data = "This is test data"
)
dir, err := ioutil.TempDir("", "data")
if err != nil {
t.Errorf("Unexpected error: %v", err)
t.Fatalf("error creating tmp dir: %v", err)
}
err = ioutil.WriteFile(dir+"/test.txt", []byte(data), 0755)
if err != nil {
t.Errorf("Unexpected error: %v", err)
if err := ioutil.WriteFile(filepath.Join(dir, fname), []byte(data), 0755); err != nil {
t.Fatalf("error writing tmp file: %v", err)
}
prefix := "/foo/"
const prefix = "/foo/"
handler := newFileHandler(prefix, dir)
server := httptest.NewServer(handler)
client := http.Client{}
req, err := http.NewRequest("GET", server.URL+prefix+"test.txt", nil)
defer server.Close()
url := server.URL + prefix + fname
res, err := http.Get(url)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
res, err := client.Do(req)
if err != nil {
t.Errorf("Unexpected error: %v", err)
t.Fatalf("http.Get(%q) error: %v", url, err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
t.Errorf("res.StatusCode = %d; want %d", res.StatusCode, http.StatusOK)
}
b, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if res.StatusCode != http.StatusOK {
t.Errorf("Unexpected status: %d", res.StatusCode)
t.Fatalf("error reading resp body: %v", err)
}
if string(b) != data {
t.Errorf("Data doesn't match: %s vs %s", string(b), data)
t.Errorf("have %q; want %q", string(b), data)
}
}
func TestAPIRequests(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "%s %s %s", r.Method, r.RequestURI, string(b))
}))
defer ts.Close()
// httptest.NewServer should always generate a valid URL.
target, _ := url.Parse(ts.URL)
proxy := newProxyServer(target)
tests := []struct{ method, body string }{
{"GET", ""},
{"DELETE", ""},
{"POST", "test payload"},
{"PUT", "test payload"},
}
const path = "/api/test?fields=ID%3Dfoo&labels=key%3Dvalue"
for i, tt := range tests {
r, err := http.NewRequest(tt.method, path, strings.NewReader(tt.body))
if err != nil {
t.Errorf("error creating request: %v", err)
continue
}
w := httptest.NewRecorder()
proxy.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Errorf("%d: proxy.ServeHTTP w.Code = %d; want %d", i, w.Code, http.StatusOK)
}
want := strings.Join([]string{tt.method, path, tt.body}, " ")
if w.Body.String() != want {
t.Errorf("%d: response body = %q; want %q", i, w.Body.String(), want)
}
}
}