mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-10-31 02:27:19 -04:00 
			
		
		
		
	Merge pull request #909 from evermax/master
Test that the host header forwarding on proxy
This commit is contained in:
		
						commit
						32329a473d
					
				| @ -31,7 +31,7 @@ func testPool() HostPool { | ||||
| 			Name: workableServer.URL, // this should resolve (healthcheck test) | ||||
| 		}, | ||||
| 		{ | ||||
| 			Name: "http://shouldnot.resolve", // this shouldn't | ||||
| 			Name: "http://localhost:99998", // this shouldn't | ||||
| 		}, | ||||
| 		{ | ||||
| 			Name: "http://C", | ||||
|  | ||||
| @ -576,6 +576,72 @@ func TestMultiReverseProxyFromClient(t *testing.T) { | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestHostSimpleProxyNoHeaderForward(t *testing.T) { | ||||
| 	var requestHost string | ||||
| 	backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||||
| 		requestHost = r.Host | ||||
| 		w.Write([]byte("Hello, client")) | ||||
| 	})) | ||||
| 	defer backend.Close() | ||||
| 
 | ||||
| 	// set up proxy | ||||
| 	p := &Proxy{ | ||||
| 		Next:      httpserver.EmptyNext, // prevents panic in some cases when test fails | ||||
| 		Upstreams: []Upstream{newFakeUpstream(backend.URL, false)}, | ||||
| 	} | ||||
| 
 | ||||
| 	r, err := http.NewRequest("GET", "/", nil) | ||||
| 	r.Host = "test.com" | ||||
| 
 | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("Failed to create request: %v", err) | ||||
| 	} | ||||
| 	w := httptest.NewRecorder() | ||||
| 
 | ||||
| 	p.ServeHTTP(w, r) | ||||
| 
 | ||||
| 	if !strings.Contains(backend.URL, "//") { | ||||
| 		t.Fatalf("The URL of the backend server doesn't contains //: %s", backend.URL) | ||||
| 	} | ||||
| 
 | ||||
| 	expectedHost := strings.Split(backend.URL, "//") | ||||
| 	if expectedHost[1] != requestHost { | ||||
| 		t.Fatalf("Expected %s as a Host header got %s\n", expectedHost[1], requestHost) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestHostHeaderReplacedUsingForward(t *testing.T) { | ||||
| 	var requestHost string | ||||
| 	backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||||
| 		requestHost = r.Host | ||||
| 		w.Write([]byte("Hello, client")) | ||||
| 	})) | ||||
| 	defer backend.Close() | ||||
| 
 | ||||
| 	upstream := newFakeUpstream(backend.URL, false) | ||||
| 	proxyHostHeader := "test2.com" | ||||
| 	upstream.host.UpstreamHeaders = http.Header{"Host": []string{proxyHostHeader}} | ||||
| 	// set up proxy | ||||
| 	p := &Proxy{ | ||||
| 		Next:      httpserver.EmptyNext, // prevents panic in some cases when test fails | ||||
| 		Upstreams: []Upstream{upstream}, | ||||
| 	} | ||||
| 
 | ||||
| 	r, err := http.NewRequest("GET", "/", nil) | ||||
| 	r.Host = "test.com" | ||||
| 
 | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("Failed to create request: %v", err) | ||||
| 	} | ||||
| 	w := httptest.NewRecorder() | ||||
| 
 | ||||
| 	p.ServeHTTP(w, r) | ||||
| 
 | ||||
| 	if proxyHostHeader != requestHost { | ||||
| 		t.Fatalf("Expected %s as a Host header got %s\n", proxyHostHeader, requestHost) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func newFakeUpstream(name string, insecure bool) *fakeUpstream { | ||||
| 	uri, _ := url.Parse(name) | ||||
| 	u := &fakeUpstream{ | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user