Announcement Announcement Module
Collapse
No announcement yet.
question about querystring and MockHttpServletRequest Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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

    Comment


    • #3
      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

      Comment


      • #4
        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...

        Comment

        Working...
        X