Skip to content

Commit

Permalink
refactor: add unit test for MP3 serving
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h committed Mar 15, 2021
1 parent 1820437 commit 9f47d40
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package gemini

import (
"context"
"crypto/sha256"
"encoding/hex"
"io"
"io/ioutil"
"net/url"
"testing"
Expand Down Expand Up @@ -104,3 +107,56 @@ func TestFileSystemHandler(t *testing.T) {
})
}
}

func TestFileSystemBinaryHandling(t *testing.T) {
var tests = []struct {
name string
url string
expectedHeader Header
expectedHash string
}{
{
name: "mp3 files are served as expected",
url: "cordova.mp3",
expectedHeader: Header{
Code: CodeSuccess,
Meta: "audio/mpeg",
},
expectedHash: "9f3bd09333627c218b3b0af371d9c7f4c6ac6a8b1c60b9e9aba0ca68c602e511",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
dir := Dir("tests")
h := FileSystemHandler(dir)
u, err := url.Parse(tt.url)
if err != nil {
t.Fatalf("failed to parse URL %q", tt.url)
}
r := &Request{
Context: context.Background(),
URL: u,
}
resp, err := Record(r, h)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if tt.expectedHeader.Code != resp.Header.Code {
t.Errorf("expected header code %v, got %v", tt.expectedHeader.Code, resp.Header.Code)
}
if tt.expectedHeader.Meta != resp.Header.Meta {
t.Errorf("expected header meta %q, got %q", tt.expectedHeader.Meta, resp.Header.Meta)
}
hash := sha256.New()
_, err = io.Copy(hash, resp.Body)
if err != nil {
t.Fatalf("unexpected error reading body: %v", err)
}
actualHash := hex.EncodeToString(hash.Sum(nil))
if tt.expectedHash != actualHash {
t.Errorf("expected\n%v\nactual\n%v", tt.expectedHash, string(actualHash))
}
})
}
}
Binary file added tests/cordova.mp3
Binary file not shown.

0 comments on commit 9f47d40

Please sign in to comment.