1

I'm using Specs2 to test my Scalatra web service.

class APISpec extends ScalatraSpec {
  def is = "Simple test" ^
     "invalid key should return status 401" ! root401^

  addServlet(new APIServlet(),"/*")

  def root401 = get("/payments") {
    status must_== 401
  }
}

This tests the web service locally (localhost). Now I would like to perform the same tests to the production Jetty server. Ideally, I would be able to do this by only changing some URL. Is this possible at all ? Or do I have to write my own (possible duplicate) testing code for the production server?

mirandes
  • 957
  • 10
  • 10

1 Answers1

1

I don't know how Scalatra manages its URLs but one thing you can do in specs2 is control parameters from the command-line:

class APISpec extends ScalatraSpec with CommandLineArguments { def is = s2"""
  Simple test
  invalid key should return status 401  $root401
    ${addServlet(new APIServlet(),s"$baseUrl/*")}
  """

  def baseUrl = {
    // assuming that you passed 'url www.production.com' on the command line
    val args = arguments.commandLine.split(" ")
    args.zip(args.drop(1)).find { case (name, value) if name == "url" => value }.
      getOrElse("localhost:8080")
  }

  def root401 = get(s"$baseUrl/payments") {
    status must_== 401
  }
}
Eric
  • 15,494
  • 38
  • 61
  • Thanks for your answer. Perhaps I didn't explain correctly, but I'd like to test the *remote* production jetty server, rather than the localhost one. All tests run on say http://localhost:8080. Now I would like them to run on http://myurl.com/api:8080. Do you know if this is possible at all? I don't want to have to copy paste the tests to a main class in order to be able to test my production environment. – mirandes Oct 15 '13 at 16:34
  • In my answer I'm suggesting to inject the url on the command line: `sbt>testOnly *APISpec* -- url myurl/api:8080`. Wouldn't that work? – Eric Oct 16 '13 at 01:20
  • Why do we need a command line at all? If we are only using it to populate the $baseUrl variable, we can just say baseUrl=myurl/api:8080, correct? The problem is that the urls in *get* and *addServlet* seem to be relative to the localhost. I can't point them to a remote url. Do you have any ideas for this? Thanks – mirandes Oct 16 '13 at 10:44
  • That's where we need a Scalatra expert who I'm not :-( – Eric Oct 16 '13 at 13:09