示例#1
0
func TestHandleResponseHeaders(t *testing.T) {
	backend := testutils.NewBackend(0, false)
	defer backend.Shutdown()

	server := NewServer(backend.Address())
	logRecord, _ := testutils.NewTestHAProxyLogRecord(backend.URL() + "/healthz")
	server.Handle(logRecord, 100*time.Millisecond)

	if logRecord.GetResponseHeaders()["Server-Status"][0] != "OK" {
		t.Errorf("should copy response headers")
	}
}
示例#2
0
func TestHandleNoNext(t *testing.T) {
	pool := NewPool("test", newTestConfig())
	defer pool.Shutdown()

	logRecord, rr := testutils.NewTestHAProxyLogRecord("")
	pool.Handle(logRecord)

	if logRecord.GetResponseStatusCode() != http.StatusServiceUnavailable ||
		rr.Code != http.StatusServiceUnavailable {
		t.Errorf("should return unavailable with no next")
	}
}
示例#3
0
func TestHandleDummy(t *testing.T) {
	pool := DummyPool("test")
	defer pool.Shutdown()

	logRecord, rr := testutils.NewTestHAProxyLogRecord("")
	pool.Handle(logRecord)

	if logRecord.GetResponseStatusCode() != http.StatusBadGateway ||
		rr.Code != http.StatusBadGateway {
		t.Errorf("should return bad gateway for dummy")
		t.Errorf("%s | %s", logRecord.GetResponseStatusCode(), rr.Code)
	}
}
示例#4
0
func TestHandleXForwardedFor(t *testing.T) {
	backend := testutils.NewBackend(0, false)
	defer backend.Shutdown()

	server := NewServer(backend.Address())
	logRecord, _ := testutils.NewTestHAProxyLogRecord(backend.URL())
	server.Handle(logRecord, 100*time.Millisecond)

	elm := backend.Handler.Recorded.Front()
	rec := elm.Value.(testutils.RequestAndTime).R
	if rec.Header["X-Forwarded-For"] == nil {
		t.Errorf("should set x-forwarded-for")
	}
}
示例#5
0
func TestHandleResponse(t *testing.T) {
	backend := testutils.NewBackend(0, false)
	backend.SetResponse(http.StatusOK, "The eagle has landed.")
	defer backend.Shutdown()

	server := NewServer(backend.Address())
	logRecord, rr := testutils.NewTestHAProxyLogRecord(backend.URL())
	server.Handle(logRecord, 100*time.Millisecond)

	if logRecord.GetResponseStatusCode() != http.StatusOK {
		t.Errorf("should set status code")
	}
	body, _ := ioutil.ReadAll(rr.Body)
	if string(body) != "The eagle has landed." {
		t.Errorf("should set response body")
	}
}
示例#6
0
func TestHandleTimeout(t *testing.T) {
	backend := testutils.NewBackend(100, false)
	backend.SetResponse(http.StatusOK, "Ba-ba-ba-ba-ba-na-na!")
	defer backend.Shutdown()

	server := NewServer(backend.Address())
	logRecord, rr := testutils.NewTestHAProxyLogRecord(backend.URL())
	server.Handle(logRecord, 10*time.Millisecond)

	if logRecord.GetResponseStatusCode() != http.StatusGatewayTimeout ||
		rr.Code != http.StatusGatewayTimeout {
		t.Errorf("should report status code 502")
	}

	body, _ := ioutil.ReadAll(rr.Body)
	if string(body) != "Gateway Timeout\n" {
		t.Errorf("should report 'Gateway Timeout'")
	}
}
示例#7
0
func TestHandle(t *testing.T) {
	pool := NewPool("test", newTestConfig())
	defer pool.Shutdown()

	backend := testutils.NewBackend(0, false)
	defer backend.Shutdown()

	backend.SetResponse(http.StatusOK, "Mickey Mouse!")

	pool.AddServer(backend.Address(), NewServer(backend.Address()))
	time.Sleep(50 * time.Millisecond)

	logRecord, rr := testutils.NewTestHAProxyLogRecord(backend.URL())
	pool.Handle(logRecord)

	body, _ := ioutil.ReadAll(rr.Body)
	if logRecord.GetResponseStatusCode() != http.StatusOK || rr.Code != http.StatusOK ||
		string(body) != "Mickey Mouse!" {
		t.Errorf("should forward requests to backend")
		t.Errorf("%d | %d | %s", logRecord.GetResponseStatusCode, rr.Code, string(body))
	}
}