Back to Repositories

Validating URI Construction and Encoding Workflows in Arthas

This test suite validates URI handling and encoding functionality in the Arthas tunnel server component. It focuses on testing URI construction, parameter encoding, and query string manipulation for agent registration and tunnel communication.

Test Coverage Overview

The test suite provides comprehensive coverage of URI manipulation scenarios in the Arthas tunnel server.

Key areas tested include:
  • Basic URI construction with query parameters
  • URI encoding for special characters
  • Multi-parameter URI generation for tunnel startup
  • Comparison between different URI building approaches

Implementation Analysis

The testing approach utilizes JUnit and AssertJ for robust URI validation. The tests implement a comparative strategy, constructing URIs through both direct URI class instantiation and Spring’s UriComponentsBuilder, ensuring consistent results across different construction methods.

Key patterns include:
  • Dual URI construction comparison
  • Special character encoding verification
  • Parameter concatenation testing

Technical Details

Testing tools and components:
  • JUnit 4 test framework
  • AssertJ assertions library
  • Spring Web UriComponentsBuilder
  • Java URI class
  • Custom URI constants for standardization

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Java URI handling.

Notable practices include:
  • Isolated test methods for specific scenarios
  • Consistent assertion patterns
  • Clear test method naming
  • Proper exception handling for URISyntaxException
  • Use of constant values for consistency

alibaba/arthas

tunnel-server/src/test/java/com/alibaba/arthas/tunnel/server/URITest.java

            
package com.alibaba.arthas.tunnel.server;

import java.net.URI;
import java.net.URISyntaxException;

import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.springframework.web.util.UriComponentsBuilder;

import com.alibaba.arthas.tunnel.common.MethodConstants;
import com.alibaba.arthas.tunnel.common.URIConstans;

/**
 * 
 * @author hengyunabc 2020-10-22
 *
 */
public class URITest {
    @Test
    public void test() throws URISyntaxException {
        String id = "xxx";
        URI responseUri = new URI("response", null, "/", "method=" + MethodConstants.AGENT_REGISTER + "&id=" + id,
                null);

        String string = responseUri.toString();

        String uriString = UriComponentsBuilder.newInstance().scheme("response").path("/")
                .queryParam("method", MethodConstants.AGENT_REGISTER).queryParam("id", id).build().toUriString();

        Assertions.assertThat(string).isEqualTo(uriString).isEqualTo("response:/?method=agentRegister&id=xxx");
    }

    @Test
    public void testEncode() throws URISyntaxException {
        String id = "xxx/%ff#ff";
        URI responseUri = new URI("response", null, "/", "method=" + MethodConstants.AGENT_REGISTER + "&id=" + id,
                null);

        String string = responseUri.toString();

        String uriString = UriComponentsBuilder.newInstance().scheme(URIConstans.RESPONSE).path("/")
                .queryParam(URIConstans.METHOD, MethodConstants.AGENT_REGISTER).queryParam(URIConstans.ID, id).build()
                .encode().toUriString();

        Assertions.assertThat(string).isEqualTo(uriString)
                .isEqualTo("response:/?method=agentRegister&id=xxx/%25ff%23ff");
    }

    @Test
    public void test3() throws URISyntaxException {

        String agentId = "ffff";
        String clientConnectionId = "ccccc";
        URI uri = new URI("response", null, "/", "method=" + MethodConstants.START_TUNNEL + "&id=" + agentId
                + "&clientConnectionId=" + clientConnectionId, null);

        String string = uri.toString();

        String uriString = UriComponentsBuilder.newInstance().scheme(URIConstans.RESPONSE).path("/")
                .queryParam(URIConstans.METHOD, MethodConstants.START_TUNNEL).queryParam(URIConstans.ID, agentId)
                .queryParam(URIConstans.CLIENT_CONNECTION_ID, clientConnectionId).build().toUriString();

        System.err.println(string);
        Assertions.assertThat(string).isEqualTo(uriString)
                .isEqualTo("response:/?method=startTunnel&id=ffff&clientConnectionId=ccccc");
    }
}