func TestBreakFilterChain(t *testing.T) { s := startTestServer([]byte("Hello World!"), 0, func(r *http.Request) { t.Error("This should never be called") }) defer s.Close() fr := make(filters.Registry) fr.Register(builtin.NewRequestHeader()) resp1 := &http.Response{ Header: make(http.Header), Body: ioutil.NopCloser(new(bytes.Buffer)), StatusCode: http.StatusUnauthorized, Status: "Impossible body", } fr.Register(&breaker{resp1}) fr.Register(builtin.NewResponseHeader()) doc := fmt.Sprintf(`breakerDemo: Path("/breaker") -> requestHeader("X-Expected", "request header") -> responseHeader("X-Expected", "response header") -> breaker() -> requestHeader("X-Unexpected", "foo") -> responseHeader("X-Unexpected", "bar") -> "%s"`, s.URL) tp, err := newTestProxyWithFilters(fr, doc, FlagsNone) if err != nil { t.Error(err) return } defer tp.close() r, _ := http.NewRequest("GET", "https://www.example.org/breaker", nil) w := httptest.NewRecorder() tp.proxy.ServeHTTP(w, r) if _, has := r.Header["X-Expected"]; !has { t.Error("Request is missing the expected header (added during filter chain winding)") } if _, has := w.Header()["X-Expected"]; !has { t.Error("Response is missing the expected header (added during filter chain unwinding)") } if _, has := r.Header["X-Unexpected"]; has { t.Error("Request has an unexpected header from a filter after the breaker in the chain") } if _, has := w.Header()["X-Unexpected"]; has { t.Error("Response has an unexpected header from a filter after the breaker in the chain") } if w.Code != http.StatusUnauthorized && w.Body.String() != "Impossible body" { t.Errorf("Wrong status code/body. Expected 401 - Impossible body but got %d - %s", w.Code, w.Body.String()) } }
func TestAppliesFilters(t *testing.T) { payload := []byte("Hello World!") s := startTestServer(payload, 0, func(r *http.Request) { if h, ok := r.Header["X-Test-Request-Header"]; !ok || h[0] != "request header value" { t.Error("request header is missing") } }) defer s.Close() u, _ := url.ParseRequestURI("https://www.example.org/hello") r := &http.Request{ URL: u, Method: "GET", Header: http.Header{"X-Test-Header": []string{"test value"}}} w := httptest.NewRecorder() fr := make(filters.Registry) fr.Register(builtin.NewRequestHeader()) fr.Register(builtin.NewResponseHeader()) doc := fmt.Sprintf(`hello: Path("/hello") -> requestHeader("X-Test-Request-Header", "request header value") -> responseHeader("X-Test-Response-Header", "response header value") -> "%s"`, s.URL) dc, err := testdataclient.NewDoc(doc) if err != nil { t.Error(err) } p := New(routing.New(routing.Options{ fr, routing.MatchingOptionsNone, sourcePollTimeout, []routing.DataClient{dc}, nil, 0}), OptionsNone) delay() p.ServeHTTP(w, r) if h, ok := w.Header()["X-Test-Response-Header"]; !ok || h[0] != "response header value" { t.Error("missing response header") } }
func TestAppliesFilters(t *testing.T) { payload := []byte("Hello World!") s := startTestServer(payload, 0, func(r *http.Request) { if h, ok := r.Header["X-Test-Request-Header"]; !ok || h[0] != "request header value" { t.Error("request header is missing") } }) defer s.Close() u, _ := url.ParseRequestURI("https://www.example.org/hello") r := &http.Request{ URL: u, Method: "GET", Header: http.Header{"X-Test-Header": []string{"test value"}}} w := httptest.NewRecorder() fr := make(filters.Registry) fr.Register(builtin.NewRequestHeader()) fr.Register(builtin.NewResponseHeader()) doc := fmt.Sprintf(`hello: Path("/hello") -> requestHeader("X-Test-Request-Header", "request header value") -> responseHeader("X-Test-Response-Header", "response header value") -> "%s"`, s.URL) tp, err := newTestProxyWithFilters(fr, doc, FlagsNone) if err != nil { t.Error(err) return } defer tp.close() tp.proxy.ServeHTTP(w, r) if h, ok := w.Header()["X-Test-Response-Header"]; !ok || h[0] != "response header value" { t.Error("missing response header") } }