示例#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)
	}
}
示例#2
0
文件: env.go 项目: lmineiro/hydra
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
}