assets_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright (C) 2020 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package assets
  7. import (
  8. "bytes"
  9. "compress/gzip"
  10. "io"
  11. "io/ioutil"
  12. "net/http"
  13. "net/http/httptest"
  14. "strconv"
  15. "strings"
  16. "testing"
  17. "time"
  18. )
  19. func compress(s string) string {
  20. var sb strings.Builder
  21. gz := gzip.NewWriter(&sb)
  22. io.WriteString(gz, s)
  23. gz.Close()
  24. return sb.String()
  25. }
  26. func decompress(p []byte) (out []byte) {
  27. r, err := gzip.NewReader(bytes.NewBuffer(p))
  28. if err == nil {
  29. out, err = ioutil.ReadAll(r)
  30. }
  31. if err != nil {
  32. panic(err)
  33. }
  34. return out
  35. }
  36. func TestServe(t *testing.T) { testServe(t, false) }
  37. func TestServeGzip(t *testing.T) { testServe(t, true) }
  38. func testServe(t *testing.T, gzip bool) {
  39. const indexHTML = `<html>Hello, world!</html>`
  40. content := indexHTML
  41. if gzip {
  42. content = compress(indexHTML)
  43. }
  44. handler := func(w http.ResponseWriter, r *http.Request) {
  45. Serve(w, r, Asset{
  46. Content: content,
  47. Gzipped: gzip,
  48. Length: len(indexHTML),
  49. Filename: r.URL.Path[1:],
  50. Modified: time.Unix(0, 0),
  51. })
  52. }
  53. for _, acceptGzip := range []bool{true, false} {
  54. r := httptest.NewRequest("GET", "http://localhost/index.html", nil)
  55. if acceptGzip {
  56. r.Header.Set("accept-encoding", "gzip, deflate")
  57. }
  58. w := httptest.NewRecorder()
  59. handler(w, r)
  60. res := w.Result()
  61. if res.StatusCode != http.StatusOK {
  62. t.Fatalf("wanted OK, got status %d", res.StatusCode)
  63. }
  64. if ctype := res.Header.Get("Content-Type"); ctype != "text/html; charset=utf-8" {
  65. t.Errorf("unexpected Content-Type %q", ctype)
  66. }
  67. // ETags must be quoted ASCII strings:
  68. // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag
  69. if etag := res.Header.Get("ETag"); etag != `"0"` {
  70. t.Errorf("unexpected ETag %q", etag)
  71. }
  72. body, _ := ioutil.ReadAll(res.Body)
  73. // Content-Length is the number of bytes in the encoded (compressed) body
  74. // (https://stackoverflow.com/a/3819303).
  75. n, err := strconv.Atoi(res.Header.Get("Content-Length"))
  76. if err != nil {
  77. t.Errorf("malformed Content-Length %q", res.Header.Get("Content-Length"))
  78. } else if n != len(body) {
  79. t.Errorf("wrong Content-Length %d, should be %d", n, len(body))
  80. }
  81. if gzip && acceptGzip {
  82. body = decompress(body)
  83. }
  84. if string(body) != indexHTML {
  85. t.Fatalf("unexpected content %q", body)
  86. }
  87. }
  88. r := httptest.NewRequest("GET", "http://localhost/index.html", nil)
  89. r.Header.Set("if-none-match", `"0"`)
  90. w := httptest.NewRecorder()
  91. handler(w, r)
  92. res := w.Result()
  93. if res.StatusCode != http.StatusNotModified {
  94. t.Fatalf("wanted NotModified, got status %d", res.StatusCode)
  95. }
  96. r = httptest.NewRequest("GET", "http://localhost/index.html", nil)
  97. r.Header.Set("if-modified-since", time.Now().Format(http.TimeFormat))
  98. w = httptest.NewRecorder()
  99. handler(w, r)
  100. res = w.Result()
  101. if res.StatusCode != http.StatusNotModified {
  102. t.Fatalf("wanted NotModified, got status %d", res.StatusCode)
  103. }
  104. }