예제 #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)
	}
}
예제 #2
0
// Test that various methods make it through
func TestProxyMethods(t *testing.T) {
	inv := newMockInventory(t, 3)
	var checkers = make([]chan bool, len(testMethods))
	for i, method := range testMethods {
		fn, oker := getResponseMethod(method)
		checkers[i] = oker
		httpmock.RegisterResponder(method, fn)
	}

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

	ts := httptest.NewServer(proxy)
	defer ts.Close()
	for i, method := range testMethods {
		body := bytes.NewBufferString("somebody")
		if method == "HEAD" {
			body = bytes.NewBufferString("")
		}
		req, err := http.NewRequest(method, ts.URL, body)
		if err != nil {
			t.Fatal(err)
		}
		res, err := http.DefaultClient.Do(req)
		if err != nil {
			if runtime.GOOS == "windows" && err.Error() == "EOF" && method == "PATCH" {
				t.Log("Let me guess. You're runnning Bitdefender as AV? ;)")
				continue
			} else {
				t.Fatal("method", method, "error:", err)
			}
		}
		if res.StatusCode != 200 {
			t.Fatal("method", method, "unexpected status code", res.StatusCode)
		}
		_, err = ioutil.ReadAll(res.Body)
		res.Body.Close()
		if err != nil {
			t.Fatal(err)
		}
		wasok := <-checkers[i]
		if !wasok {
			t.Fatal("request for method", method, "was nok ok")
		}
	}
}
예제 #3
0
// Test that various methods make it through
func TestProxyMethods(t *testing.T) {
	inv := newMockInventory(t, 3)
	var checkers = make([]chan bool, len(testMethods))
	for i, method := range testMethods {
		fn, oker := getResponseMethod(method)
		checkers[i] = oker
		httpmock.RegisterResponder(method, fn)
	}

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

	ts := httptest.NewServer(proxy)
	defer ts.Close()
	for i, method := range testMethods {
		req, err := http.NewRequest(method, ts.URL, nil)
		if err != nil {
			t.Fatal(err)
		}
		res, err := http.DefaultClient.Do(req)
		if err != nil {
			t.Fatal(err)
		}
		if res.StatusCode != 200 {
			t.Fatal("method", method, ", unexpected status code", res.StatusCode)
		}
		_, err = ioutil.ReadAll(res.Body)
		res.Body.Close()
		if err != nil {
			t.Fatal(err)
		}
		wasok := <-checkers[i]
		if !wasok {
			t.Fatal("request for method", method, "was nok ok")
		}
	}
}
예제 #4
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")
	}
}
예제 #5
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")
	}
}