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

context_tests.go 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. 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. type mockLocale struct{}
  47. func (l mockLocale) Language() string {
  48. return "en"
  49. }
  50. func (l mockLocale) Tr(s string, _ ...interface{}) string {
  51. return s
  52. }
  53. type mockResponseWriter struct {
  54. status int
  55. size int
  56. }
  57. func (rw *mockResponseWriter) Header() http.Header {
  58. return map[string][]string{}
  59. }
  60. func (rw *mockResponseWriter) Write(b []byte) (int, error) {
  61. rw.size += len(b)
  62. return len(b), nil
  63. }
  64. func (rw *mockResponseWriter) WriteHeader(status int) {
  65. rw.status = status
  66. }
  67. func (rw *mockResponseWriter) Flush() {
  68. }
  69. func (rw *mockResponseWriter) Status() int {
  70. return rw.status
  71. }
  72. func (rw *mockResponseWriter) Written() bool {
  73. return rw.status > 0
  74. }
  75. func (rw *mockResponseWriter) Size() int {
  76. return rw.size
  77. }
  78. func (rw *mockResponseWriter) Before(b macaron.BeforeFunc) {
  79. b(rw)
  80. }
  81. type mockRender struct {
  82. http.ResponseWriter
  83. }
  84. func (tr *mockRender) SetResponseWriter(rw http.ResponseWriter) {
  85. tr.ResponseWriter = rw
  86. }
  87. func (tr *mockRender) JSON(status int, _ interface{}) {
  88. tr.Status(status)
  89. }
  90. func (tr *mockRender) JSONString(interface{}) (string, error) {
  91. return "", nil
  92. }
  93. func (tr *mockRender) RawData(status int, _ []byte) {
  94. tr.Status(status)
  95. }
  96. func (tr *mockRender) PlainText(status int, _ []byte) {
  97. tr.Status(status)
  98. }
  99. func (tr *mockRender) HTML(status int, _ string, _ interface{}, _ ...macaron.HTMLOptions) {
  100. tr.Status(status)
  101. }
  102. func (tr *mockRender) HTMLSet(status int, _ string, _ string, _ interface{}, _ ...macaron.HTMLOptions) {
  103. tr.Status(status)
  104. }
  105. func (tr *mockRender) HTMLSetString(string, string, interface{}, ...macaron.HTMLOptions) (string, error) {
  106. return "", nil
  107. }
  108. func (tr *mockRender) HTMLString(string, interface{}, ...macaron.HTMLOptions) (string, error) {
  109. return "", nil
  110. }
  111. func (tr *mockRender) HTMLSetBytes(string, string, interface{}, ...macaron.HTMLOptions) ([]byte, error) {
  112. return nil, nil
  113. }
  114. func (tr *mockRender) HTMLBytes(string, interface{}, ...macaron.HTMLOptions) ([]byte, error) {
  115. return nil, nil
  116. }
  117. func (tr *mockRender) XML(status int, _ interface{}) {
  118. tr.Status(status)
  119. }
  120. func (tr *mockRender) Error(status int, _ ...string) {
  121. tr.Status(status)
  122. }
  123. func (tr *mockRender) Status(status int) {
  124. tr.ResponseWriter.WriteHeader(status)
  125. }
  126. func (tr *mockRender) SetTemplatePath(string, string) {
  127. }
  128. func (tr *mockRender) HasTemplateSet(string) bool {
  129. return true
  130. }