Skip to content

Commit

Permalink
ctx: add test for "IsProxyTrusted" func
Browse files Browse the repository at this point in the history
  • Loading branch information
leonklingele committed Dec 3, 2022
1 parent e171b84 commit 9a1e5af
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions ctx_test.go
Expand Up @@ -17,6 +17,7 @@ import (
"errors"
"fmt"
"io"
"log"
"mime/multipart"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -3591,6 +3592,70 @@ func Benchmark_Ctx_XHR(b *testing.B) {
utils.AssertEqual(b, true, equal)
}

// go test -run Test_Ctx_IsProxyTrusted
func Test_Ctx_IsProxyTrusted(t *testing.T) {
t.Parallel()

{
app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
utils.AssertEqual(t, true, c.IsProxyTrusted())
}
{
app := New(Config{
EnableTrustedProxyCheck: false,
})
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
utils.AssertEqual(t, true, c.IsProxyTrusted())
}

{
app := New(Config{
EnableTrustedProxyCheck: true,
})
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
utils.AssertEqual(t, false, c.IsProxyTrusted())
}
{
app := New(Config{
EnableTrustedProxyCheck: true,
TrustedProxies: []string{
"127.0.0.1",
},
})
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
log.Println("Test_Ctx_IsProxyTrusted, IP", c.Context().RemoteIP().String())
utils.AssertEqual(t, true, c.IsProxyTrusted())
}
{
app := New(Config{
EnableTrustedProxyCheck: true,
TrustedProxies: []string{
"127.0.0.0/31",
},
})
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
log.Println("Test_Ctx_IsProxyTrusted, IP", c.Context().RemoteIP().String())
utils.AssertEqual(t, true, c.IsProxyTrusted())
}
{
app := New(Config{
EnableTrustedProxyCheck: true,
TrustedProxies: []string{
"127.0.0.0//31junk",
},
})
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
utils.AssertEqual(t, false, c.IsProxyTrusted())
}
}

// go test -v -run=^$ -bench=Benchmark_Ctx_SendString_B -benchmem -count=4
func Benchmark_Ctx_SendString_B(b *testing.B) {
app := New()
Expand Down

0 comments on commit 9a1e5af

Please sign in to comment.