compress_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package restful
  2. import (
  3. "bytes"
  4. "compress/gzip"
  5. "compress/zlib"
  6. "io"
  7. "io/ioutil"
  8. "net/http"
  9. "net/http/httptest"
  10. "testing"
  11. )
  12. // go test -v -test.run TestGzip ...restful
  13. func TestGzip(t *testing.T) {
  14. EnableContentEncoding = true
  15. httpRequest, _ := http.NewRequest("GET", "/test", nil)
  16. httpRequest.Header.Set("Accept-Encoding", "gzip,deflate")
  17. httpWriter := httptest.NewRecorder()
  18. wanted, encoding := wantsCompressedResponse(httpRequest)
  19. if !wanted {
  20. t.Fatal("should accept gzip")
  21. }
  22. if encoding != "gzip" {
  23. t.Fatal("expected gzip")
  24. }
  25. c, err := NewCompressingResponseWriter(httpWriter, encoding)
  26. if err != nil {
  27. t.Fatal(err.Error())
  28. }
  29. c.Write([]byte("Hello World"))
  30. c.Close()
  31. if httpWriter.Header().Get("Content-Encoding") != "gzip" {
  32. t.Fatal("Missing gzip header")
  33. }
  34. reader, err := gzip.NewReader(httpWriter.Body)
  35. if err != nil {
  36. t.Fatal(err.Error())
  37. }
  38. data, err := ioutil.ReadAll(reader)
  39. if err != nil {
  40. t.Fatal(err.Error())
  41. }
  42. if got, want := string(data), "Hello World"; got != want {
  43. t.Errorf("got %v want %v", got, want)
  44. }
  45. }
  46. func TestDeflate(t *testing.T) {
  47. EnableContentEncoding = true
  48. httpRequest, _ := http.NewRequest("GET", "/test", nil)
  49. httpRequest.Header.Set("Accept-Encoding", "deflate,gzip")
  50. httpWriter := httptest.NewRecorder()
  51. wanted, encoding := wantsCompressedResponse(httpRequest)
  52. if !wanted {
  53. t.Fatal("should accept deflate")
  54. }
  55. if encoding != "deflate" {
  56. t.Fatal("expected deflate")
  57. }
  58. c, err := NewCompressingResponseWriter(httpWriter, encoding)
  59. if err != nil {
  60. t.Fatal(err.Error())
  61. }
  62. c.Write([]byte("Hello World"))
  63. c.Close()
  64. if httpWriter.Header().Get("Content-Encoding") != "deflate" {
  65. t.Fatal("Missing deflate header")
  66. }
  67. reader, err := zlib.NewReader(httpWriter.Body)
  68. if err != nil {
  69. t.Fatal(err.Error())
  70. }
  71. data, err := ioutil.ReadAll(reader)
  72. if err != nil {
  73. t.Fatal(err.Error())
  74. }
  75. if got, want := string(data), "Hello World"; got != want {
  76. t.Errorf("got %v want %v", got, want)
  77. }
  78. }
  79. func TestGzipDecompressRequestBody(t *testing.T) {
  80. b := new(bytes.Buffer)
  81. w := newGzipWriter()
  82. w.Reset(b)
  83. io.WriteString(w, `{"msg":"hi"}`)
  84. w.Flush()
  85. w.Close()
  86. req := new(Request)
  87. httpRequest, _ := http.NewRequest("GET", "/", bytes.NewReader(b.Bytes()))
  88. httpRequest.Header.Set("Content-Type", "application/json")
  89. httpRequest.Header.Set("Content-Encoding", "gzip")
  90. req.Request = httpRequest
  91. doCacheReadEntityBytes = false
  92. doc := make(map[string]interface{})
  93. req.ReadEntity(&doc)
  94. if got, want := doc["msg"], "hi"; got != want {
  95. t.Errorf("got %v want %v", got, want)
  96. }
  97. }
  98. func TestZlibDecompressRequestBody(t *testing.T) {
  99. b := new(bytes.Buffer)
  100. w := newZlibWriter()
  101. w.Reset(b)
  102. io.WriteString(w, `{"msg":"hi"}`)
  103. w.Flush()
  104. w.Close()
  105. req := new(Request)
  106. httpRequest, _ := http.NewRequest("GET", "/", bytes.NewReader(b.Bytes()))
  107. httpRequest.Header.Set("Content-Type", "application/json")
  108. httpRequest.Header.Set("Content-Encoding", "deflate")
  109. req.Request = httpRequest
  110. doCacheReadEntityBytes = false
  111. doc := make(map[string]interface{})
  112. req.ReadEntity(&doc)
  113. if got, want := doc["msg"], "hi"; got != want {
  114. t.Errorf("got %v want %v", got, want)
  115. }
  116. }