Improve query string handling. 

* Add objectToQuery and queryToObject.
* Start replacing fragile URL query string manipulation with those functions.
* Fix specs that incorrectly depended on exact URL string equality, and other spec cleanup.

Fixes #2165
It's probably best to be consistent, so I went through and switched all these places to use a ternary expression with `defined()`, which is almost as easy to read, I think.