restendpointhelper_test.go 951 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package utils
  2. import (
  3. "strings"
  4. "testing"
  5. )
  6. func TestGetPatternForRestEndpoint(t *testing.T) {
  7. expected := "/hello/"
  8. endpoints := [...]string{"/hello/{param1}", "/hello/{param1}/{param2}", "/hello/{param1}/world/{param2}"}
  9. for _, endpoint := range endpoints {
  10. if ep := getPatternForRestEndpoint(endpoint); ep != expected {
  11. t.Errorf("%s p does not match expected %s", ep, expected)
  12. }
  13. }
  14. }
  15. func TestReadParameter(t *testing.T) {
  16. expected := "world"
  17. endpoints := [...]string{
  18. "/hello/{p1}",
  19. "/hello/cruel/{p1}",
  20. "/hello/{p1}/my/friend",
  21. "/hello/{p1}/{p2}/friend",
  22. "/hello/{p2}/{p3}/{p1}",
  23. "/{p1}/is/nice",
  24. "/{p1}/{p1}/{p1}",
  25. }
  26. for _, ep := range endpoints {
  27. v, err := readParameter(ep, strings.Replace(ep, "{p1}", expected, -1), "p1")
  28. if err != nil {
  29. t.Errorf("Unexpected error when reading parameter: %s", err.Error())
  30. }
  31. if v != expected {
  32. t.Errorf("'%s' should have returned %s", ep, expected)
  33. }
  34. }
  35. }