Skip to content

Commit

Permalink
Set the target host as Host header in the reverse proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Mancke committed Mar 27, 2016
1 parent d8d0c22 commit 61f6883
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions proxy/http_test.go
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())
}
}

0 comments on commit 61f6883

Please sign in to comment.