mirror of
https://github.com/buger/goreplay.git
synced 2024-04-21 12:32:02 +00:00
Add a testable SetHeader function with associated tests
It seemed easier to pull this bit of functionality out and be able to test it on it's own than rely on the whole http tests
This commit is contained in:
+15
-8
@@ -131,14 +131,7 @@ func (o *HTTPOutput) sendRequest(client *http.Client, data []byte) {
|
||||
request.URL, _ = url.ParseRequestURI(URL)
|
||||
|
||||
for _, header := range o.headers {
|
||||
// Need to check here for the Host header as it needs to be set on the request and not as a separate header
|
||||
// http.ReadRequest sets it by default to the URL Host of the request being read
|
||||
if header.Name == "Host" {
|
||||
request.Host = header.Value
|
||||
} else {
|
||||
request.Header.Set(header.Name, header.Value)
|
||||
}
|
||||
|
||||
SetHeader(request, header.Name, header.Value)
|
||||
}
|
||||
|
||||
resp, err := client.Do(request)
|
||||
@@ -158,6 +151,20 @@ func (o *HTTPOutput) sendRequest(client *http.Client, data []byte) {
|
||||
|
||||
}
|
||||
|
||||
func SetHeader(request *http.Request, name string, value string) {
|
||||
|
||||
// Need to check here for the Host header as it needs to be set on the request and not as a separate header
|
||||
// http.ReadRequest sets it by default to the URL Host of the request being read
|
||||
if name == "Host" {
|
||||
request.Host = value
|
||||
} else {
|
||||
request.Header.Set(name, value)
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
func (o *HTTPOutput) String() string {
|
||||
return "HTTP output: " + o.address
|
||||
}
|
||||
|
||||
@@ -21,6 +21,27 @@ func startHTTP(cb func(*http.Request)) net.Listener {
|
||||
return listener
|
||||
}
|
||||
|
||||
func TestSetHeader(t *testing.T) {
|
||||
|
||||
req := &http.Request{
|
||||
Header: make(map[string][]string),
|
||||
}
|
||||
req.Host = "test.com"
|
||||
|
||||
SetHeader(req, "Host", "test2.com")
|
||||
|
||||
if req.Host != "test2.com" {
|
||||
t.Error("Expected test2.com - got ", req.Host)
|
||||
}
|
||||
|
||||
SetHeader(req, "test_header", "test_value")
|
||||
|
||||
if req.Header.Get("test_header") != "test_value" {
|
||||
t.Error("Wrong header value found")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestHTTPOutput(t *testing.T) {
|
||||
wg := new(sync.WaitGroup)
|
||||
quit := make(chan int)
|
||||
|
||||
Reference in New Issue
Block a user