test_helpers.go 846 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright 2017 Manu Martinez-Almeida. 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 gin
  5. import (
  6. "net/http"
  7. "net/http/httptest"
  8. )
  9. // CreateTestContext returns a fresh engine and context for testing purposes
  10. func CreateTestContext(w http.ResponseWriter) (c *Context, r *Engine) {
  11. r = New()
  12. c = r.allocateContext()
  13. c.reset()
  14. c.writermem.reset(w)
  15. return
  16. }
  17. type TestResponseRecorder struct {
  18. *httptest.ResponseRecorder
  19. closeChannel chan bool
  20. }
  21. func (r *TestResponseRecorder) CloseNotify() <-chan bool {
  22. return r.closeChannel
  23. }
  24. func (r *TestResponseRecorder) closeClient() {
  25. r.closeChannel <- true
  26. }
  27. func CreateTestResponseRecorder() *TestResponseRecorder {
  28. return &TestResponseRecorder{
  29. httptest.NewRecorder(),
  30. make(chan bool, 1),
  31. }
  32. }