engine_test.go 3.9 KB

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