Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Update to MockHttpServletRequest and its test #1516

Merged
merged 6 commits into from

4 participants

@dmclean62

Fix for problem where a parameter in the URL has a blank value (either "a=" or just "a").

dmclean62 added some commits
@dmclean62 dmclean62 Update MockHttpRequestSpec.scala
Added test for parameters with empty values
bc69309
@dmclean62 dmclean62 Update MockHttpServletRequest.scala
Add case matching a parameter with no/empty value ("a=" or just "a")
9b6938f
@fmpwizard
Owner

Looks good, we just need one small change, to add your name and email address to the https://github.com/lift/framework/blob/master/contributors.md file

@dmclean62
@fmpwizard
Owner

:+1:

@Shadowfiend
Owner

Hm. How do I run the MockHttpRequestSpec?

@dmclean62
@fmpwizard
Owner

this should do:

> test-only net.liftweb.mocks.MockHttpRequestSpec
@Shadowfiend
Owner

Derp. Heh. I was in the wrong project.

Anyway, the test isn't passing, and broke another one ;)

[error] x parse parameters with empty values
[error]    'https://foo.com/test/this/page?a=b&b=a&c=&d='
[error]     is not equal to
[error]    'https://foo.com/test/this/page?a=b&b=a&a=c' (MockHttpRequestSpec.scala:58)
[error] Expected: ...&b=a&[a]=[c]
[error] Actual:   ...&b=a&[c]=[&d=]
...
[error] x throw an IllegalArgumentException for an invalid query string
[error]    Expected: java.lang.IllegalArgumentException. Got nothing (MockHttpRequestSpec.scala:90)
@Shadowfiend
Owner

So it looks like a couple of things need to happen:

  • case Array(key) is not valid if key.isEmpty, so we need a guard on that match
  • In the test, you need to compare getRequestURL.toString to the appropriate expected URL
  • In the test, you need to compare getQueryString to the appropriate expected query string
@Shadowfiend
Owner

Any updates on this?

@dmclean62
@Shadowfiend
Owner

No problem, just wanted to ping and see :)

@Shadowfiend
Owner

Let me know if you need someone else to step in and make the remaining changes; I may have a moment to look at it this weekend and wrap things up :)

@dmclean62
@Shadowfiend
Owner

Ok, looks like that double-ampersand test also specifically tests the situation where the querystring is just foo, which was an IllegalArgumentException but shouldn't be anymore. So line 91 in the spec should probably go away, or change to must not throwA.... So that test is still failing at the moment…

The other test also still isn't passing, because you're expecting the serialization of the no-value parameter to just be d, but it'll be d=. Changing lines 58 and 59 of the spec to add an = to the end of the expected strings should fix that :)

@Shadowfiend
Owner

(As a side note, to double-check the test you can just:

$ sbt
> project lift-testkit
> test

)

@farmdawgnation
Collaborator

It'd be really cool to get this into M3. @dmclean62 Anything I can do to help out? :)

@Shadowfiend
Owner

@dmclean62 I'm going to go ahead and apply the last couple of fixes and merge this in today I think :)

@Shadowfiend Shadowfiend merged commit a4510e6 into lift:master
@fmpwizard fmpwizard added this to the 2.6-M3 milestone
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 23, 2014
  1. @dmclean62

    Update MockHttpRequestSpec.scala

    dmclean62 authored
    Added test for parameters with empty values
  2. @dmclean62

    Update MockHttpServletRequest.scala

    dmclean62 authored
    Add case matching a parameter with no/empty value ("a=" or just "a")
  3. @dmclean62

    Added my name and email

    dmclean62 authored
  4. @dmclean62

    Corrected formatting

    dmclean62 authored
Commits on Feb 7, 2014
  1. @dmclean62
  2. @dmclean62
This page is out of date. Refresh to see the latest.
View
4 contributors.md
@@ -192,4 +192,8 @@ Christopher Webster
### Email: ###
cwebster93 at gmail .. com
+### Name: ###
+Donald McLean
+### Email: ###
+dmclean62 @@ gmail .. com
View
5 web/testkit/src/main/scala/net/liftweb/mocks/MockHttpServletRequest.scala
@@ -216,6 +216,11 @@ class MockHttpServletRequest(val url : String = null, var contextPath : String =
// Append to the current key's value
newParams += key -> value
}
+ case Array("") => throw new IllegalArgumentException("Invalid query string: \"" + q + "\"")
+ case Array(key) => {
+ // Append to the current key's value
+ newParams += key -> ""
+ }
case invalid => throw new IllegalArgumentException("Invalid query string: \"" + q + "\"")
}
}
View
15 web/testkit/src/test/scala/net/liftweb/http/testing/MockHttpRequestSpec.scala
@@ -29,6 +29,7 @@ object MockHttpRequestSpec extends Specification {
val IF_MODIFIED_HEADER = "If-Modified-Since"
val TEST_URL = "https://foo.com/test/this/page?a=b&b=a&a=c"
+ val TEST_URL_BP = "https://foo.com/test/this/page?a=b&b=a&c=&d"
"MockHttpRequest" should {
@@ -46,6 +47,20 @@ object MockHttpRequestSpec extends Specification {
testRequest.getParameter("b") must_== "a"
}
+ "parse parameters with empty values" in {
+ val testRequest = new MockHttpServletRequest(TEST_URL_BP, "/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_BP
+ testRequest.getQueryString must_== "a=b&b=a&c=&d"
+ testRequest.getParameter("c") must_== ""
+ testRequest.getParameter("d") must_== ""
+ }
+
"correctly add and parse a date header" in {
val testRequest = new MockHttpServletRequest(TEST_URL, "/test")
Something went wrong with that request. Please try again.