url_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package util
  2. import (
  3. "testing"
  4. . "github.com/smartystreets/goconvey/convey"
  5. "net/url"
  6. )
  7. func TestUrl(t *testing.T) {
  8. Convey("When joining two urls where right hand side is empty", t, func() {
  9. result := JoinUrlFragments("http://localhost:8080", "")
  10. So(result, ShouldEqual, "http://localhost:8080")
  11. })
  12. Convey("When joining two urls where right hand side is empty and lefthand side has a trailing slash", t, func() {
  13. result := JoinUrlFragments("http://localhost:8080/", "")
  14. So(result, ShouldEqual, "http://localhost:8080/")
  15. })
  16. Convey("When joining two urls where neither has a trailing slash", t, func() {
  17. result := JoinUrlFragments("http://localhost:8080", "api")
  18. So(result, ShouldEqual, "http://localhost:8080/api")
  19. })
  20. Convey("When joining two urls where lefthand side has a trailing slash", t, func() {
  21. result := JoinUrlFragments("http://localhost:8080/", "api")
  22. So(result, ShouldEqual, "http://localhost:8080/api")
  23. })
  24. Convey("When joining two urls where righthand side has preceding slash", t, func() {
  25. result := JoinUrlFragments("http://localhost:8080", "/api")
  26. So(result, ShouldEqual, "http://localhost:8080/api")
  27. })
  28. Convey("When joining two urls where righthand side has trailing slash", t, func() {
  29. result := JoinUrlFragments("http://localhost:8080", "api/")
  30. So(result, ShouldEqual, "http://localhost:8080/api/")
  31. })
  32. Convey("When joining two urls where lefthand side has a trailing slash and righthand side has preceding slash", t, func() {
  33. result := JoinUrlFragments("http://localhost:8080/", "/api/")
  34. So(result, ShouldEqual, "http://localhost:8080/api/")
  35. })
  36. }
  37. func TestNewUrlQueryReader(t *testing.T) {
  38. u, _ := url.Parse("http://www.abc.com/foo?bar=baz&bar2=baz2")
  39. uqr, _ := NewUrlQueryReader(u)
  40. Convey("when trying to retrieve the first query value", t, func() {
  41. result := uqr.Get("bar", "foodef")
  42. So(result, ShouldEqual, "baz")
  43. })
  44. Convey("when trying to retrieve the second query value", t, func() {
  45. result := uqr.Get("bar2", "foodef")
  46. So(result, ShouldEqual, "baz2")
  47. })
  48. Convey("when trying to retrieve from a non-existent key, the default value is returned", t, func() {
  49. result := uqr.Get("bar3", "foodef")
  50. So(result, ShouldEqual, "foodef")
  51. })
  52. }