whatismyip/router/headers_test.go

52 lines
1.2 KiB
Go
Raw Permalink Normal View History

2021-11-10 19:06:12 +00:00
package router
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/dcarrillo/whatismyip/internal/setting"
2021-11-10 19:06:12 +00:00
"github.com/stretchr/testify/assert"
)
func TestHeader(t *testing.T) {
req, _ := http.NewRequest("GET", "/user-agent", nil)
req.Header.Set("User-Agent", "test")
w := httptest.NewRecorder()
app.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code)
assert.Equal(t, contentType.text, w.Header().Get("Content-Type"))
assert.Equal(t, "test", w.Body.String())
}
func TestHeaders(t *testing.T) {
expected := `Header1: value1
Header2: value21
Header2: value22
Header3: value3
Host:
`
_, _ = setting.Setup([]string{
"-geoip2-city", "city",
"-geoip2-asn", "asn",
"-trusted-header", trustedHeader,
"-trusted-port-header", trustedPortHeader,
})
2021-11-10 19:06:12 +00:00
req, _ := http.NewRequest("GET", "/headers", nil)
2021-11-30 15:19:45 +00:00
req.Header = map[string][]string{
"Header1": {"value1"},
"Header2": {"value21", "value22"},
"Header3": {"value3"},
}
req.Header.Set(trustedHeader, "1.1.1.1")
req.Header.Set(trustedPortHeader, "1025")
2021-11-10 19:06:12 +00:00
w := httptest.NewRecorder()
app.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code)
assert.Equal(t, contentType.text, w.Header().Get("Content-Type"))
assert.Equal(t, expected, w.Body.String())
}