-
Notifications
You must be signed in to change notification settings - Fork 619
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set the target host as Host header in the reverse proxy
- Loading branch information
Sebastian Mancke
committed
Mar 27, 2016
1 parent
d8d0c22
commit 61f6883
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package proxy | ||
|
||
import ( | ||
"net" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
) | ||
|
||
// Test, that the proxy calls the target | ||
// with the target host as host header, | ||
// and not with the ip of the request to the proxy itself | ||
func TestCorrectHostHeader(t *testing.T) { | ||
|
||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Write([]byte(r.Host)) | ||
})) | ||
|
||
defer server.Close() | ||
serverUrl, _ := url.Parse(server.URL) | ||
|
||
tr := &http.Transport{ | ||
Dial: (&net.Dialer{}).Dial, | ||
} | ||
proxy := newHTTPProxy(serverUrl, tr) | ||
|
||
req, _ := http.NewRequest("GET", "http://example.com:666", nil) | ||
res := httptest.NewRecorder() | ||
|
||
proxy.ServeHTTP(res, req) | ||
|
||
if serverUrl.Host != res.Body.String() { | ||
t.Errorf("expected host was %v, but got %v", serverUrl.Host, res.Body.String()) | ||
} | ||
} |