Ratpacked: Easy URI Creation With HttpUrlBUillder
When we need to create a URI
object in Ratpack we can use the HttpUrlBuilder
class.
We use several methods to build up a complete URI
object in an easy way.
This is very useful when we for example use Ratpack’s HttpClient
object and we need to pass an URI
to do a request.
In the following example specification we see several usages of the HttpUrlBuilder
class:
package mrhaki.sample
import ratpack.http.HttpUrlBuilder
import spock.lang.Specification
import spock.lang.Unroll
class HttpUrlBuilderSpec extends Specification {
void 'create URI with http protocol'() {
expect:
HttpUrlBuilder.http()
.host('localhost')
.port(5050)
.build()
.toString() == 'http://localhost:5050'
}
@Unroll
void 'create URI with path'() {
given:
final URI server = 'http://server:8080/'.toURI()
expect:
HttpUrlBuilder.base(server)
.maybePath(path)
.build()
.toString() == result
where:
path | result
null | 'http://server:8080'
'' | 'http://server:8080'
'user' | 'http://server:8080/user'
}
void 'create URI with parameters'() {
expect:
HttpUrlBuilder.https()
.host("localhost")
.path('users')
.params(page: 2, max: 100)
.params('details', 'true')
.build()
.toString() == 'https://localhost/users?page=2&max=100&details=true'
}
}
Written with Ratpack 1.4.5.