diff --git a/output_http.go b/output_http.go index 2cc4a87..fc7aa75 100644 --- a/output_http.go +++ b/output_http.go @@ -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 } diff --git a/output_http_test.go b/output_http_test.go index a951d3f..42c8ed4 100644 --- a/output_http_test.go +++ b/output_http_test.go @@ -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)