Back to Repositories

Testing Client IP Resolution Utils in Apollo Config

This test suite evaluates the WebUtils class functionality in Apollo, focusing on client IP address extraction from HTTP requests. It verifies the handling of X-FORWARDED-FOR headers and fallback mechanisms for IP address resolution in web requests.

Test Coverage Overview

The test suite provides targeted coverage for IP address extraction logic in web requests.

  • Tests X-FORWARDED-FOR header parsing with multiple IPs
  • Verifies fallback to remote address when header is absent
  • Validates correct extraction of the first IP from forwarded chain

Implementation Analysis

The implementation uses JUnit with MockitoJUnitRunner for test execution. MockHttpServletRequest is utilized to simulate HTTP requests with different IP configurations.

The testing approach focuses on two key scenarios with explicit assertions to verify the IP extraction behavior.

Technical Details

  • JUnit 4 test framework
  • MockitoJUnitRunner for test execution
  • Spring MockHttpServletRequest for HTTP request simulation
  • AssertJ for fluent assertions
  • Configured with standard HTTP headers and remote addresses

Best Practices Demonstrated

The test demonstrates clean testing practices with clear scenario separation and explicit test naming.

  • Proper mock object initialization
  • Clear test method naming convention
  • Focused test scenarios with single responsibility
  • Effective use of assertion libraries

apolloconfig/apollo

apollo-common/src/test/java/com/ctrip/framework/apollo/common/utils/WebUtilsTest.java

            
/*
 * Copyright 2024 Apollo Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.ctrip.framework.apollo.common.utils;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.mock.web.MockHttpServletRequest;

/**
 * @author kl (http://kailing.pub)
 * @since 2022/8/12
 */
@RunWith(MockitoJUnitRunner.class)
public class WebUtilsTest {

  @Test
  public void testTryToGetClientIp() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("X-FORWARDED-FOR", "172.1.1.1,172.1.1.2");
    request.setRemoteAddr("172.1.1.3");
    String ip = WebUtils.tryToGetClientIp(request);
    assertThat(ip).isEqualTo("172.1.1.1");

    request.removeHeader("X-FORWARDED-FOR");
    ip = WebUtils.tryToGetClientIp(request);
    assertThat(ip).isEqualTo("172.1.1.3");
  }
}