Skip to content

Commit

Permalink
Check for EOF in DetectReader; for gabriel-vasile#162
Browse files Browse the repository at this point in the history
Before v1.2.0, DetectReader returned text/plain for empty inputs.
After v1.2.0, it started returning EOF error.
This commit changes DetectReader behavior to how it was before v1.2.0
  • Loading branch information
gabriel-vasile committed Jul 1, 2021
1 parent 422cc9c commit 04bcd30
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mimetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func DetectReader(r io.Reader) (*MIME, error) {
n := 0
in = make([]byte, readLimit)
n, err = io.ReadFull(r, in)
if err != nil && err != io.ErrUnexpectedEOF {
if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
return root, err
}
in = in[:n]
Expand Down
16 changes: 16 additions & 0 deletions mimetype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,22 @@ func TestConcurrent(t *testing.T) {
mimetype.SetLimit(3072)
}

// For #162.
func TestEmptyInput(t *testing.T) {
mtype, err := mimetype.DetectReader(bytes.NewReader(nil))
if err != nil {
t.Fatalf("empty reader err; expected: nil, got: %s", err)
}
plain := "text/plain"
if !mtype.Is(plain) {
t.Fatalf("empty reader detection; expected: %s, got: %s", plain, mtype)
}
mtype = mimetype.Detect(nil)
if !mtype.Is(plain) {
t.Fatalf("empty bytes slice detection; expected: %s, got: %s", plain, mtype)
}
}

// Benchmarking a random slice of bytes is as close as possible to the real
// world usage. A random byte slice is almost guaranteed to fail being detected.
//
Expand Down

0 comments on commit 04bcd30

Please sign in to comment.