Micro & pluggable web framework for Go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
2.7KB

  1. // Copyright 2015 The Tango Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package tango
  5. import (
  6. "bytes"
  7. "compress/flate"
  8. "compress/gzip"
  9. "fmt"
  10. "io/ioutil"
  11. "net/http"
  12. "net/http/httptest"
  13. "testing"
  14. )
  15. type CompressExample struct {
  16. Compress // add this for ask compress according request accept-encoding
  17. }
  18. func (CompressExample) Get() string {
  19. return fmt.Sprintf("This is a auto compress text")
  20. }
  21. type GZipExample struct {
  22. GZip // add this for ask compress to GZip
  23. }
  24. func (GZipExample) Get() string {
  25. return fmt.Sprintf("This is a gzip compress text")
  26. }
  27. type DeflateExample struct {
  28. Deflate // add this for ask compress to Deflate, if not support then not compress
  29. }
  30. func (DeflateExample) Get() string {
  31. return fmt.Sprintf("This is a deflate compress text")
  32. }
  33. type NoCompress struct {
  34. }
  35. func (NoCompress) Get() string {
  36. return fmt.Sprintf("This is a non-compress text")
  37. }
  38. func TestCompressAuto(t *testing.T) {
  39. o := Classic()
  40. o.Get("/", new(CompressExample))
  41. testCompress(t, o, "http://localhost:8000/",
  42. "This is a auto compress text", "gzip")
  43. }
  44. func TestCompressGzip(t *testing.T) {
  45. o := Classic()
  46. o.Get("/", new(GZipExample))
  47. testCompress(t, o, "http://localhost:8000/",
  48. "This is a gzip compress text", "gzip")
  49. }
  50. func TestCompressDeflate(t *testing.T) {
  51. o := Classic()
  52. o.Get("/", new(DeflateExample))
  53. testCompress(t, o, "http://localhost:8000/",
  54. "This is a deflate compress text", "deflate")
  55. }
  56. func TestCompressNon(t *testing.T) {
  57. o := Classic()
  58. o.Get("/", new(NoCompress))
  59. testCompress(t, o, "http://localhost:8000/",
  60. "This is a non-compress text", "")
  61. }
  62. func TestCompressStatic(t *testing.T) {
  63. o := New()
  64. o.Use(Compresses([]string{".html"}))
  65. o.Use(ClassicHandlers...)
  66. testCompress(t, o, "http://localhost:8000/public/test.html",
  67. "hello tango", "gzip")
  68. }
  69. func testCompress(t *testing.T, o *Tango, url, content, enc string) {
  70. buff := bytes.NewBufferString("")
  71. recorder := httptest.NewRecorder()
  72. recorder.Body = buff
  73. req, err := http.NewRequest("GET", url, nil)
  74. if err != nil {
  75. t.Error(err)
  76. }
  77. req.Header.Add("Accept-Encoding", "gzip, deflate")
  78. o.ServeHTTP(recorder, req)
  79. expect(t, recorder.Code, http.StatusOK)
  80. refute(t, len(buff.String()), 0)
  81. ce := recorder.Header().Get("Content-Encoding")
  82. if ce == "gzip" {
  83. r, err := gzip.NewReader(buff)
  84. if err != nil {
  85. t.Error(err)
  86. }
  87. defer r.Close()
  88. bs, err := ioutil.ReadAll(r)
  89. if err != nil {
  90. t.Error(err)
  91. }
  92. expect(t, string(bs), content)
  93. } else if ce == "deflate" {
  94. r := flate.NewReader(buff)
  95. defer r.Close()
  96. bs, err := ioutil.ReadAll(r)
  97. if err != nil {
  98. t.Error(err)
  99. }
  100. expect(t, string(bs), content)
  101. } else {
  102. expect(t, buff.String(), content)
  103. }
  104. expect(t, enc, ce)
  105. }