public
Created

lift:WebSpec2 + Unit test

  • Download Gist
WebSpec2.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
package code.mockweb
/*
* Copyright 2011 WorldWide Conferencing, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
 
import javax.servlet.http.HttpServletRequest
 
import scala.xml.NodeSeq
 
import org.specs2.mutable._
 
import net.liftweb.common
import net.liftweb.common.Empty
import common.{Box,Empty,Full}
import net.liftweb.http
import http._
import net.liftweb.json
import json.JsonAST._
import net.liftweb.mocks
import mocks.MockHttpServletRequest
import net.liftweb.http.LiftRules
import net.liftweb.http.LiftRulesMocker
import net.liftweb.common.Box
import net.liftweb.http.LiftSession
import net.liftweb.json.JsonAST.JValue
import net.liftweb.mocks.MockHttpServletRequest
import net.liftweb.mockweb.MockWeb
import net.liftweb.http.Req
import net.liftweb.http.S
import net.liftweb.common.Full
import org.specs2.execute.Result
 
 
/**
* This trait provides Lift-specific extensions to the Specification
* base trait to simplify unit testing of your code. In addition to
* the Scaladoc, Please see the
* source to WebSpecSpec.scala for an example of how to use this. This class
* allows you to optionally configure LiftRules for your spec. LiftRules rules
* are always used, so if you don't want that to happen you can just use MockWeb directly
* in your own custom spec and simply omit any LiftRules setup.
*
* @param boot defines a method that is called prior to
* testing to set up the Lift environment. This is where you'll
* initialize LiftRules, Mapper, etc. The simplest approach
* is to just point this at your Boostrap.boot method.
*/
abstract class WebSpec2(boot : () => Any = () => {}) extends Specification {
/**
* This is our private spec instance of Liftrules. Everything we run will
* use this instance instead of the global instance
*/
private val liftRules = new LiftRules()
 
LiftRulesMocker.devTestLiftRulesInstance.doWith(liftRules) {
boot()
}
 
/**
* A class that bridges between the description string
* and classes that provide the Lift-specific wrapping
* of the Expectation code. The method names here
* should be self-explanatory. For methods that take a
* Box[LiftSession], an Empty Box will result in the
* creation of a new Session.
*/
class WebSpecBridge (description : String) {
def withSFor (url : String, session : Box[LiftSession] = Empty, contextPath : String = "") =
new SessionSpecification(description, url, session, contextPath)
 
def withSFor (url : String, session : LiftSession) : SessionSpecification =
new SessionSpecification(description, url, Box.!!(session), "")
 
def withSFor (req : HttpServletRequest) =
new SessionSpecification(description, req, Empty)
 
def withSFor (req : HttpServletRequest, session : Box[LiftSession]) =
new SessionSpecification(description, req, session)
 
def withSFor (req : HttpServletRequest, session : LiftSession) =
new SessionSpecification(description, req, Box.!!(session))
 
def withReqFor (url : String, contextPath : String = "") =
new ReqSpecification(description, url, contextPath)
 
def withReqFor (req : HttpServletRequest) =
new ReqSpecification(description, req)
 
def withTemplateFor (url : String, session : Box[LiftSession] = Empty, contextPath : String = "") =
new TemplateSpecification(description, url, session, contextPath)
 
def withTemplateFor (url : String, session : LiftSession) =
new TemplateSpecification(description, url, Box.!!(session), "")
 
def withTemplateFor (req : HttpServletRequest) =
new TemplateSpecification(description, req, Empty)
 
def withTemplateFor (req : HttpServletRequest, session : Box[LiftSession]) =
new TemplateSpecification(description, req, session)
 
def withTemplateFor (req : HttpServletRequest, session : LiftSession) =
new TemplateSpecification(description, req, Box.!!(session))
}
 
/**
* Converts a String description into a WebSpecBridge that can
* then be used to set up either an S or Req instance.
*/
implicit def strToWebSpecBridge (description : String) =
new WebSpecBridge(description)
 
/**
* A comon trait to provide utility methods for mutating the
* underlying HttpServletRequest.
*/
trait ModifiableRequest [T <: ModifiableRequest[T]] {
// Make sure that our return values are for the supertype, not ModifiableRequest
self : T =>
 
val req : HttpServletRequest
 
/**
* Modifies the request to POST the given request body text. Optionally,
* you can set the content type (defaults to "text/plain")
*/
def withPost (text : String, contentType : String = "text/plain") : T =
withMods { mockReq =>
mockReq.body = text
mockReq.contentType = contentType
mockReq.method = "POST"
}
 
/**
* Modifies the request to POST the given request body JSON.
*/
def withPost (jval : JValue) = withMods { mockReq =>
mockReq.body = jval
mockReq.method = "POST"
}
 
/**
* Modifies the request to POST the given request body XML.
*/
def withPost (node : NodeSeq) = withMods { mockReq =>
mockReq.body = node
mockReq.method = "POST"
}
 
/**
* Modifies the request to PUT the given request body text. Optionally,
* you can set the content type (defaults to "text/plain")
*/
def withPut (text : String, contentType : String = "text/plain") =
withMods { mockReq =>
mockReq.body = text
mockReq.contentType = contentType
mockReq.method = "PUT"
}
 
/**
* Modifies the request to PUT the given request body JSON.
*/
def withPut (jval : JValue) = withMods { mockReq =>
mockReq.body = jval
mockReq.method = "PUT"
}
 
/**
* Modifies the request to PUT the given request body XML.
*/
def withPut (node : NodeSeq) = withMods { mockReq =>
mockReq.body = node
mockReq.method = "PUT"
}
 
/**
* Allows you to specify your own modification function for the servlet request
* prior to initialization.
*/
def withMods (f : MockHttpServletRequest => Unit) : T = req match {
case r : MockHttpServletRequest => f(r); this
case _ => throw new IllegalArgumentException("We can only mutate MockHttpServletRequest instances")
}
}
 
/**
* This class provides a wrapper to test methods that require an
* initialized S.
*/
class SessionSpecification (description : String,
val req : HttpServletRequest,
session : Box[LiftSession]) extends ModifiableRequest[SessionSpecification] {
def this (description : String, url : String, session : Box[LiftSession], contextPath : String) =
this(description, new MockHttpServletRequest(url, contextPath), session)
 
def in [T <% Result](expectations : => T) = {
// val example = exampleContainer.createExample(description)
// if (sequential) example.setSequential()
 
 
exampleFactory.newExample (description, {
LiftRulesMocker.devTestLiftRulesInstance.doWith(liftRules) {
MockWeb.useLiftRules.doWith(true) {
MockWeb.testS(req, session) {
expectations
}
}
}
})
}
}
 
/**
* This class provides a wrapper to test methods that require an
* initialized Req.
*/
class ReqSpecification (description : String,
val req : HttpServletRequest) extends ModifiableRequest[ReqSpecification] {
def this (description : String, url : String, contextPath : String) =
this(description, new MockHttpServletRequest(url, contextPath))
 
def in [T <% Result](expectations : Req => T) = {
 
// val example = exampleFactory.newExample (description, expectations)// exampleContainer.createExample(description)
// if (sequential) example.setSequential()
 
exampleFactory.newExample (description, {
LiftRulesMocker.devTestLiftRulesInstance.doWith(liftRules) {
MockWeb.useLiftRules.doWith(true) {
MockWeb.testReq(req)(expectations)
}
}
})
}
}
 
/**
* This class provides a wrapper to test methods that require
* a processed template.
*/
class TemplateSpecification (description : String,
val req : HttpServletRequest,
session : Box[LiftSession]) extends ModifiableRequest[TemplateSpecification] {
def this (description : String, url : String, session : Box[LiftSession], contextPath : String) =
this(description, new MockHttpServletRequest(url, contextPath), session)
 
def in [T <% Result](expectations : Box[NodeSeq] => T) = {
// val example = exampleContainer.createExample(description)
// if (sequential) example.setSequential()
 
exampleFactory.newExample (description, {
LiftRulesMocker.devTestLiftRulesInstance.doWith(liftRules) {
MockWeb.useLiftRules.doWith(true) {
MockWeb.testS(req, session) {
S.request match {
case Full(sReq) => expectations(S.runTemplate(sReq.path.partPath))
case other => failure("Error: withTemplateFor call did not result in " +
"request initialization (S.request = " + other + ")")
}
}
}
}
})
}
}
}
WebSpec2Spec.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
package code.mockweb
/*
* Copyright 2011 WorldWide Conferencing, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import net.liftweb._
 
import mockweb.MockWeb
import scala.xml.NodeSeq
 
import common.{Box,Empty,Full}
import http._
import http.rest._
import json._
import json.JsonDSL._
import mocks.MockHttpServletRequest
 
/**
* This only exists to keep the WebSpecSpec clean. Normally,
* you could just use "() => bootstrap.Boot.boot".
*/
object WebSpecSpecBoot {
def boot() {
println("WebSpecSpec Booting up")
 
// Add this so that withTemplateFor test works
LiftRules.addToPackages("net.liftweb.mockweb")
 
 
LiftRules.statelessRewrite.append {
case RewriteRequest(ParsePath(List("test", "stateless"), _, _, _), _, _) => {
RewriteResponse(List("stateless", "works"))
}
}
 
LiftRules.statefulRewrite.append {
case RewriteRequest(ParsePath(List("test", "stateful"), _, _, _), _, _) => {
RewriteResponse(List("stateful", "works"))
}
}
 
println("WebSpecSpec Boot complete")
}
}
 
/**
* A test RestHelper to show usage.
*/
object WebSpecSpecRest extends RestHelper {
serve {
case "api" :: "info" :: Nil JsonGet req => {
("version" -> "1.0") ~ ("name" -> "WebSpec")
}
}
}
 
/**
* This spec does double duty as both a spec against the
* WebSpec trait as well as an example of how to use it.
*/
class WebSpec2Spec extends WebSpec2(WebSpecSpecBoot.boot _) {
"WebSpec" should {
args(sequential=true) // This is important for using SessionVars, etc.
 
val testUrl = "http://foo.com/test/stateless"
 
val testReq =
new MockHttpServletRequest("http://foo.com/test/this?foo=bar", "/test")
 
// Create a new session for use in the tests
val testSession = MockWeb.testS(testUrl) {
S.session
}
 
object TestVar extends SessionVar[String]("Empty")
 
"properly set up S with a String url" withSFor(testUrl) in {
S.request match {
case Full(req) => req.path.partPath must_== List("stateless", "works")
case _ => failure("No request in S")
}
}
 
"properly set up S with a String url and session" withSFor(testUrl, testSession) in {
TestVar("foo!")
TestVar.is must_== "foo!"
}
 
 
"properly re-use a provided session" withSFor(testUrl, testSession) in {
TestVar.is must_== "foo!"
}
 
"properly set up S with a HttpServletRequest" withSFor(testReq) in {
S.uri must_== "/this"
S.param("foo") must_== Full("bar")
}
 
"properly set up a Req with a String url" withReqFor(testUrl) in {
_.path.partPath must_== List("stateless", "works")
}
 
"properly set up a Req with a String url and context path" withReqFor(testUrl, "/test") in {
_.path.partPath must_== List("stateless")
}
 
"properly set up a Req with a HttpServletRequest" withReqFor(testReq) in {
_.uri must_== "/this"
}
 
"properly set a plain text body" withReqFor(testUrl) withPost("This is a test") in {
req =>
req.contentType must_== Full("text/plain")
req.post_? must_== true
req.body match {
case Full(body) => (new String(body)) must_== "This is a test"
case _ => failure("No body set")
}
}
 
"properly set a JSON body" withReqFor(testUrl) withPut(("name" -> "Joe")) in {
req =>
req.json_? must_== true
req.put_? must_== true
req.json match {
case Full(jval) => jval must_== JObject(List(JField("name", JString("Joe"))))
case _ => failure("No body set")
}
}
 
"properly set an XML body" withSFor(testUrl) withPost(<test/>) in {
S.request match {
case Full(req) => {
req.xml_? must_== true
req.post_? must_== true
req.xml must_== Full(<test/>)
}
case _ => failure("No request found in S")
}
}
 
"properly mutate the request" withSFor(testUrl) withMods(_.contentType = "application/xml") in {
(S.request.map(_.xml_?) openOr false) must_== true
}
 
"process a JSON RestHelper Request" withReqFor("http://foo.com/api/info.json") in { req =>
(WebSpecSpecRest(req)() match {
case Full(JsonResponse(_, _, _, 200)) => true
case other => failure("Invalid response : " + other); false
}) must_== true
}
 
// "properly process a template" withTemplateFor("http://foo.com/net/liftweb/mockweb/webspecspectemplate") in ({
// case Full(template) => template.toString.contains("Hello, WebSpec!") must_== true
// case other => {
// failure("Error on template : " + other)
// }
// } : PartialFunction[Box[NodeSeq],Unit])
}
}

Work with

 <dependency>
            <groupId>org.specs2</groupId>
            <artifactId>specs2_2.9.1</artifactId>
            <version>1.8.2</version>
            <scope>test</scope>
 </dependency>

or
val spec2Version = "1.8.2"
val specs2 = "org.specs2" %% "specs2" % spec2Version %"test->default" // For specs2.org tests

Intellij 11.1 min.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.