Add tests for Uri; add fixes for bugs uncovered by tests.