コード例 #1
0
ファイル: proxy_test.go プロジェクト: hafizh/skipper
func makeTestSettingsDispatcher(url string, filters []skipper.Filter, shunt bool) skipper.SettingsDispatcher {
	sd := dispatch.Make()
	sd.Push() <- mock.MakeSettings(url, filters, shunt)

	// todo: don't let to get into busy loop
	c := make(chan skipper.Settings)
	sd.Subscribe(c)
	for {
		if s := <-c; s != nil {
			return sd
		}
	}
}
コード例 #2
0
ファイル: proxy_test.go プロジェクト: hafizh/skipper
func TestRoute(t *testing.T) {
	payload1 := []byte("host one")
	s1 := startTestServer(payload1, 0, voidCheck)
	defer s1.Close()

	payload2 := []byte("host two")
	s2 := startTestServer(payload2, 0, voidCheck)
	defer s2.Close()

	sd := makeTestSettingsDispatcher("", nil, false)
	ts := mock.MakeSettings("", nil, false)
	ts.RouterImpl.AddRoute("Path(\"/host-one<any>\")", &mock.Route{urlToBackend(s1.URL), nil})
	ts.RouterImpl.AddRoute("Path(\"/host-two<any>\")", &mock.Route{urlToBackend(s2.URL), nil})
	sd.Push() <- ts

	p := Make(sd, false)

	var (
		r *http.Request
		w *httptest.ResponseRecorder
		u *url.URL
	)

	u, _ = url.ParseRequestURI("http://localhost:9090/host-one")
	r = &http.Request{
		URL:    u,
		Method: "GET"}
	w = httptest.NewRecorder()
	p.ServeHTTP(w, r)
	if w.Code != 200 || !bytes.Equal(w.Body.Bytes(), payload1) {
		t.Error("wrong routing 1")
	}

	u, _ = url.ParseRequestURI("http://localhost:9090/host-two")
	r = &http.Request{
		URL:    u,
		Method: "GET"}
	w = httptest.NewRecorder()
	p.ServeHTTP(w, r)
	if w.Code != 200 || !bytes.Equal(w.Body.Bytes(), payload2) {
		t.Error("wrong routing 2")
	}
}