| 587 | } |
| 588 | |
| 589 | func TestDirectoryIfNotModified(t *testing.T) { |
| 590 | defer afterTest(t) |
| 591 | const indexContents = "I am a fake index.html file" |
| 592 | fileMod := time.Unix(1000000000, 0).UTC() |
| 593 | fileModStr := fileMod.Format(http.TimeFormat) |
| 594 | dirMod := time.Unix(123, 0).UTC() |
| 595 | indexFile := &fakeFileInfo{ |
| 596 | basename: "index.html", |
| 597 | modtime: fileMod, |
| 598 | contents: indexContents, |
| 599 | } |
| 600 | fsys := fakeFS{ |
| 601 | "/": &fakeFileInfo{ |
| 602 | dir: true, |
| 603 | modtime: dirMod, |
| 604 | ents: []*fakeFileInfo{indexFile}, |
| 605 | }, |
| 606 | "/index.html": indexFile, |
| 607 | } |
| 608 | |
| 609 | fs := &FileServer{ |
| 610 | "version", |
| 611 | fsys, |
| 612 | inject.CopyInject{}, |
| 613 | ricetemp.MustMakeTemplates(rice.MustFindBox("../templates")), |
| 614 | []routespec.RouteSpec{}, |
| 615 | "", |
| 616 | } |
| 617 | |
| 618 | ts := httptest.NewServer(fs) |
| 619 | defer ts.Close() |
| 620 | |
| 621 | res, err := http.Get(ts.URL) |
| 622 | if err != nil { |
| 623 | t.Fatal(err) |
| 624 | } |
| 625 | b, err := ioutil.ReadAll(res.Body) |
| 626 | if err != nil { |
| 627 | t.Fatal(err) |
| 628 | } |
| 629 | if string(b) != indexContents { |
| 630 | t.Fatalf("Got body %q; want %q", b, indexContents) |
| 631 | } |
| 632 | _ = res.Body.Close() |
| 633 | |
| 634 | lastMod := res.Header.Get("Last-Modified") |
| 635 | if lastMod != fileModStr { |
| 636 | t.Fatalf("initial Last-Modified = %q; want %q", lastMod, fileModStr) |
| 637 | } |
| 638 | |
| 639 | req, _ := http.NewRequest("GET", ts.URL, nil) |
| 640 | req.Header.Set("If-Modified-Since", lastMod) |
| 641 | |
| 642 | res, err = http.DefaultClient.Do(req) |
| 643 | if err != nil { |
| 644 | t.Fatal(err) |
| 645 | } |
| 646 | if res.StatusCode != 304 { |