Skip to content

Commit

Permalink
Added expires to order list
Browse files Browse the repository at this point in the history
  • Loading branch information
thoraage committed Mar 25, 2011
1 parent 377fe75 commit f5f3991
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 10 deletions.
1 change: 1 addition & 0 deletions project/build/Project.scala
Expand Up @@ -8,6 +8,7 @@ class Project(info: ProjectInfo) extends DefaultWebProject(info) {
val dispatchHttp = dispatch("http")
val specs = "org.scala-tools.testing" %% "specs" % "1.6.7" % "test" withSources
val httpClient = "org.apache.httpcomponents" % "httpclient" % "4.0.1" withSources
val jodaTime = "joda-time" % "joda-time" % "1.6.2" withSources

val jettyTest = "org.mortbay.jetty" % "jetty" % "6.1.22" % "test" withSources
override def jettyPort = 8086
Expand Down
14 changes: 8 additions & 6 deletions src/main/scala/no/arktekk/coffeetrader/OrderResource.scala
Expand Up @@ -2,14 +2,16 @@ package no.arktekk.coffeetrader

import Function.tupled
import java.util.Random
import net.liftweb.common.{Box, Full}
import net.liftweb.http.rest.RestHelper
import net.liftweb.http._
import net.liftweb.common.{Box, Full, Empty}
import net.liftweb.util.TimeHelpers._
import org.joda.time.DateTime
import org.joda.time.format.ISODateTimeFormat
import util.MatchLong
import util.Resource._
import util.ResourceHelpers._
import util.TimeHelpers._
import xml.{NodeSeq, Elem}
import org.apache.http.HttpStatus

object OrderResource extends RestHelper with RestExtensions {
val random = new Random
Expand All @@ -21,8 +23,6 @@ object OrderResource extends RestHelper with RestExtensions {
def findAndDo(orderId: Long, f: Elem => LiftResponse): Box[LiftResponse] =
Box(Orders(orderId).map(entity => f(entity.elem)).orElse(Some(NotFoundResponse())))

lazy val isoTimeFormat = ISODateTimeFormat.dateTimeNoMillis

def orderLocation(req: Req, id: Long) = pathify(req, req.path.partPath :+ id.toString: _*)

serve {
Expand Down Expand Up @@ -90,7 +90,9 @@ object OrderResource extends RestHelper with RestExtensions {
</entry>
}}
</feed>
Full(XmlResponse(response, atomMediaType))
Full(new XmlResponse(response, HttpStatus.SC_OK, atomMediaType, Nil) {
override def headers = ("Expires" -> expiresTimeFormatter.format((new DateTime).plus(1 minute).toDate)) :: super.headers.filter(_._1 != "Expires")
})
}
}
serve {
Expand Down
Expand Up @@ -3,7 +3,7 @@ package no.arktekk.coffeetrader
import net.liftweb.http.rest.RestHelper
import net.liftweb.common.Box
import util.MatchLong
import util.Resource._
import util.ResourceHelpers._
import xml.Elem
import net.liftweb.http.{CreatedResponse, XmlResponse, NotFoundResponse, LiftResponse}

Expand Down
Expand Up @@ -2,7 +2,7 @@ package no.arktekk.coffeetrader.util

import net.liftweb.http.Req

object Resource {
object ResourceHelpers {
val xmlMediaType = "application/xml"
val atomMediaType = "application/atom+xml"

Expand Down
12 changes: 12 additions & 0 deletions src/main/scala/no/arktekk/coffeetrader/util/TimeHelpers.scala
@@ -0,0 +1,12 @@
package no.arktekk.coffeetrader.util

import org.joda.time.format.{DateTimeFormat, ISODateTimeFormat}
import java.text.SimpleDateFormat

object TimeHelpers {

lazy val isoTimeFormat = ISODateTimeFormat.dateTimeNoMillis

def expiresTimeFormatter = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z")

}
14 changes: 12 additions & 2 deletions src/test/scala/no/arktekk/coffeetrader/BaristaSpec.scala
Expand Up @@ -4,6 +4,9 @@ import dispatch.{StatusCode, Http}
import Http._
import org.apache.http.HttpStatus
import org.specs.Specification
import org.specs.util.TimeConversions._
import util.TimeHelpers._
import org.joda.time.{DateTime, Duration}

object BaristaSpec extends Specification with DispatchUtil {

Expand All @@ -12,10 +15,18 @@ object BaristaSpec extends Specification with DispatchUtil {
postOrder
postOrder

val entries = Http(ordersUrl <:< atomTypes <> pass)
val (entries, headers) = Http(ordersUrl <:< atomTypes >+ (h => (h <> pass, h >:> pass)))
"contain orders" in {
(entries \\ "entry").size must_== 2
}

"should have expiring time" in {
headers.get("Expires").flatMap(_.headOption).map {
expires =>
val duration = new Duration(new DateTime, new DateTime(expiresTimeFormatter.parse(expires)))
duration.getMillis must beCloseTo(1.minute.at, 5.seconds.at)
}.getOrElse(error("Missing expires header"))
}
}

"listing drinks with wrong media type" should {
Expand All @@ -26,7 +37,6 @@ object BaristaSpec extends Specification with DispatchUtil {
}
}

// TODO: Add Expires
"retrieve drink entry" should {
val entries = Http(ordersUrl <:< atomTypes <> pass)
val ids = (entries \\ "entry" \\ "id").map(_.text)
Expand Down

0 comments on commit f5f3991

Please sign in to comment.