MCPcopy
hub / github.com/cloudflare/cloudflared / TestHTTPServiceHostHeaderOverride

Function TestHTTPServiceHostHeaderOverride

ingress/origin_proxy_test.go:107–147  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

105}
106
107func TestHTTPServiceHostHeaderOverride(t *testing.T) {
108 cfg := OriginRequestConfig{
109 HTTPHostHeader: t.Name(),
110 }
111 handler := func(w http.ResponseWriter, r *http.Request) {
112 require.Equal(t, r.Host, t.Name())
113 if websocket.IsWebSocketUpgrade(r) {
114 respHeaders := websocket.NewResponseHeader(r)
115 for k, v := range respHeaders {
116 w.Header().Set(k, v[0])
117 }
118 w.WriteHeader(http.StatusSwitchingProtocols)
119 return
120 }
121 // return the X-Forwarded-Host header for assertions
122 // as the httptest Server URL isn't available here yet
123 w.Write([]byte(r.Header.Get("X-Forwarded-Host")))
124 }
125 origin := httptest.NewServer(http.HandlerFunc(handler))
126 defer origin.Close()
127
128 originURL, err := url.Parse(origin.URL)
129 require.NoError(t, err)
130
131 httpService := &httpService{
132 url: originURL,
133 }
134 shutdownC := make(chan struct{})
135 require.NoError(t, httpService.start(TestLogger, shutdownC, cfg))
136
137 req, err := http.NewRequest(http.MethodGet, originURL.String(), nil)
138 require.NoError(t, err)
139
140 resp, err := httpService.RoundTrip(req)
141 require.NoError(t, err)
142 require.Equal(t, http.StatusOK, resp.StatusCode)
143
144 respBody, err := io.ReadAll(resp.Body)
145 require.NoError(t, err)
146 require.Equal(t, respBody, []byte(originURL.Host))
147}
148
149// TestHTTPServiceUsesIngressRuleScheme makes sure httpService uses scheme defined in ingress rule and not by eyeball request
150func TestHTTPServiceUsesIngressRuleScheme(t *testing.T) {

Callers

nothing calls this directly

Calls 13

startMethod · 0.95
RoundTripMethod · 0.95
IsWebSocketUpgradeFunction · 0.92
NewResponseHeaderFunction · 0.92
GetMethod · 0.80
NameMethod · 0.65
EqualMethod · 0.65
WriteMethod · 0.65
CloseMethod · 0.65
StringMethod · 0.65
SetMethod · 0.45
HeaderMethod · 0.45

Tested by

no test coverage detected