This is very thin and lightweight HTTP server in Groovy for integration and functional tests. It is based on JDK HttpServer, with little Groovy DSL flavouring.
To start server on port 9999 that returns "qwerty" on GET requests to "/test", just run
Lightweightest.start(port:9999) {
get("/test") {request, response ->
"qwerty"
}
}
This is it.
For testing, you usually know exact execution scenario, so, for example, to serve just one request and destroy server automatically, run
Lightweightest.start(port:9999, stopAfter:1) {
get("/test") {request, response ->
"qwerty"
}
}
To stop server explicitly, run
def server = Lightweightest.start(port:9999) {
get("/test") {request, response ->
"qwerty"
}
}
server.stop()
It is also possible to update handlers dynamically, like
def server = Lightweightest.start(port:9999) {
get("/test") {request, response ->
"qwerty"
}
}
...
server.get("/test") {request, response ->
"asdfg"
}
server.stop()
Complete example of running server from Groovy script:
@GrabResolver(name="lightweightest", m2Compatible='true', root='https://raw.github.com/dmitart/lightweightest/master/repository')
@Grab("org.lightweightest:lightweightest:0.1" )
import org.lightweightest.Lightweightest
Lightweightest.start(port:9999, stopAfter:1) {
get("/card/list") {request, response ->
"qwerty"
}
}
println "http://localhost:9999/card/list".toURL().text