NET-A-PORTER/scala-uri

Default Query Serialization

Closed this issue · 3 comments

You may or may not want to do this:

  "Query parameters" should "use application/x-www-form-urlencoded serialization by default" in {
    val uri = "http://example.com/" ?
      ("safe" -> "*-.0-9A-Za-z_") &
      ("bad" -> "\u0021\u0029\u002B\u002C\u002F\u003A\u0040\u005B\u0060\u007B") &
      ("control" -> "\u0019\u007F") &
      ("space" -> " ")
    uri.toString should equal("http://example.com/?safe=*-.0-9A-Za-z_&bad=%21%29%2B%2C%2F%3A%40%5B%60%7B&control=%19%7F&space=+")
  }

This is following http://url.spec.whatwg.org/#urlencoded-serializing since http://tools.ietf.org/html/rfc3986#section-3.4 doesn't really comment on how to serialize the key-value pairs. (It just states that pchars, /, and ? are all valid chars for a URI's query.)

I think it's reasonable to have the space encode as %20 instead of a + by default, since you could always mix in the spaceAsPlus encoder, so I separated that out. I also separated the control chars since they don't currently show up in the final output.

Control characters is a good catch - I've had a painful experience with that before.

Thanks for all the tickets you have raised. I'd this library like to follow rfc3986. Hopefully query parameter encoding is closer to spec this now. Let me know if you spot any other places we are diverging from spec.

You're welcome, and thanks for the fixes!