Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7993307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:48:54+00:00 2026-06-04T13:48:54+00:00

I am making a simple snippet that should pass a Box[String] with the requests

  • 0

I am making a simple snippet that should pass a Box[String] with the requests user-agent to a helper class that passes back the css classes that should be added to the html element. I am doing this since it seems tricky to get Lift to supply a html respons with conditional comments like those in html5boilerplate. This is what I have now and it works:

class LiftBoilerplate {

   def render = "html [class+]" #> getClassForUserAgent(S.request)

   private def getClassForUserAgent(request:Box[Req]) = request match {
       case Full(r) => LiftBoilerplateHelper.getHtmlClass(r.userAgent)
       case _ => ""
   }
}

My problem is that I’d like to write a unit test for this like:

object LiftBoilerplateSpecs extends Specification {

  val session = new LiftSession("", randomString(20), Empty)

  "LiftBoilerplate" should {
    "add 'no-js' to the class of an html tag element" in {

      val snippet = new LiftBoilerplate
      val result = snippet.render(<html><head></head><body>test</body></html>)

      result must ==/(<html class="no-js"><head></head><body>test</body></html>)
    }
  }
}

This test fails since S.request is Empty. What should I do to supply the snippet with a mocked request with a userAgent in it?

So far I have looked at http://www.assembla.com/spaces/liftweb/wiki/Unit_Testing_Snippets_With_A_Logged_In_User
and
http://www.assembla.com/spaces/liftweb/wiki/Mocking_HTTP_Requests
but I do not understand how to achive my goal.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T13:48:55+00:00Added an answer on June 4, 2026 at 1:48 pm

    To make the request and apply it automatically in each test example you will need to use the Trait AroundExample to wrap each test in a S.init call:

    object LiftBoilerplateSpecs extends Specification with AroundExample {
    
      val session = new LiftSession("", randomString(20), Empty)
    
      def makeReq = {
        val mockRequest = new MockHttpServletRequest("http://localhost")
        mockRequest.headers = Map("User-Agent" -> List("Safari"))
    
        new Req(Req.NilPath, "", GetRequest, Empty, new HTTPRequestServlet(mockRequest, null),
          System.nanoTime, System.nanoTime, false,
          () => ParamCalcInfo(Nil, Map(), Nil, Empty), Map())
      }
    
      def around[T <% Result](t: => T) = S.init(makeReq, session)(t)
    
      "LiftBoilerplate" should {
        "add 'no-js' to the class of an html tag element" in {
    
          val snippet = new LiftBoilerplate
          val result = snippet.render(<html><head></head><body>test</body></html>)
    
          result must ==/(<html class="no-js"><head></head><body>test</body></html>)
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in making a simple cgi server for a course. To do that in some
I'm making a simple program in Objective-C. It has one class with a lot
I was making a simple class for a small project and decided to just
I am making a simple byte buffer that stores its data in a char
I am making simple ANSI C program, that simulates Unix shell. So I am
I am making simple application that will take hotel name and price of item,
This is a rather simple question that is making me a bit curious. Consider
New to rails and making a simple blog in rails 3.1.3 where a User
I've got a simple JavaScript statement that reads a string and fires off a
I am making simple connection between two table - first one called users has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.