選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

gzip_test.go 4.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package handlers
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "testing"
  6. )
  7. func TestGzipped(t *testing.T) {
  8. body := "This is the body"
  9. headers := []string{"gzip", "*", "gzip, deflate, sdch"}
  10. h := GZIPHandler(http.HandlerFunc(
  11. func(w http.ResponseWriter, r *http.Request) {
  12. w.Header().Set("Content-Type", "text/plain")
  13. _, err := w.Write([]byte(body))
  14. if err != nil {
  15. panic(err)
  16. }
  17. }), nil)
  18. s := httptest.NewServer(h)
  19. defer s.Close()
  20. for _, hdr := range headers {
  21. t.Logf("running with Accept-Encoding header %s", hdr)
  22. req, err := http.NewRequest("GET", s.URL, nil)
  23. if err != nil {
  24. panic(err)
  25. }
  26. req.Header.Set("Accept-Encoding", hdr)
  27. res, err := http.DefaultClient.Do(req)
  28. if err != nil {
  29. panic(err)
  30. }
  31. assertStatus(http.StatusOK, res.StatusCode, t)
  32. assertHeader("Content-Encoding", "gzip", res, t)
  33. assertGzippedBody([]byte(body), res, t)
  34. }
  35. }
  36. func TestNoGzip(t *testing.T) {
  37. body := "This is the body"
  38. h := GZIPHandler(http.HandlerFunc(
  39. func(w http.ResponseWriter, r *http.Request) {
  40. w.Header().Set("Content-Type", "text/plain")
  41. _, err := w.Write([]byte(body))
  42. if err != nil {
  43. panic(err)
  44. }
  45. }), nil)
  46. s := httptest.NewServer(h)
  47. defer s.Close()
  48. req, err := http.NewRequest("GET", s.URL, nil)
  49. if err != nil {
  50. panic(err)
  51. }
  52. res, err := http.DefaultClient.Do(req)
  53. if err != nil {
  54. panic(err)
  55. }
  56. assertStatus(http.StatusOK, res.StatusCode, t)
  57. assertHeader("Content-Encoding", "", res, t)
  58. assertBody([]byte(body), res, t)
  59. }
  60. func TestGzipOuterPanic(t *testing.T) {
  61. msg := "ko"
  62. h := PanicHandler(
  63. GZIPHandler(http.HandlerFunc(
  64. func(w http.ResponseWriter, r *http.Request) {
  65. panic(msg)
  66. }), nil), nil)
  67. s := httptest.NewServer(h)
  68. defer s.Close()
  69. req, err := http.NewRequest("GET", s.URL, nil)
  70. if err != nil {
  71. panic(err)
  72. }
  73. res, err := http.DefaultClient.Do(req)
  74. if err != nil {
  75. panic(err)
  76. }
  77. assertStatus(http.StatusInternalServerError, res.StatusCode, t)
  78. assertHeader("Content-Encoding", "", res, t)
  79. assertBody([]byte(msg+"\n"), res, t)
  80. }
  81. func TestNoGzipOnFilter(t *testing.T) {
  82. body := "This is the body"
  83. h := GZIPHandler(http.HandlerFunc(
  84. func(w http.ResponseWriter, r *http.Request) {
  85. w.Header().Set("Content-Type", "x/x")
  86. _, err := w.Write([]byte(body))
  87. if err != nil {
  88. panic(err)
  89. }
  90. }), nil)
  91. s := httptest.NewServer(h)
  92. defer s.Close()
  93. req, err := http.NewRequest("GET", s.URL, nil)
  94. if err != nil {
  95. panic(err)
  96. }
  97. req.Header.Set("Accept-Encoding", "gzip")
  98. res, err := http.DefaultClient.Do(req)
  99. if err != nil {
  100. panic(err)
  101. }
  102. assertStatus(http.StatusOK, res.StatusCode, t)
  103. assertHeader("Content-Encoding", "", res, t)
  104. assertBody([]byte(body), res, t)
  105. }
  106. func TestNoGzipOnCustomFilter(t *testing.T) {
  107. body := "This is the body"
  108. h := GZIPHandler(http.HandlerFunc(
  109. func(w http.ResponseWriter, r *http.Request) {
  110. w.Header().Set("Content-Type", "text/plain")
  111. _, err := w.Write([]byte(body))
  112. if err != nil {
  113. panic(err)
  114. }
  115. }), func(w http.ResponseWriter, r *http.Request) bool {
  116. return false
  117. })
  118. s := httptest.NewServer(h)
  119. defer s.Close()
  120. req, err := http.NewRequest("GET", s.URL, nil)
  121. if err != nil {
  122. panic(err)
  123. }
  124. req.Header.Set("Accept-Encoding", "gzip")
  125. res, err := http.DefaultClient.Do(req)
  126. if err != nil {
  127. panic(err)
  128. }
  129. assertStatus(http.StatusOK, res.StatusCode, t)
  130. assertHeader("Content-Encoding", "", res, t)
  131. assertBody([]byte(body), res, t)
  132. }
  133. func TestGzipOnCustomFilter(t *testing.T) {
  134. body := "This is the body"
  135. h := GZIPHandler(http.HandlerFunc(
  136. func(w http.ResponseWriter, r *http.Request) {
  137. w.Header().Set("Content-Type", "x/x")
  138. _, err := w.Write([]byte(body))
  139. if err != nil {
  140. panic(err)
  141. }
  142. }), func(w http.ResponseWriter, r *http.Request) bool {
  143. return true
  144. })
  145. s := httptest.NewServer(h)
  146. defer s.Close()
  147. req, err := http.NewRequest("GET", s.URL, nil)
  148. if err != nil {
  149. panic(err)
  150. }
  151. req.Header.Set("Accept-Encoding", "gzip")
  152. res, err := http.DefaultClient.Do(req)
  153. if err != nil {
  154. panic(err)
  155. }
  156. assertStatus(http.StatusOK, res.StatusCode, t)
  157. assertHeader("Content-Encoding", "gzip", res, t)
  158. assertGzippedBody([]byte(body), res, t)
  159. }