domain_api_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package domain
  2. import (
  3. "testing"
  4. )
  5. func TestGetTargetURL(t *testing.T) {
  6. // Empty repos should not find any short.
  7. RegisterRepositories(make(MockShortRepo), nil)
  8. _, err := GetTargetURL("", nil)
  9. if err == nil {
  10. t.Error("empty repository should fail to get a target")
  11. }
  12. var mockSR MockShortRepo = make(map[ShortURL]TargetURL)
  13. shortURL := "some_short"
  14. expected := TargetURL{URL: URL("some target")}
  15. mockSR[ShortURL{URL: URL(shortURL)}] = expected
  16. RegisterRepositories(mockSR, nil)
  17. // Existing shorts should return proper target.
  18. actual, err := GetTargetURL(shortURL, nil)
  19. if err != nil {
  20. t.Error("short with an existing target should be found")
  21. }
  22. if actual != string(expected.URL) {
  23. t.Error("short with an existing target should return it")
  24. }
  25. // Non-existent shorts should fail.
  26. _, err = GetTargetURL(shortURL+"_other", nil)
  27. if err == nil {
  28. t.Error("short with no existing target should not be found")
  29. }
  30. }
  31. func TestGetShortURL(t *testing.T) {
  32. // No repo should be able to return a short for an empty target.
  33. RegisterRepositories(nil, nil)
  34. _, _, err := GetShortURL("", nil)
  35. if err == nil {
  36. t.Error("empty repository should fail to get a short for an empty URL")
  37. }
  38. // Empty repos unable to create shorts should not find any short.
  39. RegisterRepositories(nil, MakeMockTargetRepo(false))
  40. _, isNew, err := GetShortURL("some_target", nil)
  41. if err == nil {
  42. t.Error("empty repository should fail to get a short")
  43. }
  44. if isNew {
  45. t.Error("empty repository without create ability should fail to create a short")
  46. }
  47. // Empty repos able to create shorts should return a short for a valid target.
  48. RegisterRepositories(nil, MakeMockTargetRepo(true))
  49. const targetURL = "some_target"
  50. actual, isNew, err := GetShortURL(targetURL, nil)
  51. if err != nil {
  52. t.Error("repository with create ability should not fail to create a short")
  53. }
  54. if !isNew {
  55. t.Error("repository creating a new short should report is as new")
  56. }
  57. if len(actual) == 0 {
  58. t.Error("repository should not return empty short URLs")
  59. }
  60. // Specific to mock.
  61. if actual != targetURL {
  62. t.Error("mock repository should return correct generated short")
  63. }
  64. // Repos with a matching short for a target should return it as non-new.
  65. actual, isNew, err = GetShortURL(targetURL, nil)
  66. if err != nil {
  67. t.Error("repository with matching short should return it")
  68. }
  69. if isNew {
  70. t.Error("repository with matching short should now claim it is new")
  71. }
  72. // Specific to mock.
  73. if actual != targetURL {
  74. t.Error("mock repository should return correct generated short")
  75. }
  76. }