marmotte is a modern gopher server.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
marmotte/gopher/files_test.go

143 lines
3.7 KiB

package gopher
import (
"fmt"
"os"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestFileType(t *testing.T) {
d, _ := os.Getwd()
c := Context { Root: d + "/../testdata" }
files := map[string]ResponseType {c.Root + "/attic/Goblin_Market": PlainText,
c.Root + "/attic/pictures/starry_night.jpg": Image,
c.Root + "/attic/pictures/hamtori.gif": Image,
c.Root + "/attic/audio/Bubbles-SoundBible.com-810959520.mp3": Audio,
c.Root + "/": Directory,
}
for f, expected := range files {
got, err := FileType(f)
if err != nil {
t.Errorf("Error determining the file type of %s: %s", f, err)
}
if got != expected {
t.Errorf("Unexpected file type for %s: expected %d, got %d", f, int(expected), int(got))
}
}
p := "/attic/non-existent"
_, err := FileType(p)
if err == nil {
t.Errorf("Expected an error when calling FileType with a non-existent path %s, didn'r receive any", p)
}
}
func TestGopherMap(t *testing.T) {
d, _ := os.Getwd()
c := Context { Root: d + "/../testdata" }
lines, err := GopherMap(c, "")
expected := true
got := len(lines) > 0
if err != nil {
t.Errorf("Unexpected error when calling GopherMap with an empty path: %s", err)
}
if got != expected {
t.Error("Unexpected empty lines when calling GopherMap with an empty path")
}
p := "/some/non/existant/path"
lines, err = GopherMap(c, p)
expectedlines := 0
gotlines := len(lines)
if err == nil {
t.Errorf("Expected receiving an error when calling GopherMap with an invalid path: %s, err: %s", p, err)
}
if got != expected {
t.Errorf("Unexpected non-empty lines when calling GopherMap with an empty path, expected %d, got %d", expectedlines, gotlines)
}
}
func TestGopherMapFixLine(t *testing.T) {
d, _ := os.Getwd()
c := Context { Host: "myhost", Port: 7777, Root: d + "/../testdata" }
l := "An info line with no tab\r\n"
expected := "i" + strings.TrimRight(l, "\r\n") + "\tfake.host\t1\r\n"
got := GopherMapFixLine(c, l)
if got != expected {
t.Errorf("Unexpected info line for a normal info line: line: %s, expected: %s, got: %s", l, expected, got)
}
l = "An info line\twith\ttabs\r\n"
expected = l
got = GopherMapFixLine(c, l)
if got != expected {
t.Errorf("Unexpected info line for an info line with tabs: line: %s, expected: %s, got: %s", l, expected, got)
}
l = "0A menu line with no server and no port\tfile.txt\r\n"
expected = strings.TrimRight(l, "\r\n") + "\t" + c.Host + "\t" + fmt.Sprintf("%d", c.Port) + "\r\n"
got = GopherMapFixLine(c, l)
if got != expected {
t.Errorf("Unexpected menu line for a menu line with no host or port: line: %s, expected: %s, got: %s", l, expected, got)
}
l = "0A menu line with a server and a port\tfile.txt\tsomeserver.com\t70\r\n"
expected = l
got = GopherMapFixLine(c, l)
if got != expected {
t.Errorf("Unexpected menu line for a menu line with a host and a port: line: %s, expected: %s, got: %s", l, expected, got)
}
}
func TestGopherMapFixLines(t *testing.T) {
d, _ := os.Getwd()
c := Context { Host: "myhost", Port: 7777, Root: d + "/../testdata" }
l1 := "An info line with no tab\r\n"
expected1 := "i" + strings.TrimRight(l1, "\r\n") + "\tfake.host\t1\r\n"
l2 := "An info line\twith\ttabs\r\n"
expected2 := l2
l3 := "0A menu line with no server and no port\tfile.txt\r\n"
expected3 := strings.TrimRight(l3, "\r\n") + "\t" + c.Host + "\t" + fmt.Sprintf("%d", c.Port) + "\r\n"
l4 := "0A menu line with a server and a port\tfile.txt\tsomeserver.com\t70\r\n"
expected4 := l4
got := GopherMapFixLines(c, []string{ l1, l2, l3, l4})
expected := []string { expected1, expected2, expected3, expected4 }
if ! cmp.Equal(expected, got) {
t.Errorf("Unexpected info lines from GopherMapFixLines: %s", cmp.Diff(expected, got))
}
}