alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Lift Framework example source code file (MockHttpRequestSpec.scala)

This example Lift Framework source code file (MockHttpRequestSpec.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Lift Framework tags/keywords

if_modified_header, illegalargumentexception, illegalargumentexception, json, list, mockhttprequest, mockhttprequest, mockhttpservletrequest, mockhttpservletrequest, specification, string, test_url, test_url, xml

The Lift Framework MockHttpRequestSpec.scala source code

/*
 * 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 mocks

import org.specs.Specification

import json.JsonDSL._


/**
 * System under specification for MockHttpRequest.
 */
object MockHttpRequestSpec extends Specification("MockHttpRequest Specification") {

  val IF_MODIFIED_HEADER = "If-Modified-Since"
  val TEST_URL = "https://foo.com/test/this/page?a=b&b=a&a=c"

  "MockHttpRequest" should {

    "properly deconstruct from a URL" in {
      val testRequest = new MockHttpServletRequest(TEST_URL, "/test")

      testRequest.getScheme must_== "https"
      testRequest.isSecure must_== true
      testRequest.getServerName must_== "foo.com"
      testRequest.getContextPath must_== "/test"
      testRequest.getRequestURI must_== "/test/this/page"
      testRequest.getRequestURL.toString must_== TEST_URL
      testRequest.getQueryString must_== "a=b&b=a&a=c"
      testRequest.getParameterValues("a").toList must_== List("b","c")
      testRequest.getParameter("b") must_== "a"
    }

    "correctly add and parse a date header" in {
      val testRequest = new MockHttpServletRequest(TEST_URL, "/test")

      val epoch = 241000 // (milliseconds not included in RFC 1123)

      testRequest.setDateHeader(IF_MODIFIED_HEADER, epoch)

      testRequest.getDateHeader(IF_MODIFIED_HEADER) must_== epoch
    }

    "throw an IllegalArgumentException for an invalid date header" in {
      val testRequest = new MockHttpServletRequest(TEST_URL, "/test")

      testRequest.headers += IF_MODIFIED_HEADER -> List("this is not a valid date")

      testRequest.getDateHeader(IF_MODIFIED_HEADER) must throwA[IllegalArgumentException]
    }

    "throw an IllegalArgumentException for an invalid context path" in {
      (new MockHttpServletRequest(TEST_URL, "foo")) must throwA[IllegalArgumentException]
      (new MockHttpServletRequest(TEST_URL, "/foo/")) must throwA[IllegalArgumentException]
     }

    "throw an IllegalArgumentException for an invalid query string" in {
      val testRequest = new MockHttpServletRequest(TEST_URL, "/test")
      
      (testRequest.queryString ="this=a&&that=b") must throwA[IllegalArgumentException]
      (testRequest.queryString = "foo") must throwA[IllegalArgumentException]
     }


    "properly set a default content type for JSON" in {
      val testRequest = new MockHttpServletRequest(TEST_URL, "/test")

      testRequest.body = ("name" -> "joe")

      testRequest.contentType must_== "application/json"
    }

    "properly set a user-specificed content type for JSON" in {
      val testRequest = new MockHttpServletRequest(TEST_URL, "/test")

      testRequest.body_=(("name" -> "joe"), "text/json")

      testRequest.contentType must_== "text/json"
    }

    "properly set a default content type for XML" in {
      val testRequest = new MockHttpServletRequest(TEST_URL, "/test")

      testRequest.body = <test/>

      testRequest.contentType must_== "text/xml"
    }

    "properly set a user-specificed content type for XML" in {
      val testRequest = new MockHttpServletRequest(TEST_URL, "/test")

      testRequest.body_=(<test/>, "application/xml")

      testRequest.contentType must_== "application/xml"  
    }

    "properly set a default content type for a String" in {
      val testRequest = new MockHttpServletRequest(TEST_URL, "/test")

      testRequest.body = "test"

      testRequest.contentType must_== "text/plain"
    }

    "properly set a user-specificed content type for a String" in {
      val testRequest = new MockHttpServletRequest(TEST_URL, "/test")

      testRequest.body_=("test", "text/csv")

      testRequest.contentType must_== "text/csv"
    }
    
  }
}

Other Lift Framework examples (source code examples)

Here is a short list of links related to this Lift Framework MockHttpRequestSpec.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.