I’m running some unit tests on Android, and I have to run an http server locally to get images from it during my tests. Until now, I’m using a python script that run the SimpleHttpServer of python, then call Gradle, then kill the server at the end.
I’ve found that there is a SimpleHttpFileServer that could be used in Gradle.
But I can’t make it work. I found absolutely nothing about usage of this
class, except the doc here :
that doesn’t say much. What is a Stoppable for instance? No doc about it.
When I try to use it, I have this error :
Could not find matching constructor for:
org.gradle.plugins.javascript.envjs.http.simple.SimpleHttpFileServer
Anyone here has played with that? Or do you see any other way to do it? Another idea was to run the python server from Gradle, then kill it at the end, probably using task.finalizedBy, but I didn’t success either.
Any help on any of those methods would be appreciated.
You’ll want to use the SimpleHttpFileServerFactory to create instances of one of these. And yes, using finalizedBy to kill the server after running your tests is one way of doing things. Alternatively you could do the server startup/shutdown in your test code.
Thanks, I’ll try that.
For your last remark, maybe I didn’t explained well, but my test code runs on an Android device, the HTTPserver is launched on a Linux or Windows desktop, and Android device communicate with that HTTP server, so I don’t think I can do what you describe.
Hmm, using it this way is better, I still don’t get the error message I had previously though :
SimpleHttpFileServerFactory factory = new SimpleHttpFileServerFactory()
HttpFileServer server = factory.start(root, port)
So now server is started, but I can browse anything, it doesn’t answer, it just binds the port I’m giving him.
Is there a real example of using HttpFileServer that is really working?
It’s just doing autoboxing. The error is a result of the fact that start() isn’t a static method so you need to create an instance of SimpleHttpFileServerFactory to call it.
I’m not sure I understand. Is the server not answering to requests? Given a file called ‘foo.jpg’ in the root dir, you should be able to browse to http://localhost:8001/foo.jpg.
getResponseCode throw a EOFException when I try to connect to the http server launched by Gradle. I have no problem with http server from python or Apache. Is there a known problem with Gradle http server?
I read the discussion and am trying to implement a similar functionality, where Gradle starts a file server on the localhost before running a task (dependsOn) and then stops the server after the task finishes (finalizedBy). The problem I am facing is that calling stop on the HttpFileServer does not stop the port from listening. Because of which I get “address already in use” error when I run the task the second time. Below is the code I am using to implement the functionality.
def httpFileServer = null
task startHttpFileServer {
doFirst{
println 'Starting http file server'
File root = new File("${buildDir}/stage")
int port = 8530
def factory = new SimpleHttpFileServerFactory()
httpFileServer = factory.start(root, port)
println "HTTP file Server started in directory " + httpFileServer.getContentRoot()
}
}
task stopHttpFileServer {
doLast{
if (httpFileServer != null) {
println 'Stopping Server..'
httpFileServer.stop()
}
}
}
I am using Gradle 3.1 on SLES 12. Is this the expected behavior?