Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 170 lines (144 sloc) 4.868 kb
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
/*
* 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.
*/
package net.liftweb
package mockweb

import scala.xml.{Null,Text,UnprefixedAttribute}

import org.specs.Specification

import common._
import util._
import http._
import provider.servlet.HTTPRequestServlet
import mocks.MockHttpServletRequest


/**
* System under specification for MockWeb. This does the double duty as both a spec
* against the MockWeb object as well as an example of how to use it.
*/
object MockWebSpec extends Specification("MockWeb Specification") {
  import MockWeb._

// TODO : Uncomment this code when LiftRules can be scoped
// // Global LiftRules setup
// 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"))
// }
// }
//
// LiftRules.early.append {
// req =>
// req match {
// case httpReq : HTTPRequestServlet => {
// httpReq.req match {
// case mocked : MockHttpServletRequest => {
// mocked.remoteAddr = "1.2.3.4"
// }
// case _ => println("Not a mocked request?")
// }
// }
// case _ => println("Not a servlet request?")
// }
// }

  "MockWeb" should {
    shareVariables() // Avoid setting up LiftRules multiple times

    "provide a Req corresponding to a string url" in {
      testReq("http://foo.com/test/this?a=b&a=c", "/test") {
        req =>
          req.uri must_== "/this"
          req.params("a") must_== List("b","c")
      }
    }

    "provide a Req corresponding to a HttpServletRequest" in {
      val mockReq =
        new MockHttpServletRequest("http://foo.com/test/this", "/test")

      mockReq.method = "POST"

      import json.JsonDSL._

      mockReq.body = ("name" -> "joe") ~ ("age" -> 35)

      testReq(mockReq) {
        req =>
          req.json_? must_== true
      }
    }

// TODO : Uncomment this code when LiftRules can be scoped
// "process LiftRules.early when configured" in {
// useLiftRules.doWith(true) {
// testReq("http://foo.com/test/this") {
// req => req.remoteAddr must_== "1.2.3.4"
// }
// }
// }
//
// "process LiftRules stateless rewrites when configured" in {
// useLiftRules.doWith(true) {
// testReq("http://foo.com/test/stateless") {
// req => req.path.partPath must_== List("stateless", "works")
// }
// }
// }

    "initialize S based on a string url" in {
      testS("http://foo.com/test/that?a=b&b=c") {
        S.param("b") must_== Full("c")
      }
    }

    "initialize S based on a HttpServletRequest" in {
      val mockReq =
        new MockHttpServletRequest("http://foo.com/test/this?foo=bar", "/test")
      
      testS(mockReq) {
        S.param("foo") must_== Full("bar")

        S.uri must_== "/this"
      }
    }

// TODO : Uncomment this code when LiftRules can be scoped
// "process S with stateless rewrites" in {
// useLiftRules.doWith(true) {
// testS("http://foo.com/test/stateless") {
// S.request.foreach(_.path.partPath must_== List("stateless", "works"))
// }
// }
// }
//
// "process S with stateful rewrites" in {
// useLiftRules.doWith(true) {
// testS("http://foo.com/test/stateful") {
// S.request.foreach(_.path.partPath must_== List("stateful", "works"))
// }
// }
// }

    "emulate a snippet invocation" in {
        testS("http://foo.com/test/stateful") {
          withSnippet("MyWidget.foo", new UnprefixedAttribute("bar", Text("bat"), Null)) {
            S.currentSnippet must_== Full("MyWidget.foo")
            S.attr("bar") must_== Full("bat")
          }
        }
    }


    "simplify shared sessions" in {
      object testVar extends SessionVar[String]("Empty")
   
      val session = testS("http://foo.com/test") {
        testVar("Foo!")
        S.session // returns the current session
      }

      // A second test
      testS("http://foo.com/test2", session) {
        testVar.is must_== "Foo!"
      }
    }

  }
}
Something went wrong with that request. Please try again.