~ handleBad Request on POST
authorAlexandre Bertails <bertails@gmail.com>
Sun, 28 Aug 2011 23:58:13 -0400
changeset 20 e9f01d09f6ad
parent 19 0dbf1797f1b8
child 21 6cce50b70e79
~ handleBad Request on POST
src/main/scala/Main.scala
src/main/scala/Post.scala
src/test/scala/Test.scala
src/test/scala/utiltest.scala
--- a/src/main/scala/Main.scala	Sun Aug 28 23:14:33 2011 -0400
+++ b/src/main/scala/Main.scala	Sun Aug 28 23:58:13 2011 -0400
@@ -43,6 +43,7 @@
         case POST(_) => {
           /* http://openjena.org/ARQ/javadoc/com/hp/hpl/jena/update/UpdateFactory.html */
           Post.parse(Body.stream(req), baseURI) match {
+            case PostUnknown => BadRequest ~> ResponseString("You MUST provide valid content for either: SPARQL UPDATE, SPARQL Query, RDF/XML, TURTLE")
             case PostUpdate(update) => {
               val model = r.get()
               UpdateAction.execute(update, model)
--- a/src/main/scala/Post.scala	Sun Aug 28 23:14:33 2011 -0400
+++ b/src/main/scala/Post.scala	Sun Aug 28 23:58:13 2011 -0400
@@ -16,6 +16,7 @@
 case class PostUpdate(update:UpdateRequest) extends Post
 case class PostRDF(model:Model) extends Post
 case class PostQuery(query:Query) extends Post
+case object PostUnknown extends Post
 
 object Post {
   
@@ -28,19 +29,23 @@
   def parse(s:String, baseURI:String):Post = {
     val reader = new StringReader(s)
     try {
-      val update:UpdateRequest = UpdateFactory.create(s, baseURI)
-      PostUpdate(update)      
+      try {
+        val update:UpdateRequest = UpdateFactory.create(s, baseURI)
+        PostUpdate(update)      
+      } catch {
+        case qpe:QueryParseException =>
+          try {
+            val model = modelFromString(s, baseURI)
+            PostRDF(model)
+          } catch {
+            case je:JenaException => {
+              val query = QueryFactory.create(s)
+              PostQuery(query)
+            }
+          }
+      }
     } catch {
-      case qpe:QueryParseException =>
-        try {
-          val model = modelFromString(s, baseURI)
-          PostRDF(model)
-        } catch {
-          case je:JenaException => {
-            val query = QueryFactory.create(s)
-            PostQuery(query)
-          }
-        }
+      case _ => PostUnknown
     }
   }
   
--- a/src/test/scala/Test.scala	Sun Aug 28 23:14:33 2011 -0400
+++ b/src/test/scala/Test.scala	Sun Aug 28 23:58:13 2011 -0400
@@ -170,5 +170,12 @@
       model must beIsomorphicWith (expectedFinalModel)
     }
   }
+  
+  """POSTing something that does not make sense to Joe's URI""" should {
+    "return a 400 Bad Request" in {
+      val statusCode = Http.when(_ == 400)(joe.post("that's bouleshit") get_statusCode)
+      statusCode must_== 400
+    }
+  }
     
 }
--- a/src/test/scala/utiltest.scala	Sun Aug 28 23:14:33 2011 -0400
+++ b/src/test/scala/utiltest.scala	Sun Aug 28 23:58:13 2011 -0400
@@ -45,7 +45,7 @@
       
     def put(body:String):Request = req <<< body
       
-    def get_statusCode:Handler[Int] = new Handler(req, (c, r, e) => c, null)
+    def get_statusCode:Handler[Int] = new Handler(req, (c, r, e) => c, { case t => () })
     
     def get_header(header:String):Handler[String] = req >:> { _(header).head }