Beispiel #1
0
func TestGetIP(t *testing.T) {
	for k, c := range []struct {
		expectedIP string
		request    *http.Request
		headers    map[string]string
	}{
		{"1234", &http.Request{RemoteAddr: "1234"}, map[string]string{"user-Agent": "firefox"}},
		{"1234", &http.Request{RemoteAddr: "4321"}, map[string]string{"x-FoRwarded-FoR": "1234"}},
		{"1234", &http.Request{RemoteAddr: "4321"}, map[string]string{"x-FoRwarded-FoR": "1234", "user-Agent": "firefox"}},
	} {
		h := http.Header{}
		for y, x := range c.headers {
			h.Set(y, x)
		}
		r := c.request
		r.Header = h
		assert.Equal(t, c.expectedIP, pkg.GetIP(r), "Case %d", k)
	}
}
Beispiel #2
0
func (e *Env) Req(req *http.Request) *Env {
	e.ctx.ClientIP = pkg.GetIP(req)
	e.ctx.UserAgent = req.Header.Get("User-Agent")
	e.ctx.Timestamp = time.Now()
	return e
}