Skip to content

Instantly share code, notes, and snippets.

@nickgrealy
Created December 16, 2016 05:12
Show Gist options
  • Save nickgrealy/b4f35bf5e5e5a444aff20c4616b51513 to your computer and use it in GitHub Desktop.
Save nickgrealy/b4f35bf5e5e5a444aff20c4616b51513 to your computer and use it in GitHub Desktop.
URI Builder Tests - comparing different implementations.
@Grab('javax:javaee-api:7.0')
@Grab('org.glassfish.jersey.core:jersey-common:2.22.2')
@Grab('org.apache.httpcomponents:httpclient:4.5.2')
@Grab('org.springframework:spring-web:4.2.5.RELEASE')
import java.net.URI;
import javax.ws.rs.core.UriBuilder;
import org.apache.http.client.utils.URIBuilder;
import org.springframework.web.util.UriComponentsBuilder;
def javaee = [
['http://example.com', 'http://example.com/?name=John'],
['http://example.com#fragment', 'http://example.com/?name=John#fragment'],
['http://[email protected]', 'http://example.com/[email protected]&name=John'],
['http://[email protected]#fragment', 'http://example.com/[email protected]&name=John#fragment']
]
def spring = [
['http://example.com', 'http://example.com?name=John'],
['http://example.com#fragment', 'http://example.com?name=John#fragment'],
['http://[email protected]', 'http://example.com?email=john.doe%40email.com&name=John'],
['http://[email protected]#fragment', 'http://example.com?email=john.doe%40email.com&name=John#fragment']
]
def apache = [
['http://example.com', 'http://example.com?name=John'],
['http://example.com#fragment', 'http://example.com?name=John#fragment'],
['http://[email protected]', 'http://[email protected]&name=John'],
['http://[email protected]#fragment', 'http://[email protected]&name=John#fragment']
]
String key = "name", value = "John"
// verify javaee
javaee.each {
URI uri = UriBuilder.fromUri(it[0]).queryParam(key, value).build();
assert it[1] == uri.toString();
}
println "JavaEE passed."
// verify spring
spring.each {
URI uri = new URIBuilder(it[0]).addParameter(key, value).build();
assert it[1] == uri.toString();
}
println "Spring passed."
// verify apache
apache.each {
URI uri = UriComponentsBuilder.fromUriString(it[0]).queryParam(key, value).build().toUri();
assert it[1] == uri.toString();
}
println "Apache passed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment