You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

context_tests.go 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package test
  5. import (
  6. "net/http"
  7. "net/url"
  8. "testing"
  9. "code.gitea.io/gitea/modules/context"
  10. "github.com/stretchr/testify/assert"
  11. macaron "gopkg.in/macaron.v1"
  12. )
  13. // MockContext mock context for unit tests
  14. func MockContext(t *testing.T) *context.Context {
  15. var macaronContext *macaron.Context
  16. mac := macaron.New()
  17. mac.Get("*/", func(ctx *macaron.Context) {
  18. macaronContext = ctx
  19. })
  20. req, err := http.NewRequest("GET", "star", nil)
  21. assert.NoError(t, err)
  22. req.Form = url.Values{}
  23. mac.ServeHTTP(&mockResponseWriter{}, req)
  24. assert.NotNil(t, macaronContext)
  25. assert.EqualValues(t, req, macaronContext.Req.Request)
  26. macaronContext.Locale = &mockLocale{}
  27. macaronContext.Resp = &mockResponseWriter{}
  28. macaronContext.Render = &mockRender{ResponseWriter: macaronContext.Resp}
  29. return &context.Context{
  30. Context: macaronContext,
  31. }
  32. }
  33. type mockLocale struct{}
  34. func (l mockLocale) Language() string {
  35. return "en"
  36. }
  37. func (l mockLocale) Tr(s string, _ ...interface{}) string {
  38. return "test translation"
  39. }
  40. type mockResponseWriter struct {
  41. status int
  42. size int
  43. }
  44. func (rw *mockResponseWriter) Header() http.Header {
  45. return map[string][]string{}
  46. }
  47. func (rw *mockResponseWriter) Write(b []byte) (int, error) {
  48. rw.size += len(b)
  49. return len(b), nil
  50. }
  51. func (rw *mockResponseWriter) WriteHeader(status int) {
  52. rw.status = status
  53. }
  54. func (rw *mockResponseWriter) Flush() {
  55. }
  56. func (rw *mockResponseWriter) Status() int {
  57. return rw.status
  58. }
  59. func (rw *mockResponseWriter) Written() bool {
  60. return rw.status > 0
  61. }
  62. func (rw *mockResponseWriter) Size() int {
  63. return rw.size
  64. }
  65. func (rw *mockResponseWriter) Before(b macaron.BeforeFunc) {
  66. b(rw)
  67. }
  68. type mockRender struct {
  69. http.ResponseWriter
  70. }
  71. func (tr *mockRender) SetResponseWriter(rw http.ResponseWriter) {
  72. tr.ResponseWriter = rw
  73. }
  74. func (tr *mockRender) JSON(int, interface{}) {
  75. }
  76. func (tr *mockRender) JSONString(interface{}) (string, error) {
  77. return "", nil
  78. }
  79. func (tr *mockRender) RawData(status int, _ []byte) {
  80. tr.Status(status)
  81. }
  82. func (tr *mockRender) PlainText(status int, _ []byte) {
  83. tr.Status(status)
  84. }
  85. func (tr *mockRender) HTML(status int, _ string, _ interface{}, _ ...macaron.HTMLOptions) {
  86. tr.Status(status)
  87. }
  88. func (tr *mockRender) HTMLSet(status int, _ string, _ string, _ interface{}, _ ...macaron.HTMLOptions) {
  89. tr.Status(status)
  90. }
  91. func (tr *mockRender) HTMLSetString(string, string, interface{}, ...macaron.HTMLOptions) (string, error) {
  92. return "", nil
  93. }
  94. func (tr *mockRender) HTMLString(string, interface{}, ...macaron.HTMLOptions) (string, error) {
  95. return "", nil
  96. }
  97. func (tr *mockRender) HTMLSetBytes(string, string, interface{}, ...macaron.HTMLOptions) ([]byte, error) {
  98. return nil, nil
  99. }
  100. func (tr *mockRender) HTMLBytes(string, interface{}, ...macaron.HTMLOptions) ([]byte, error) {
  101. return nil, nil
  102. }
  103. func (tr *mockRender) XML(status int, _ interface{}) {
  104. tr.Status(status)
  105. }
  106. func (tr *mockRender) Error(status int, _ ...string) {
  107. tr.Status(status)
  108. }
  109. func (tr *mockRender) Status(status int) {
  110. tr.ResponseWriter.WriteHeader(status)
  111. }
  112. func (tr *mockRender) SetTemplatePath(string, string) {
  113. }
  114. func (tr *mockRender) HasTemplateSet(string) bool {
  115. return true
  116. }