aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/traccar/helper/WebHelperTest.java
blob: 3a7329cb82f3430177f0233348911d1b1cfbc906 (plain)
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
package org.traccar.helper;

import org.junit.jupiter.api.Test;

import javax.servlet.http.HttpServletRequest;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class WebHelperTest {

    @Test
    public void testRetrieveRemoteAddressProxyMultiple() {
        HttpServletRequest request = mock(HttpServletRequest.class);
        when(request.getRemoteAddr()).thenReturn("147.120.1.5");
        when(request.getHeader("X-FORWARDED-FOR")).thenReturn("231.23.45.65, 10.20.10.33, 10.20.20.34");

        assertEquals("231.23.45.65", WebHelper.retrieveRemoteAddress(request));
    }

    @Test
    public void testRetrieveRemoteAddressProxySingle() {
        HttpServletRequest request = mock(HttpServletRequest.class);
        when(request.getRemoteAddr()).thenReturn("147.120.1.5");
        when(request.getHeader("X-FORWARDED-FOR")).thenReturn("231.23.45.65");

        assertEquals("231.23.45.65", WebHelper.retrieveRemoteAddress(request));
    }

    @Test
    public void testRetrieveRemoteAddressNoProxy() {
        HttpServletRequest request = mock(HttpServletRequest.class);
        when(request.getRemoteAddr()).thenReturn("231.23.45.65");

        assertEquals("231.23.45.65", WebHelper.retrieveRemoteAddress(request));
    }

}