path_expression_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. package restful
  2. import "testing"
  3. var tempregexs = []struct {
  4. template, regex string
  5. literalCount, varCount int
  6. }{
  7. {"", "^(/.*)?$", 0, 0},
  8. {"/a/{b}/c/", "^/a/([^/]+?)/c(/.*)?$", 2, 1},
  9. {"/{a}/{b}/{c-d-e}/", "^/([^/]+?)/([^/]+?)/([^/]+?)(/.*)?$", 0, 3},
  10. {"/{p}/abcde", "^/([^/]+?)/abcde(/.*)?$", 5, 1},
  11. {"/a/{b:*}", "^/a/(.*)(/.*)?$", 1, 1},
  12. {"/a/{b:[a-z]+}", "^/a/([a-z]+)(/.*)?$", 1, 1},
  13. }
  14. func TestTemplateToRegularExpression(t *testing.T) {
  15. ok := true
  16. for i, fixture := range tempregexs {
  17. actual, lCount, vCount, _ := templateToRegularExpression(fixture.template)
  18. if actual != fixture.regex {
  19. t.Logf("regex mismatch, expected:%v , actual:%v, line:%v\n", fixture.regex, actual, i) // 11 = where the data starts
  20. ok = false
  21. }
  22. if lCount != fixture.literalCount {
  23. t.Logf("literal count mismatch, expected:%v , actual:%v, line:%v\n", fixture.literalCount, lCount, i)
  24. ok = false
  25. }
  26. if vCount != fixture.varCount {
  27. t.Logf("variable count mismatch, expected:%v , actual:%v, line:%v\n", fixture.varCount, vCount, i)
  28. ok = false
  29. }
  30. }
  31. if !ok {
  32. t.Fatal("one or more expression did not match")
  33. }
  34. }