0

I am trying to define a webservice, using Scalatra, where the parameters are passed in in the body, preferably as JSON, not having everything on the url, as I have it now.

So, I would like this test to pass, but the commented out code is what passes currently. The non-commented code isn't JSON, but I also am not certain how I would pass JSON for testing, as put requires an Iterable in the second parameter.

class WebAppSpec extends MutableScalatraSpec {
  addServlet(classOf[WebApp], "/*")
  "PUT /phaseupdate" should {
    "return status 200" in {
      //put("/phaseupdate/test1/address1/starting/10") {
      put("/phaseupdate", Map("filename" -> "test1", "entryaddress" -> "address1","name" -> "starting","percentcomplete" -> "10")) {
        status must_== 200
      }
    }
}

My current definition, which is wrong, is:

put("/phaseupdate/:filename/:entryaddress/:name/:percentcomplete") {
    val filename = params("filename")
    val entryaddress = params("entryaddress")
    val name = params("name")
    val percentcomplete = params("percentcomplete")

So how do I define my put service to just call it with PUT /phaseupdate and have the parameters in the body?

I am trying to limit what is going into the webserver access log, basically.

James Black
  • 41,583
  • 10
  • 86
  • 166

1 Answers1

1

The solution is to do this:

put("/phaseupdate") {
    val filename = if (params("filename").indexOf('.') > -1) params("filename").substring(0, params("filename").indexOf('.')) else params("filename")
    val entryaddress = params("entryaddress")
    val name = params("name")
    val percentcomplete = params("percentcomplete")

Basically, params() can read in what was passed.

The specs2 test is, and if this is followed by the get it returns the correct information.

  "PUT /phaseupdate" should {
    "return status 200" in {
      put("/phaseupdate", Map("filename" -> "test1", "entryaddress" -> "address1", "name" -> "starting", "percentcomplete" -> "10")) {
        status must_== 200
      }
    }
  }
James Black
  • 41,583
  • 10
  • 86
  • 166