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 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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/git"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/context"
  12. "github.com/go-macaron/session"
  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. Values: make(url.Values),
  34. },
  35. }
  36. }
  37. // LoadRepo load a repo into a test context.
  38. func LoadRepo(t *testing.T, ctx *context.Context, repoID int64) {
  39. ctx.Repo = &context.Repository{}
  40. ctx.Repo.Repository = models.AssertExistsAndLoadBean(t, &models.Repository{ID: repoID}).(*models.Repository)
  41. }
  42. // LoadUser load a user into a test context.
  43. func LoadUser(t *testing.T, ctx *context.Context, userID int64) {
  44. ctx.User = models.AssertExistsAndLoadBean(t, &models.User{ID: userID}).(*models.User)
  45. }
  46. // LoadGitRepo load a git repo into a test context. Requires that ctx.Repo has
  47. // already been populated.
  48. func LoadGitRepo(t *testing.T, ctx *context.Context) {
  49. assert.NoError(t, ctx.Repo.Repository.GetOwner())
  50. var err error
  51. ctx.Repo.GitRepo, err = git.OpenRepository(ctx.Repo.Repository.RepoPath())
  52. assert.NoError(t, err)
  53. }
  54. type mockLocale struct{}
  55. func (l mockLocale) Language() string {
  56. return "en"
  57. }
  58. func (l mockLocale) Tr(s string, _ ...interface{}) string {
  59. return s
  60. }
  61. type mockResponseWriter struct {
  62. status int
  63. size int
  64. }
  65. func (rw *mockResponseWriter) Header() http.Header {
  66. return map[string][]string{}
  67. }
  68. func (rw *mockResponseWriter) Write(b []byte) (int, error) {
  69. rw.size += len(b)
  70. return len(b), nil
  71. }
  72. func (rw *mockResponseWriter) WriteHeader(status int) {
  73. rw.status = status
  74. }
  75. func (rw *mockResponseWriter) Flush() {
  76. }
  77. func (rw *mockResponseWriter) Status() int {
  78. return rw.status
  79. }
  80. func (rw *mockResponseWriter) Written() bool {
  81. return rw.status > 0
  82. }
  83. func (rw *mockResponseWriter) Size() int {
  84. return rw.size
  85. }
  86. func (rw *mockResponseWriter) Before(b macaron.BeforeFunc) {
  87. b(rw)
  88. }
  89. type mockRender struct {
  90. http.ResponseWriter
  91. }
  92. func (tr *mockRender) SetResponseWriter(rw http.ResponseWriter) {
  93. tr.ResponseWriter = rw
  94. }
  95. func (tr *mockRender) JSON(status int, _ interface{}) {
  96. tr.Status(status)
  97. }
  98. func (tr *mockRender) JSONString(interface{}) (string, error) {
  99. return "", nil
  100. }
  101. func (tr *mockRender) RawData(status int, _ []byte) {
  102. tr.Status(status)
  103. }
  104. func (tr *mockRender) PlainText(status int, _ []byte) {
  105. tr.Status(status)
  106. }
  107. func (tr *mockRender) HTML(status int, _ string, _ interface{}, _ ...macaron.HTMLOptions) {
  108. tr.Status(status)
  109. }
  110. func (tr *mockRender) HTMLSet(status int, _ string, _ string, _ interface{}, _ ...macaron.HTMLOptions) {
  111. tr.Status(status)
  112. }
  113. func (tr *mockRender) HTMLSetString(string, string, interface{}, ...macaron.HTMLOptions) (string, error) {
  114. return "", nil
  115. }
  116. func (tr *mockRender) HTMLString(string, interface{}, ...macaron.HTMLOptions) (string, error) {
  117. return "", nil
  118. }
  119. func (tr *mockRender) HTMLSetBytes(string, string, interface{}, ...macaron.HTMLOptions) ([]byte, error) {
  120. return nil, nil
  121. }
  122. func (tr *mockRender) HTMLBytes(string, interface{}, ...macaron.HTMLOptions) ([]byte, error) {
  123. return nil, nil
  124. }
  125. func (tr *mockRender) XML(status int, _ interface{}) {
  126. tr.Status(status)
  127. }
  128. func (tr *mockRender) Error(status int, _ ...string) {
  129. tr.Status(status)
  130. }
  131. func (tr *mockRender) Status(status int) {
  132. tr.ResponseWriter.WriteHeader(status)
  133. }
  134. func (tr *mockRender) SetTemplatePath(string, string) {
  135. }
  136. func (tr *mockRender) HasTemplateSet(string) bool {
  137. return true
  138. }