MCPcopy Index your code
hub / github.com/labstack/echo / TestModifyResponseUseContext

Function TestModifyResponseUseContext

middleware/proxy_test.go:905–939  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

903}
904
905func TestModifyResponseUseContext(t *testing.T) {
906 server := httptest.NewServer(
907 http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
908 w.WriteHeader(http.StatusOK)
909 w.Write([]byte("OK"))
910 }),
911 )
912 defer server.Close()
913 targetURL, _ := url.Parse(server.URL)
914 e := echo.New()
915 e.Use(ProxyWithConfig(
916 ProxyConfig{
917 Balancer: &customBalancer{
918 target: &ProxyTarget{
919 Name: "tst",
920 URL: targetURL,
921 },
922 },
923 RetryCount: 1,
924 ModifyResponse: func(res *http.Response) error {
925 val := res.Request.Context().Value(testContextKey("FROM_BALANCER"))
926 if valStr, ok := val.(string); ok {
927 res.Header.Set("FROM_BALANCER", valStr)
928 }
929 return nil
930 },
931 },
932 ))
933 req := httptest.NewRequest(http.MethodGet, "/", nil)
934 rec := httptest.NewRecorder()
935 e.ServeHTTP(rec, req)
936 assert.Equal(t, http.StatusOK, rec.Code)
937 assert.Equal(t, "OK", rec.Body.String())
938 assert.Equal(t, "CUSTOM_BALANCER", rec.Header().Get("FROM_BALANCER"))
939}
940
941func createSimpleWebSocketServer(serveTLS bool) *httptest.Server {
942 handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

Callers

nothing calls this directly

Calls 11

ProxyWithConfigFunction · 0.85
testContextKeyTypeAlias · 0.85
SetMethod · 0.80
ServeHTTPMethod · 0.80
WriteHeaderMethod · 0.45
WriteMethod · 0.45
CloseMethod · 0.45
UseMethod · 0.45
StringMethod · 0.45
GetMethod · 0.45
HeaderMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…