Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

context_tests.go 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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/models"
  10. "code.gitea.io/gitea/modules/context"
  11. "github.com/go-macaron/session"
  12. _ "github.com/mattn/go-sqlite3" // for the test engine
  13. "github.com/stretchr/testify/assert"
  14. "gopkg.in/macaron.v1"
  15. )
  16. // MockContext mock context for unit tests
  17. func MockContext(t *testing.T, path string) *context.Context {
  18. var macaronContext macaron.Context
  19. macaronContext.ReplaceAllParams(macaron.Params{})
  20. macaronContext.Locale = &mockLocale{}
  21. requestURL, err := url.Parse(path)
  22. assert.NoError(t, err)
  23. macaronContext.Req = macaron.Request{Request: &http.Request{
  24. URL: requestURL,
  25. Form: url.Values{},
  26. }}
  27. macaronContext.Resp = &mockResponseWriter{}
  28. macaronContext.Render = &mockRender{ResponseWriter: macaronContext.Resp}
  29. macaronContext.Data = map[string]interface{}{}
  30. return &context.Context{
  31. Context: &macaronContext,
  32. Flash: &session.Flash{},
  33. }
  34. }
  35. // LoadRepo load a repo into a test context.
  36. func LoadRepo(t *testing.T, ctx *context.Context, repoID int64) {
  37. ctx.Repo = &context.Repository{}
  38. ctx.Repo.Repository = models.AssertExistsAndLoadBean(t, &models.Repository{ID: repoID}).(*models.Repository)
  39. }
  40. // LoadUser load a user into a test context.
  41. func LoadUser(t *testing.T, ctx *context.Context, userID int64) {
  42. ctx.User = models.AssertExistsAndLoadBean(t, &models.User{ID: userID}).(*models.User)
  43. }
  44. type mockLocale struct{}
  45. func (l mockLocale) Language() string {
  46. return "en"
  47. }
  48. func (l mockLocale) Tr(s string, _ ...interface{}) string {
  49. return s
  50. }
  51. type mockResponseWriter struct {
  52. status int
  53. size int
  54. }
  55. func (rw *mockResponseWriter) Header() http.Header {
  56. return map[string][]string{}
  57. }
  58. func (rw *mockResponseWriter) Write(b []byte) (int, error) {
  59. rw.size += len(b)
  60. return len(b), nil
  61. }
  62. func (rw *mockResponseWriter) WriteHeader(status int) {
  63. rw.status = status
  64. }
  65. func (rw *mockResponseWriter) Flush() {
  66. }
  67. func (rw *mockResponseWriter) Status() int {
  68. return rw.status
  69. }
  70. func (rw *mockResponseWriter) Written() bool {
  71. return rw.status > 0
  72. }
  73. func (rw *mockResponseWriter) Size() int {
  74. return rw.size
  75. }
  76. func (rw *mockResponseWriter) Before(b macaron.BeforeFunc) {
  77. b(rw)
  78. }
  79. type mockRender struct {
  80. http.ResponseWriter
  81. }
  82. func (tr *mockRender) SetResponseWriter(rw http.ResponseWriter) {
  83. tr.ResponseWriter = rw
  84. }
  85. func (tr *mockRender) JSON(status int, _ interface{}) {
  86. tr.Status(status)
  87. }
  88. func (tr *mockRender) JSONString(interface{}) (string, error) {
  89. return "", nil
  90. }
  91. func (tr *mockRender) RawData(status int, _ []byte) {
  92. tr.Status(status)
  93. }
  94. func (tr *mockRender) PlainText(status int, _ []byte) {
  95. tr.Status(status)
  96. }
  97. func (tr *mockRender) HTML(status int, _ string, _ interface{}, _ ...macaron.HTMLOptions) {
  98. tr.Status(status)
  99. }
  100. func (tr *mockRender) HTMLSet(status int, _ string, _ string, _ interface{}, _ ...macaron.HTMLOptions) {
  101. tr.Status(status)
  102. }
  103. func (tr *mockRender) HTMLSetString(string, string, interface{}, ...macaron.HTMLOptions) (string, error) {
  104. return "", nil
  105. }
  106. func (tr *mockRender) HTMLString(string, interface{}, ...macaron.HTMLOptions) (string, error) {
  107. return "", nil
  108. }
  109. func (tr *mockRender) HTMLSetBytes(string, string, interface{}, ...macaron.HTMLOptions) ([]byte, error) {
  110. return nil, nil
  111. }
  112. func (tr *mockRender) HTMLBytes(string, interface{}, ...macaron.HTMLOptions) ([]byte, error) {
  113. return nil, nil
  114. }
  115. func (tr *mockRender) XML(status int, _ interface{}) {
  116. tr.Status(status)
  117. }
  118. func (tr *mockRender) Error(status int, _ ...string) {
  119. tr.Status(status)
  120. }
  121. func (tr *mockRender) Status(status int) {
  122. tr.ResponseWriter.WriteHeader(status)
  123. }
  124. func (tr *mockRender) SetTemplatePath(string, string) {
  125. }
  126. func (tr *mockRender) HasTemplateSet(string) bool {
  127. return true
  128. }