Ejemplo n.º 1
0
// Test that Status code is returned.
func TestProxyStatusCode(t *testing.T) {
	inv := newMockInventory(t, 3)
	responder := func(req *http.Request) (*http.Response, error) {
		res, err := httpmock.MockResponse(req)
		res.StatusCode = 404
		return res, err
	}
	httpmock.RegisterResponder("GET", responder)

	lb, err := NewLoadBalancer(defaultConfig.LoadBalancing, inv)
	if err != nil {
		t.Fatal(err)
	}
	proxy := NewReverseProxyConfig(*defaultConfig, lb)

	ts := httptest.NewServer(proxy)
	defer ts.Close()
	res, err := http.Get(ts.URL)
	if err != nil {
		t.Fatal(err)
	}
	response, err := ioutil.ReadAll(res.Body)
	res.Body.Close()
	if err != nil {
		t.Fatal(err)
	}
	if res.StatusCode != 404 {
		t.Fatal("Unexpected status code", res.StatusCode)
	}
	if string(response) != "ok" {
		t.Fatalf("expected response %q got %q", "ok", response)
	}
}
Ejemplo n.º 2
0
func getResponseMethod(method string) (func(req *http.Request) (*http.Response, error), chan bool) {
	checker := make(chan bool, 1)
	fn := func(req *http.Request) (*http.Response, error) {
		checker <- strings.EqualFold(method, req.Method)
		return httpmock.MockResponse(req)
	}
	return fn, checker
}
Ejemplo n.º 3
0
// Test that X-Forwarded-For is added.
func TestProxyAddForward(t *testing.T) {
	inv := newMockInventory(t, 3)
	var respOK = make(chan bool, 1)
	responder := func(req *http.Request) (*http.Response, error) {
		t.Log("X-Forwarded-For:", req.Header.Get("X-Forwarded-For"))
		respOK <- req.Header.Get("X-Forwarded-For") == "127.0.0.1"
		return httpmock.MockResponse(req)
	}
	httpmock.RegisterResponder("GET", responder)

	lb, err := NewLoadBalancer(defaultConfig.LoadBalancing, inv)
	if err != nil {
		t.Fatal(err)
	}
	conf := *defaultConfig
	conf.AddForwarded = true
	proxy := NewReverseProxyConfig(conf, lb)

	ts := httptest.NewServer(proxy)
	defer ts.Close()
	res, err := http.Get(ts.URL)
	if err != nil {
		t.Fatal(err)
	}
	response, err := ioutil.ReadAll(res.Body)
	res.Body.Close()
	if err != nil {
		t.Fatal(err)
	}
	if res.StatusCode != 200 {
		t.Fatal("Unexpected status code", res.StatusCode)
	}
	if string(response) != "ok" {
		t.Fatalf("expected response %q got %q", "ok", response)
	}
	wasok := <-respOK
	if !wasok {
		t.Fatal("request was nok ok")
	}
}
Ejemplo n.º 4
0
// Test a simple roundtrip
func TestProxyRoundtrip(t *testing.T) {
	inv := newMockInventory(t, 3)
	var respOK = make(chan bool, 1)
	responder := func(req *http.Request) (*http.Response, error) {
		t.Log("Path:", req.URL.Path)
		respOK <- req.URL.Path == "/somepath"
		return httpmock.MockResponse(req)
	}
	httpmock.RegisterResponder("GET", responder)

	lb, err := NewLoadBalancer(defaultConfig.LoadBalancing, inv)
	if err != nil {
		t.Fatal(err)
	}
	proxy := NewReverseProxyConfig(*defaultConfig, lb)

	ts := httptest.NewServer(proxy)
	defer ts.Close()
	res, err := http.Get(ts.URL + "/somepath")
	if err != nil {
		t.Fatal(err)
	}
	if res.StatusCode != 200 {
		t.Fatal("Unexpected status code", res.StatusCode)
	}
	response, err := ioutil.ReadAll(res.Body)
	res.Body.Close()
	if err != nil {
		t.Fatal(err)
	}
	if string(response) != "ok" {
		t.Fatalf("expected response %q got %q", "ok", response)
	}
	wasok := <-respOK
	if !wasok {
		t.Fatal("request was nok ok")
	}
}