assets_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. "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 = ioutil.ReadAll(r)
  29. }
  30. if err != nil {
  31. panic(err)
  32. }
  33. return out
  34. }
  35. func TestServe(t *testing.T) {
  36. indexHTML := `<html>Hello, world!</html>`
  37. indexGz := compress(indexHTML)
  38. handler := func(w http.ResponseWriter, r *http.Request) {
  39. Serve(w, r, Asset{
  40. ContentGz: indexGz,
  41. Filename: r.URL.Path[1:],
  42. Modified: time.Unix(0, 0),
  43. })
  44. }
  45. for _, acceptGzip := range []bool{true, false} {
  46. r := httptest.NewRequest("GET", "http://localhost/index.html", nil)
  47. if acceptGzip {
  48. r.Header.Set("accept-encoding", "gzip, deflate")
  49. }
  50. w := httptest.NewRecorder()
  51. handler(w, r)
  52. res := w.Result()
  53. if res.StatusCode != http.StatusOK {
  54. t.Fatalf("wanted OK, got status %d", res.StatusCode)
  55. }
  56. if ctype := res.Header.Get("Content-Type"); ctype != "text/html; charset=utf-8" {
  57. t.Errorf("unexpected Content-Type %q", ctype)
  58. }
  59. // ETags must be quoted ASCII strings:
  60. // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag
  61. if etag := res.Header.Get("ETag"); etag != `"0"` {
  62. t.Errorf("unexpected ETag %q", etag)
  63. }
  64. body, _ := ioutil.ReadAll(res.Body)
  65. if acceptGzip {
  66. body = decompress(body)
  67. }
  68. if string(body) != indexHTML {
  69. t.Fatalf("unexpected content %q", body)
  70. }
  71. }
  72. r := httptest.NewRequest("GET", "http://localhost/index.html", nil)
  73. r.Header.Set("if-none-match", `"0"`)
  74. w := httptest.NewRecorder()
  75. handler(w, r)
  76. res := w.Result()
  77. if res.StatusCode != http.StatusNotModified {
  78. t.Fatalf("wanted NotModified, got status %d", res.StatusCode)
  79. }
  80. r = httptest.NewRequest("GET", "http://localhost/index.html", nil)
  81. r.Header.Set("if-modified-since", time.Now().Format(http.TimeFormat))
  82. w = httptest.NewRecorder()
  83. handler(w, r)
  84. res = w.Result()
  85. if res.StatusCode != http.StatusNotModified {
  86. t.Fatalf("wanted NotModified, got status %d", res.StatusCode)
  87. }
  88. }