utils_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package uritemplates
  2. import (
  3. "testing"
  4. )
  5. type ExpandTest struct {
  6. in string
  7. expansions map[string]string
  8. want string
  9. }
  10. var expandTests = []ExpandTest{
  11. // #0: no expansions
  12. {
  13. "http://www.golang.org/",
  14. map[string]string{},
  15. "http://www.golang.org/",
  16. },
  17. // #1: one expansion, no escaping
  18. {
  19. "http://www.golang.org/{bucket}/delete",
  20. map[string]string{
  21. "bucket": "red",
  22. },
  23. "http://www.golang.org/red/delete",
  24. },
  25. // #2: one expansion, with hex escapes
  26. {
  27. "http://www.golang.org/{bucket}/delete",
  28. map[string]string{
  29. "bucket": "red/blue",
  30. },
  31. "http://www.golang.org/red%2Fblue/delete",
  32. },
  33. // #3: one expansion, with space
  34. {
  35. "http://www.golang.org/{bucket}/delete",
  36. map[string]string{
  37. "bucket": "red or blue",
  38. },
  39. "http://www.golang.org/red%20or%20blue/delete",
  40. },
  41. // #4: expansion not found
  42. {
  43. "http://www.golang.org/{object}/delete",
  44. map[string]string{
  45. "bucket": "red or blue",
  46. },
  47. "http://www.golang.org//delete",
  48. },
  49. // #5: multiple expansions
  50. {
  51. "http://www.golang.org/{one}/{two}/{three}/get",
  52. map[string]string{
  53. "one": "ONE",
  54. "two": "TWO",
  55. "three": "THREE",
  56. },
  57. "http://www.golang.org/ONE/TWO/THREE/get",
  58. },
  59. // #6: utf-8 characters
  60. {
  61. "http://www.golang.org/{bucket}/get",
  62. map[string]string{
  63. "bucket": "£100",
  64. },
  65. "http://www.golang.org/%C2%A3100/get",
  66. },
  67. // #7: punctuations
  68. {
  69. "http://www.golang.org/{bucket}/get",
  70. map[string]string{
  71. "bucket": `/\@:,.*~`,
  72. },
  73. "http://www.golang.org/%2F%5C%40%3A%2C.%2A~/get",
  74. },
  75. // #8: mis-matched brackets
  76. {
  77. "http://www.golang.org/{bucket/get",
  78. map[string]string{
  79. "bucket": "red",
  80. },
  81. "",
  82. },
  83. // #9: "+" prefix for suppressing escape
  84. // See also: http://tools.ietf.org/html/rfc6570#section-3.2.3
  85. {
  86. "http://www.golang.org/{+topic}",
  87. map[string]string{
  88. "topic": "/topics/myproject/mytopic",
  89. },
  90. // The double slashes here look weird, but it's intentional
  91. "http://www.golang.org//topics/myproject/mytopic",
  92. },
  93. }
  94. func TestExpand(t *testing.T) {
  95. for i, test := range expandTests {
  96. got, _ := Expand(test.in, test.expansions)
  97. if got != test.want {
  98. t.Errorf("got %q expected %q in test %d", got, test.want, i)
  99. }
  100. }
  101. }