Forward client HTTP headers through pipeline

This commit is contained in:
Alex
2017-03-31 09:10:09 +01:00
committed by Alex Ellis
parent a740783c78
commit c705cd8e33
6 changed files with 80 additions and 18 deletions

View File

@ -12,10 +12,11 @@ import (
// Before running these tests do a Docker stack deploy.
func fireRequest(url string, method string, reqBody string) (string, int, error) {
return fireRequestWithHeader(url, method, reqBody, "")
headers := make(map[string]string)
return fireRequestWithHeaders(url, method, reqBody, headers)
}
func fireRequestWithHeader(url string, method string, reqBody string, xheader string) (string, int, error) {
func fireRequestWithHeaders(url string, method string, reqBody string, headers map[string]string) (string, int, error) {
httpClient := http.Client{
Timeout: time.Second * 2, // Maximum of 2 secs
}
@ -26,9 +27,10 @@ func fireRequestWithHeader(url string, method string, reqBody string, xheader st
}
req.Header.Set("User-Agent", "go-integration")
if len(xheader) != 0 {
req.Header.Set("X-Function", xheader)
for kk, vv := range headers {
req.Header.Set(kk, vv)
}
res, getErr := httpClient.Do(req)
if getErr != nil {
log.Fatal(getErr)
@ -46,14 +48,33 @@ func TestGet_Rejected(t *testing.T) {
var reqBody string
_, code, err := fireRequest("http://localhost:8080/function/func_echoit", http.MethodGet, reqBody)
if code != http.StatusInternalServerError {
t.Log("Failed")
t.Logf("Failed got: %d", code)
}
if err != nil {
t.Log(err)
t.Fail()
}
}
func TestEchoIt_Post_Route_Handler_ForwardsClientHeaders(t *testing.T) {
reqBody := "test message"
headers := make(map[string]string, 0)
headers["X-Api-Key"] = "123"
body, code, err := fireRequestWithHeaders("http://localhost:8080/function/func_echoit", http.MethodPost, reqBody, headers)
if err != nil {
t.Log(err)
t.Fail()
}
if code != http.StatusOK {
t.Log("Failed")
}
if body != reqBody {
t.Log("Expected body returned")
t.Fail()
}
}
func TestEchoIt_Post_Route_Handler(t *testing.T) {
@ -73,9 +94,12 @@ func TestEchoIt_Post_Route_Handler(t *testing.T) {
}
}
func TestEchoIt_Post_Header_Handler(t *testing.T) {
func TestEchoIt_Post_X_Header_Routing_Handler(t *testing.T) {
reqBody := "test message"
body, code, err := fireRequestWithHeader("http://localhost:8080/", http.MethodPost, reqBody, "func_echoit")
headers := make(map[string]string, 0)
headers["X-Function"] = "func_echoit"
body, code, err := fireRequestWithHeaders("http://localhost:8080/", http.MethodPost, reqBody, headers)
if err != nil {
t.Log(err)