fix dir listing content-neg + tests webid
authorHenry Story <henry.story@bblfish.net>
Wed, 11 Apr 2012 12:00:41 +0200
branchwebid
changeset 196 3053b87b4ff2
parent 195 8ab372620ee2
child 197 c6520ef80d5c
fix dir listing content-neg + tests
src/main/scala/ReadWriteWeb.scala
src/main/scala/RequestLang.scala
src/test/scala/CreateContentSpecs.scala
src/test/scala/CreateDirectorySpec.scala
src/test/scala/OtherSpecs.scala
src/test/scala/util/utiltest.scala
--- a/src/main/scala/ReadWriteWeb.scala	Wed Apr 11 10:30:35 2012 +0200
+++ b/src/main/scala/ReadWriteWeb.scala	Wed Apr 11 12:00:41 2012 +0200
@@ -85,13 +85,15 @@
               val body = source.getLines.mkString("\n")
               Ok ~> ViaSPARQL ~> ContentType("text/html") ~> ResponseString(body)
             }
-            case GET(_) | HEAD(_) =>
+            case  GET(_) | HEAD(_) =>
               for {
                 model <- r.get() failMap { x => NotFound }
-                lang = representation match {
-                  case RDFRepr(l) => l
-                  case _ => Lang.default
-                }
+                lang =  AcceptLang.unapply(req).getOrElse{
+                    representation match {
+                      case RDFRepr(l) => l
+                      case _ => Lang.default
+                    }
+                  }
               } yield {
                 val res = req match {
                   case GET(_) => Ok ~> ViaSPARQL ~> ContentType(lang.contentType) ~> ResponseModel(model, uri, lang)
@@ -155,7 +157,7 @@
                 }
                 case PostQuery(query) => {
                   logger.info("SPARQL Query:\n" + query.toString())
-                  lazy val lang = RequestLang(req) getOrElse Lang.default
+                  lazy val lang = RequestLang.unapply(req) getOrElse Lang.default
                   for {
                     model <- r.get() failMap { t => NotFound }
                   } yield {
--- a/src/main/scala/RequestLang.scala	Wed Apr 11 10:30:35 2012 +0200
+++ b/src/main/scala/RequestLang.scala	Wed Apr 11 12:00:41 2012 +0200
@@ -26,14 +26,27 @@
 import unfiltered.request._
 
 object RequestLang {
-  
+
+  //is this type of use of apply, where it returns a type different from the class it seems
+  //to be constructing not confusing?
   def apply(req: HttpRequest[_]): Option[Lang] =
     Lang(RequestContentType(req))
 
   def unapply(req: HttpRequest[_]): Option[Lang] =
-    apply(req)
+    Lang(RequestContentType(req))
 
   def unapply(ct: String): Option[Lang] =
     Lang(ct)
     
 }
+
+object AcceptLang {
+
+
+  def unapply(req: HttpRequest[_]): Option[Lang] =
+    Lang(Accept(req))
+
+  def unapply(ct: String): Option[Lang] =
+    Lang(ct)
+
+}
\ No newline at end of file
--- a/src/test/scala/CreateContentSpecs.scala	Wed Apr 11 10:30:35 2012 +0200
+++ b/src/test/scala/CreateContentSpecs.scala	Wed Apr 11 12:00:41 2012 +0200
@@ -8,6 +8,7 @@
 import java.io.File
 import com.hp.hpl.jena.vocabulary.RDF
 import com.hp.hpl.jena.sparql.vocabulary.FOAF
+import com.hp.hpl.jena.rdf.model.Model
 
 object PutRDFXMLSpec extends SomePeopleDirectory {
 
@@ -98,7 +99,20 @@
       clazz must_== FOAF.Person
     }
 
+    "the directory should say that it contains that resource" in {
+      val (statusCode, model): Pair[Int,Model] = Http(dirUri >+ {
+        req => (req.get_statusCode,
+          req as_model(uriBase, RDFXML))
+      } )
+      val sioc = "http://rdfs.org/sioc/ns#"
+      statusCode must_== 200
+      val newRes = model.createResource(createdDocURL.toURI.toString)
+      model.contains(model.createResource(uri.to_uri.toString),model.createProperty(sioc+"container_of"),newRes) mustBe true
+      model.contains(newRes,RDF.`type`,model.createResource(sioc+"Item")) mustBe true
+    }
   }
+
+
   
 }
 
--- a/src/test/scala/CreateDirectorySpec.scala	Wed Apr 11 10:30:35 2012 +0200
+++ b/src/test/scala/CreateDirectorySpec.scala	Wed Apr 11 12:00:41 2012 +0200
@@ -4,6 +4,8 @@
 import org.w3.readwriteweb.utiltest._
 
 import dispatch._
+import com.hp.hpl.jena.rdf.model.Model
+import com.hp.hpl.jena.vocabulary.RDF
 
 object CreateDirSpec extends FilesystemBased with SomeRDF with SomeURI {
 
@@ -15,6 +17,16 @@
     "create a directory on disk" in {
       directory must be directory
     }
+    "return a directory listing" in {
+      val (statusCode, model): Pair[Int,Model] = Http(dirUri >+ {
+        req => (req.get_statusCode,
+          req as_model(uriBase, RDFXML))
+      } )
+      val sioc = "http://rdfs.org/sioc/ns#"
+      statusCode must_== 200
+      model.contains(model.createResource(uri.to_uri.toString),RDF.`type`,model.createResource(sioc+"Container")) mustBe true
+      model.listStatements(null,RDF.`type`,model.createResource(sioc+"Item")).hasNext mustBe false
+    }
   }
 
 }
--- a/src/test/scala/OtherSpecs.scala	Wed Apr 11 10:30:35 2012 +0200
+++ b/src/test/scala/OtherSpecs.scala	Wed Apr 11 12:00:41 2012 +0200
@@ -9,7 +9,7 @@
 
   """POSTing something that does not make sense to Joe's URI""" should {
     "return a 400 Bad Request" in {
-      val statusCode = Http.when(_ == 400)(uri.post("that's bouleshit", RDFXML) get_statusCode)
+      val statusCode = Http.when(_ == 400)(uri.post("that's bullshit", RDFXML) get_statusCode)  //bulls and bears on the stock exchange
       statusCode must_== 400
     }
   }
--- a/src/test/scala/util/utiltest.scala	Wed Apr 11 10:30:35 2012 +0200
+++ b/src/test/scala/util/utiltest.scala	Wed Apr 11 12:00:41 2012 +0200
@@ -62,6 +62,8 @@
 
     def get: Request = req.copy(method="GET")
 
+    def get(lang: Lang): Request = req.copy(method="GET") <:< Map("Accept"->lang.contentType)
+
     def delete: Request = req.copy(method="DELETE")
     
     def >++ [A, B, C] (block:  Request => (Handler[A], Handler[B], Handler[C])) = {