-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain_test.go
More file actions
52 lines (49 loc) · 1.27 KB
/
Copy pathmain_test.go
File metadata and controls
52 lines (49 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"bytes"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestLazyRendering(t *testing.T) {
srv := httptest.NewServer(&mdHandler{dir: "testdata"})
defer srv.Close()
logBuf := new(bytes.Buffer)
log.SetOutput(logBuf)
r, err := http.Get(srv.URL + "/hello.md")
if err != nil {
t.Fatal(err)
}
if r.StatusCode != http.StatusOK {
t.Fatalf("invalid status on first call, want 200, got: %q", r.Status)
}
if _, err := io.Copy(ioutil.Discard, r.Body); err != nil {
t.Fatal(err)
}
lastmod := r.Header.Get("Last-Modified")
if lastmod == "" {
t.Fatalf("no or empty Last-Modified header; response headers are:\n%v", r.Header)
}
r.Body.Close()
req, err := http.NewRequest(http.MethodGet, srv.URL+"/hello.md", nil)
if err != nil {
t.Fatal(err)
}
req.Header.Set("If-Modified-Since", lastmod)
r, err = http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
if r.StatusCode != http.StatusNotModified {
t.Fatalf("unexpected status on second call, want 304, got: %q", r.Status)
}
defer r.Body.Close()
if cnt := strings.Count(logBuf.String(), "lazyReadSeeker init()"); cnt != 1 {
t.Fatalf("want 1 logged lazyReadSeeker init call, got %d; full log:\n%s", cnt, logBuf.String())
}
}
func init() { testRun = true }