MCPcopy Index your code
hub / github.com/zalando/skipper / TestBreakFilterChain

Function TestBreakFilterChain

proxy/proxy_test.go:970–1026  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

968func (*shunter) Name() string { return "shunter" }
969
970func TestBreakFilterChain(t *testing.T) {
971 s := startTestServer([]byte("Hello World!"), 0, func(r *http.Request) {
972 t.Error("This should never be called")
973 })
974 defer s.Close()
975
976 fr := make(filters.Registry)
977 fr.Register(builtin.NewAppendRequestHeader())
978 resp1 := &http.Response{
979 Header: make(http.Header),
980 Body: io.NopCloser(new(bytes.Buffer)),
981 StatusCode: http.StatusUnauthorized,
982 Status: "Impossible body",
983 }
984 fr.Register(&shunter{resp1})
985 fr.Register(builtin.NewAppendResponseHeader())
986
987 doc := fmt.Sprintf(`breakerDemo:
988 Path("/shunter") ->
989 appendRequestHeader("X-Expected", "request header") ->
990 appendResponseHeader("X-Expected", "response header") ->
991 shunter() ->
992 appendRequestHeader("X-Unexpected", "foo") ->
993 appendResponseHeader("X-Unexpected", "bar") ->
994 "%s"`, s.URL)
995 tp, err := newTestProxyWithFilters(fr, doc, FlagsNone)
996 if err != nil {
997 t.Error(err)
998 return
999 }
1000
1001 defer tp.close()
1002
1003 r, _ := http.NewRequest("GET", "https://www.example.org/shunter", nil)
1004 w := httptest.NewRecorder()
1005 tp.proxy.ServeHTTP(w, r)
1006
1007 if _, has := r.Header["X-Expected"]; !has {
1008 t.Error("Request is missing the expected header (added during filter chain winding)")
1009 }
1010
1011 if _, has := w.Header()["X-Expected"]; !has {
1012 t.Error("Response is missing the expected header (added during filter chain unwinding)")
1013 }
1014
1015 if _, has := r.Header["X-Unexpected"]; has {
1016 t.Error("Request has an unexpected header from a filter after the shunter in the chain")
1017 }
1018
1019 if _, has := w.Header()["X-Unexpected"]; has {
1020 t.Error("Response has an unexpected header from a filter after the shunter in the chain")
1021 }
1022
1023 if w.Code != http.StatusUnauthorized && w.Body.String() != "Impossible body" {
1024 t.Errorf("Wrong status code/body. Expected 401 - Impossible body but got %d - %s", w.Code, w.Body.String())
1025 }
1026}
1027

Callers

nothing calls this directly

Calls 12

NewAppendRequestHeaderFunction · 0.92
NewAppendResponseHeaderFunction · 0.92
newTestProxyWithFiltersFunction · 0.85
RegisterMethod · 0.80
startTestServerFunction · 0.70
ErrorMethod · 0.65
CloseMethod · 0.65
StringMethod · 0.65
ErrorfMethod · 0.65
closeMethod · 0.45
ServeHTTPMethod · 0.45
HeaderMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…