Skip to content

Instantly share code, notes, and snippets.

@simon04
Created Aug 21, 2020
Embed
What would you like to do?
Java URI copy constructor

Using the URI copy constructor on an URI where the userinfo part contains a percent-escaped colon (U+003A as %3A) produces a different result.

import org.junit.Assert;
import org.junit.jupiter.api.Test;
import java.net.URI;
import java.net.URISyntaxException;
public class URITest {
@Test
void test() throws URISyntaxException {
URI u = new URI("http://user%3Acolon:password@example.com/");
URI copy = new URI(u.getScheme(), u.getUserInfo(), u.getHost(), u.getPort(), u.getPath(), u.getQuery(), u.getFragment());
Assert.assertEquals(u, copy);
// java.lang.AssertionError: expected:<http://user%3Acolon:password@example.com/> but was:<http://user:colon:password@example.com/>
// Expected :http://user%3Acolon:password@example.com/
// Actual :http://user:colon:password@example.com/
}
}
@simon04
Copy link
Author

simon04 commented Aug 24, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment