compress_test.go 2.5 KB

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