Capstan is a Golang web framework that shares some similarities with others in its segment.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
3.6KB

  1. package capstan_test
  2. import (
  3. "testing"
  4. "git.destrealm.org/go/capstan"
  5. "git.destrealm.org/go/capstan/config"
  6. )
  7. func Test_URLMapper_For(t *testing.T) {
  8. var err error
  9. var out string
  10. var builder *capstan.URLBuilder
  11. mapper := capstan.NewURLMapper(&config.ServerConfig{
  12. FullHost: "localhost",
  13. Protocol: "http",
  14. })
  15. mapper.Map(&capstan.Route{
  16. Name: "index",
  17. Path: "/",
  18. Method: "GET",
  19. Endpoint: func(ctx capstan.Context) error { return nil },
  20. })
  21. mapper.Map(&capstan.Route{
  22. Name: "test",
  23. Path: "/test/{value}",
  24. Method: "GET",
  25. Endpoint: func(ctx capstan.Context) error { return nil },
  26. })
  27. builder = mapper.For("index:get")
  28. if builder == nil {
  29. t.Error("URLBUilder should be returned from .For(); got nil")
  30. return
  31. }
  32. builder.External = true
  33. out = builder.Encode()
  34. if err != nil {
  35. t.Error("URLBuilder.Encode() failed:", err)
  36. return
  37. }
  38. if out != "http://localhost/" {
  39. t.Error("URL did not match")
  40. t.Error(`expected: "http://localhost/"`)
  41. t.Errorf(`actual: "%s"`, out)
  42. }
  43. builder = mapper.For("test:get")
  44. if builder == nil {
  45. t.Error("URLBUilder should be returned from .For(); got nil")
  46. return
  47. }
  48. builder = mapper.For("index:get")
  49. if builder == nil {
  50. t.Error("URLBUilder should be returned from .For(); got nil")
  51. return
  52. }
  53. builder.External = false
  54. out = builder.Encode()
  55. if err != nil {
  56. t.Error("URLBuilder.Encode() failed:", err)
  57. return
  58. }
  59. if out != "/" {
  60. t.Error("URL did not match")
  61. t.Error(`expected: "/"`)
  62. t.Errorf(`actual: "%s"`, out)
  63. }
  64. builder = mapper.For("test:get")
  65. if builder == nil {
  66. t.Error("URLBUilder should be returned from .For(); got nil")
  67. return
  68. }
  69. builder = builder.Param("value", "unit-test")
  70. builder.External = true
  71. out = builder.Encode()
  72. if err != nil {
  73. t.Error("URLBuilder.Encode() failed:", err)
  74. return
  75. }
  76. if out != "http://localhost/test/unit-test" {
  77. t.Error("URL did not match")
  78. t.Error(`expected: "http://localhost/test/unit-test"`)
  79. t.Errorf(`actual: "%s"`, out)
  80. }
  81. builder = mapper.For("test:get")
  82. if builder == nil {
  83. t.Error("URLBUilder should be returned from .For(); got nil")
  84. return
  85. }
  86. builder = builder.Param("value", "unit-test")
  87. builder.External = false
  88. out = builder.Encode()
  89. if err != nil {
  90. t.Error("URLBuilder.Encode() failed:", err)
  91. return
  92. }
  93. if out != "/test/unit-test" {
  94. t.Error("URL did not match")
  95. t.Error(`expected: "/test/unit-test"`)
  96. t.Errorf(`actual: "%s"`, out)
  97. }
  98. builder = mapper.For("test:get")
  99. if builder == nil {
  100. t.Error("URLBUilder should be returned from .For(); got nil")
  101. return
  102. }
  103. builder = builder.Param("value", "unit-test")
  104. builder = builder.Param("query", "string-value")
  105. builder.External = true
  106. out = builder.Encode()
  107. if err != nil {
  108. t.Error("URLBuilder.Encode() failed:", err)
  109. return
  110. }
  111. if out != "http://localhost/test/unit-test?query=string-value" {
  112. t.Error("URL did not match")
  113. t.Error(`expected: "http://localhost/test/unit-test?query=string-value"`)
  114. t.Errorf(`actual: "%s"`, out)
  115. }
  116. }
  117. func Test_URLMapper_ForStatic(t *testing.T) {
  118. var err error
  119. var out string
  120. var builder *capstan.URLBuilder
  121. mapper := capstan.NewURLMapper(&config.ServerConfig{
  122. FullHost: "localhost",
  123. Protocol: "http",
  124. })
  125. mapper.Static = "/static/"
  126. builder = mapper.For("static")
  127. if builder == nil {
  128. t.Error("URLBUilder should be returned from .For(); got nil")
  129. return
  130. }
  131. builder.Asset("sample.jpg")
  132. builder.External = true
  133. out = builder.Encode()
  134. if err != nil {
  135. t.Error("URLBuilder.Encode() failed:", err)
  136. return
  137. }
  138. if out != "http://localhost/static/sample.jpg" {
  139. t.Error("URL did not match")
  140. t.Error(`expected: "http://localhost/static/sample.jpg"`)
  141. t.Errorf(`actual: "%s"`, out)
  142. }
  143. }