engine_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package rest
  2. import (
  3. "errors"
  4. "net/http"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/tal-tech/go-zero/core/conf"
  8. )
  9. func TestNewEngine(t *testing.T) {
  10. yamls := []string{
  11. `Name: foo
  12. Port: 54321
  13. `,
  14. `Name: foo
  15. Port: 54321
  16. CpuThreshold: 500
  17. `,
  18. `Name: foo
  19. Port: 54321
  20. CpuThreshold: 500
  21. Verbose: true
  22. `,
  23. }
  24. routes := []featuredRoutes{
  25. {
  26. jwt: jwtSetting{},
  27. signature: signatureSetting{},
  28. routes: []Route{{
  29. Method: http.MethodGet,
  30. Path: "/",
  31. Handler: func(w http.ResponseWriter, r *http.Request) {},
  32. }},
  33. },
  34. {
  35. priority: true,
  36. jwt: jwtSetting{},
  37. signature: signatureSetting{},
  38. routes: []Route{{
  39. Method: http.MethodGet,
  40. Path: "/",
  41. Handler: func(w http.ResponseWriter, r *http.Request) {},
  42. }},
  43. },
  44. {
  45. priority: true,
  46. jwt: jwtSetting{
  47. enabled: true,
  48. },
  49. signature: signatureSetting{},
  50. routes: []Route{{
  51. Method: http.MethodGet,
  52. Path: "/",
  53. Handler: func(w http.ResponseWriter, r *http.Request) {},
  54. }},
  55. },
  56. {
  57. priority: true,
  58. jwt: jwtSetting{
  59. enabled: true,
  60. prevSecret: "thesecret",
  61. },
  62. signature: signatureSetting{},
  63. routes: []Route{{
  64. Method: http.MethodGet,
  65. Path: "/",
  66. Handler: func(w http.ResponseWriter, r *http.Request) {},
  67. }},
  68. },
  69. {
  70. priority: true,
  71. jwt: jwtSetting{
  72. enabled: true,
  73. },
  74. signature: signatureSetting{},
  75. routes: []Route{{
  76. Method: http.MethodGet,
  77. Path: "/",
  78. Handler: func(w http.ResponseWriter, r *http.Request) {},
  79. }},
  80. },
  81. {
  82. priority: true,
  83. jwt: jwtSetting{
  84. enabled: true,
  85. },
  86. signature: signatureSetting{
  87. enabled: true,
  88. },
  89. routes: []Route{{
  90. Method: http.MethodGet,
  91. Path: "/",
  92. Handler: func(w http.ResponseWriter, r *http.Request) {},
  93. }},
  94. },
  95. {
  96. priority: true,
  97. jwt: jwtSetting{
  98. enabled: true,
  99. },
  100. signature: signatureSetting{
  101. enabled: true,
  102. SignatureConf: SignatureConf{
  103. Strict: true,
  104. },
  105. },
  106. routes: []Route{{
  107. Method: http.MethodGet,
  108. Path: "/",
  109. Handler: func(w http.ResponseWriter, r *http.Request) {},
  110. }},
  111. },
  112. {
  113. priority: true,
  114. jwt: jwtSetting{
  115. enabled: true,
  116. },
  117. signature: signatureSetting{
  118. enabled: true,
  119. SignatureConf: SignatureConf{
  120. Strict: true,
  121. PrivateKeys: []PrivateKeyConf{
  122. {
  123. Fingerprint: "a",
  124. KeyFile: "b",
  125. },
  126. },
  127. },
  128. },
  129. routes: []Route{{
  130. Method: http.MethodGet,
  131. Path: "/",
  132. Handler: func(w http.ResponseWriter, r *http.Request) {},
  133. }},
  134. },
  135. }
  136. for _, yaml := range yamls {
  137. for _, route := range routes {
  138. var cnf RestConf
  139. assert.Nil(t, conf.LoadConfigFromYamlBytes([]byte(yaml), &cnf))
  140. ng := newEngine(cnf)
  141. ng.AddRoutes(route)
  142. ng.use(func(next http.HandlerFunc) http.HandlerFunc {
  143. return func(w http.ResponseWriter, r *http.Request) {
  144. next.ServeHTTP(w, r)
  145. }
  146. })
  147. assert.NotNil(t, ng.StartWithRouter(mockedRouter{}))
  148. }
  149. }
  150. }
  151. type mockedRouter struct {
  152. }
  153. func (m mockedRouter) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
  154. }
  155. func (m mockedRouter) Handle(method string, path string, handler http.Handler) error {
  156. return errors.New("foo")
  157. }
  158. func (m mockedRouter) SetNotFoundHandler(handler http.Handler) {
  159. }
  160. func (m mockedRouter) SetNotAllowedHandler(handler http.Handler) {
  161. }