Fixes fragment encoding and closes #6455

I added some tests for #6455 and fixed an issue that involved incorrectly building URLs that have both a fragment and a query.