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