-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathutils_test.go
51 lines (39 loc) · 1.32 KB
/
utils_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright 2023 The OWASP Coraza contributors
// SPDX-License-Identifier: Apache-2.0
package coraza
import (
"context"
"fmt"
"net/http"
"testing"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/stretchr/testify/require"
)
func TestParsegClientAddress(t *testing.T) {
remoteIp := "127.0.0.1"
remotePort := 9090
clientIp := "127.0.0.2"
clientPort := 8080
req, _ := http.NewRequest("GET", "/", nil)
req.RemoteAddr = fmt.Sprintf("%v:%v", remoteIp, remotePort)
ip, port := getClientAddress(req)
require.Equal(t, remoteIp, ip)
require.Equal(t, remotePort, port)
req.RemoteAddr = remoteIp
ip, port = getClientAddress(req)
require.Equal(t, remoteIp, ip)
require.Equal(t, 0, port)
req = req.WithContext(context.WithValue(req.Context(), caddyhttp.VarsCtxKey, make(map[string]any)))
req.RemoteAddr = fmt.Sprintf("%v:%v", remoteIp, remotePort)
ip, port = getClientAddress(req)
require.Equal(t, remoteIp, ip)
require.Equal(t, remotePort, port)
caddyhttp.SetVar(req.Context(), caddyhttp.ClientIPVarKey, fmt.Sprintf("%v:%v", clientIp, clientPort))
ip, port = getClientAddress(req)
require.Equal(t, clientIp, ip)
require.Equal(t, clientPort, port)
caddyhttp.SetVar(req.Context(), caddyhttp.ClientIPVarKey, clientIp)
ip, port = getClientAddress(req)
require.Equal(t, clientIp, ip)
require.Equal(t, 0, port)
}