Results 1 to 4 of 4

Thread: question about querystring and MockHttpServletRequest

  1. #1
    Join Date
    Dec 2004
    Posts
    4

    Default question about querystring and MockHttpServletRequest

    I'm trying to test a controller that receives a request for a certain page - the URL will contain a querystring that specifies a the ID for a user to view.... How do I accurately reflect such a page request in a test environment?

    I tried creating the request object using the line below:

    request = new MockHttpServletRequest("GET","http://127.0.0.1:8080/everest/viewUserPersonalInfo.htm?userId=2");

    but when I look at the variables for the request object in my debugger, it shows that the parameters table is empty, the queryString is null.... is there some way to have the incoming URL parsed properly into the queryString and/or parameters table??? (btw: I know that there are methods in the MockHttpServletRequest object for addParameter(...) and setQueryString(...) but that seems contrived to me....).

    Thanks,
    Scott

  2. #2
    Join Date
    Aug 2004
    Location
    Leuven, Belgium
    Posts
    37

    Default

    You're using the following constructor: public MockHttpServletRequest(String method, String requestURI)
    but a requestURI does not include the queryString
    (see http://java.sun.com/j2ee/1.4/docs/ap...etRequestURI())

    It doesn't answer your question and it's probably just a matter of taste, but I much prefer:

    Code:
    request = new MockHttpServletRequest();
    request.addParameter ("userId","2");
    Because it clearly documents the needs of your controller (eg. controller probably doesn't need to know the method, nor the serverPort).

    regards,
    Maarten

  3. #3
    Join Date
    Dec 2004
    Posts
    4

    Default

    Well.... you're certainly right about the URI thing - I missed that (obviously). I ended up doing the addParameter thing as well... you might be right about it documenting the expectations of the controller.

    Thanks.
    Scott

  4. #4
    Join Date
    Oct 2005
    Location
    Bergen, Norway
    Posts
    128

    Default Seems like an error

    The parameters given in request.setQueryString( "myparam=somevalue" );

    should be able to be returned as
    request.getParameter( "myparam" )

    but it's not...

Similar Threads

  1. Replies: 1
    Last Post: Sep 9th, 2005, 03:56 AM
  2. Replies: 8
    Last Post: Jun 26th, 2005, 08:51 PM
  3. Replies: 6
    Last Post: Jun 6th, 2005, 09:41 PM
  4. ModelAndView - adding to querystring
    By stephenro in forum Web
    Replies: 1
    Last Post: Apr 13th, 2005, 10:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •