From 64c49509963da87bb45f32ec239995933798e5a6 Mon Sep 17 00:00:00 2001 From: yonbiaoxiao Date: Wed, 16 Sep 2020 10:36:43 +0800 Subject: [PATCH] improve the test coverage for context.go --- context_test.go | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/context_test.go b/context_test.go index 56ac4bebf..0044bf870 100644 --- a/context_test.go +++ b/context_test.go @@ -72,6 +72,15 @@ func BenchmarkAllocXML(b *testing.B) { } } +func BenchmarkRealIPForHeaderXForwardFor(b *testing.B) { + c := context{request: &http.Request{ + Header: http.Header{HeaderXForwardedFor: []string{"127.0.0.1, 127.0.1.1, "}}, + }} + for i := 0; i < b.N; i++ { + c.RealIP() + } +} + func (t *Template) Render(w io.Writer, name string, data interface{}, c Context) error { return t.templates.ExecuteTemplate(w, name, data) } @@ -847,6 +856,14 @@ func TestContext_RealIP(t *testing.T) { }, "127.0.0.1", }, + { + &context{ + request: &http.Request{ + Header: http.Header{HeaderXForwardedFor: []string{"127.0.0.1"}}, + }, + }, + "127.0.0.1", + }, { &context{ request: &http.Request{ @@ -871,12 +888,3 @@ func TestContext_RealIP(t *testing.T) { testify.Equal(t, tt.s, tt.c.RealIP()) } } - -func BenchmarkRealIPForHeaderXForwardFor(b *testing.B) { - c := context{request: &http.Request{ - Header: http.Header{HeaderXForwardedFor: []string{"127.0.0.1, 127.0.1.1, "}}, - }} - for i := 0; i < b.N; i++ { - c.RealIP() - } -}