package e2e import ( "net/http" "os/exec" "strings" "testing" ) // Chapter 42.4: binary content is not rendered, and a file over a megabyte is not either. func TestTheFileViewRefusesToRenderWhatItShouldNot(t *testing.T) { if _, err := exec.LookPath("git"); err != nil { t.Skip("git is not installed") } in := newInstance(t) john := in.account("john") work := seed(t, in, john, "john", "johnbot") write(t, work, "logo.bin", "\x00\x01\x02binary\x00bytes\n") write(t, work, "huge.txt", strings.Repeat("a line of a very large file\n", 40000)) write(t, work, "small.txt", "one line\n") run(t, work, "git", "add", "-A") run(t, work, "git", "commit", "-qm", "add the awkward ones") run(t, work, "git", "push", "-q", in.url(john, "/john/johnbot"), "master") cases := []struct { path string want []string not []string }{ // A null byte in the first 8000 marks it binary, and the page offers the bytes instead. {"/john/johnbot/file/master/logo.bin", []string{"binary file", "download"}, []string{"binary\x00bytes"}}, // Over a megabyte the page says so rather than sending it, per the same section. {"/john/johnbot/file/master/huge.txt", []string{"too large to render", "download"}, []string{"a line of a very large file"}}, // And an ordinary file still renders, or the two rules above have eaten everything. {"/john/johnbot/file/master/small.txt", []string{"one line"}, []string{"too large", "binary file"}}, } for _, c := range cases { code, _, body := get(t, in.http.URL+c.path) if code != http.StatusOK { t.Errorf("%s answered %d", c.path, code) continue } for _, want := range c.want { if !strings.Contains(body, want) { t.Errorf("%s does not say %q", c.path, want) } } for _, not := range c.not { if strings.Contains(body, not) { t.Errorf("%s put %q on the page", c.path, not) } } } // The download itself is an attachment and never a page, whatever the bytes are. 42.3. code, _, _ := get(t, in.http.URL+"/john/johnbot/raw/master/logo.bin") if code != http.StatusOK { t.Errorf("the raw bytes answered %d", code) } }