route_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package restful
  2. import (
  3. "testing"
  4. )
  5. // accept should match produces
  6. func TestMatchesAcceptPlainTextWhenProducePlainTextAsLast(t *testing.T) {
  7. r := Route{Produces: []string{"application/json", "text/plain"}}
  8. if !r.matchesAccept("text/plain") {
  9. t.Errorf("accept should match text/plain")
  10. }
  11. }
  12. // accept should match produces
  13. func TestMatchesAcceptStar(t *testing.T) {
  14. r := Route{Produces: []string{"application/xml"}}
  15. if !r.matchesAccept("*/*") {
  16. t.Errorf("accept should match star")
  17. }
  18. }
  19. // accept should match produces
  20. func TestMatchesAcceptIE(t *testing.T) {
  21. r := Route{Produces: []string{"application/xml"}}
  22. if !r.matchesAccept("text/html, application/xhtml+xml, */*") {
  23. t.Errorf("accept should match star")
  24. }
  25. }
  26. // accept should match produces
  27. func TestMatchesAcceptXml(t *testing.T) {
  28. r := Route{Produces: []string{"application/xml"}}
  29. if r.matchesAccept("application/json") {
  30. t.Errorf("accept should not match json")
  31. }
  32. if !r.matchesAccept("application/xml") {
  33. t.Errorf("accept should match xml")
  34. }
  35. }
  36. // accept should match produces
  37. func TestMatchesAcceptAny(t *testing.T) {
  38. r := Route{Produces: []string{"*/*"}}
  39. if !r.matchesAccept("application/json") {
  40. t.Errorf("accept should match json")
  41. }
  42. if !r.matchesAccept("application/xml") {
  43. t.Errorf("accept should match xml")
  44. }
  45. }
  46. // content type should match consumes
  47. func TestMatchesContentTypeXml(t *testing.T) {
  48. r := Route{Consumes: []string{"application/xml"}}
  49. if r.matchesContentType("application/json") {
  50. t.Errorf("accept should not match json")
  51. }
  52. if !r.matchesContentType("application/xml") {
  53. t.Errorf("accept should match xml")
  54. }
  55. }
  56. // content type should match consumes
  57. func TestMatchesContentTypeCharsetInformation(t *testing.T) {
  58. r := Route{Consumes: []string{"application/json"}}
  59. if !r.matchesContentType("application/json; charset=UTF-8") {
  60. t.Errorf("matchesContentType should ignore charset information")
  61. }
  62. }
  63. func TestMatchesPath_OneParam(t *testing.T) {
  64. params := doExtractParams("/from/{source}", 2, "/from/here", t)
  65. if params["source"] != "here" {
  66. t.Errorf("parameter mismatch here")
  67. }
  68. }
  69. func TestMatchesPath_Slash(t *testing.T) {
  70. params := doExtractParams("/", 0, "/", t)
  71. if len(params) != 0 {
  72. t.Errorf("expected empty parameters")
  73. }
  74. }
  75. func TestMatchesPath_SlashNonVar(t *testing.T) {
  76. params := doExtractParams("/any", 1, "/any", t)
  77. if len(params) != 0 {
  78. t.Errorf("expected empty parameters")
  79. }
  80. }
  81. func TestMatchesPath_TwoVars(t *testing.T) {
  82. params := doExtractParams("/from/{source}/to/{destination}", 4, "/from/AMS/to/NY", t)
  83. if params["source"] != "AMS" {
  84. t.Errorf("parameter mismatch AMS")
  85. }
  86. }
  87. func TestMatchesPath_VarOnFront(t *testing.T) {
  88. params := doExtractParams("{what}/from/{source}/", 3, "who/from/SOS/", t)
  89. if params["source"] != "SOS" {
  90. t.Errorf("parameter mismatch SOS")
  91. }
  92. }
  93. func TestExtractParameters_EmptyValue(t *testing.T) {
  94. params := doExtractParams("/fixed/{var}", 2, "/fixed/", t)
  95. if params["var"] != "" {
  96. t.Errorf("parameter mismatch var")
  97. }
  98. }
  99. func TestTokenizePath(t *testing.T) {
  100. if len(tokenizePath("/")) != 0 {
  101. t.Errorf("not empty path tokens")
  102. }
  103. }
  104. func doExtractParams(routePath string, size int, urlPath string, t *testing.T) map[string]string {
  105. r := Route{Path: routePath}
  106. r.postBuild()
  107. if len(r.pathParts) != size {
  108. t.Fatalf("len not %v %v, but %v", size, r.pathParts, len(r.pathParts))
  109. }
  110. return r.extractParameters(urlPath)
  111. }