Back to Repositories

Testing HTTP Header Client IP Extraction in Alibaba Arthas

This test suite validates the HTTP utilities in Arthas tunnel server, specifically focusing on client IP address extraction from HTTP headers. The tests verify proper handling of X-Forwarded-For headers in different scenarios, ensuring reliable client IP detection in proxy environments.

Test Coverage Overview

The test suite provides comprehensive coverage of the HttpUtils.findClientIP() method functionality.

Key areas tested include:
  • Multiple IP addresses in X-Forwarded-For header
  • Single IP address handling
  • Null header handling
The tests ensure proper extraction of the originating client IP from proxy chains.

Implementation Analysis

The testing approach uses Mockito for HTTP header simulation, allowing controlled testing of different header scenarios. The implementation follows AAA (Arrange-Act-Assert) pattern with mock setup, method execution, and assertion verification. AssertJ provides fluent assertions for result validation.

Technical patterns include:
  • Mockito stub configuration for header responses
  • Isolated unit testing of header parsing logic
  • Boundary condition verification

Technical Details

Testing tools and frameworks:
  • JUnit 4 for test execution
  • Mockito for HTTP header mocking
  • AssertJ for assertions
  • Netty’s HttpHeaders for header representation
The setup requires minimal configuration with mock objects handling the header scenarios.

Best Practices Demonstrated

The test suite exemplifies several testing best practices for header parsing validation.

Notable practices include:
  • Isolated test cases for distinct scenarios
  • Clear test method naming
  • Mock usage for external dependencies
  • Comprehensive edge case coverage
  • Clean and maintainable test structure

alibaba/arthas

tunnel-server/src/test/java/com/alibaba/arthas/tunnel/server/utils/HttpUtilsTest.java

            
package com.alibaba.arthas.tunnel.server.utils;

import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.mockito.Mockito;

import io.netty.handler.codec.http.HttpHeaders;

/**
 * 
 * @author hengyunabc 2021-02-26
 *
 */
public class HttpUtilsTest {

    @Test
    public void test1() {
        HttpHeaders headers = Mockito.mock(HttpHeaders.class);
        Mockito.when(headers.get("X-Forwarded-For")).thenReturn("30.25.233.172, 11.162.179.161");

        String ip = HttpUtils.findClientIP(headers);

        Assertions.assertThat(ip).isEqualTo("30.25.233.172");
    }

    @Test
    public void test2() {
        HttpHeaders headers = Mockito.mock(HttpHeaders.class);
        Mockito.when(headers.get("X-Forwarded-For")).thenReturn("30.25.233.172");

        String ip = HttpUtils.findClientIP(headers);

        Assertions.assertThat(ip).isEqualTo("30.25.233.172");

    }

    @Test
    public void test3() {
        HttpHeaders headers = Mockito.mock(HttpHeaders.class);
        Mockito.when(headers.get("X-Forwarded-For")).thenReturn(null);

        String ip = HttpUtils.findClientIP(headers);

        Assertions.assertThat(ip).isEqualTo(null);

    }
}