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.
319 lines
9.7 KiB
319 lines
9.7 KiB
package gopher
|
|
|
|
import (
|
|
"testing"
|
|
"os"
|
|
"path/filepath"
|
|
"fmt"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
)
|
|
|
|
func TestGopherMapTransformer(t *testing.T) {
|
|
d, _ := os.Getwd()
|
|
c := Context { Root: d + "/../testdata" }
|
|
p:= "/attic/"
|
|
rq, _ := NewRequest(c, p)
|
|
rs := NewResponse(c, *rq)
|
|
|
|
var err error = nil
|
|
GopherMapTransformer(&c, *rq, rs, err)
|
|
|
|
expected := true
|
|
got := len(rs.ContentText) > 0
|
|
|
|
if got != expected {
|
|
t.Errorf("Unexpected empty ContentText in Response instance after a valid GopherMapTransformer call on path %s", p)
|
|
}
|
|
|
|
expected = false
|
|
got = len(rs.ContentBinary) > 0
|
|
|
|
if got != expected {
|
|
t.Errorf("Unexpected non-empty ContentBinary in Response instance after a valid GopherMapTransformer call on path %s", p)
|
|
}
|
|
|
|
p = "/attic/non/existent/path"
|
|
rq, _ = NewRequest(c, "")
|
|
// craft an invalid request
|
|
rq.FullPath = c.Root + p
|
|
rq.Path = p
|
|
rs = NewResponse(c, *rq)
|
|
|
|
err = nil
|
|
_, _, _, err = GopherMapTransformer(&c, *rq, rs, err)
|
|
|
|
expectedlines := 0
|
|
gotlines := len(rs.ContentText)
|
|
|
|
if err == nil {
|
|
t.Errorf("Expected receiving an error when calling GopherMapTransformer with a non-existent directory %s", p)
|
|
}
|
|
|
|
if gotlines != expectedlines {
|
|
t.Errorf("Unexpected number of lines when calling GopherMapTransformer with a non-existent directory %s, expected %d, got %d", p, expectedlines, gotlines)
|
|
}
|
|
|
|
}
|
|
|
|
func TestTextFileTransformer(t *testing.T) {
|
|
d, _ := os.Getwd()
|
|
c := Context { Root: d + "/../testdata" }
|
|
p:= "/attic/Goblin_Market"
|
|
rq, _ := NewRequest(c, p)
|
|
rs := NewResponse(c, *rq)
|
|
|
|
var err error = nil
|
|
TextFileTransformer(&c, *rq, rs, err)
|
|
|
|
expected := true
|
|
got := len(rs.ContentText) > 0
|
|
|
|
if got != expected {
|
|
t.Errorf("Unexpected empty ContentText in Response instance after a valid TextFileTransformer call on path %s", p)
|
|
}
|
|
|
|
expected = false
|
|
got = len(rs.ContentBinary) > 0
|
|
|
|
if got != expected {
|
|
t.Errorf("Unexpected non-empty ContentBinary in Response instance after a valid TextFileTransformer call on path %s", p)
|
|
}
|
|
|
|
p = "/attic/Goblin_Market"
|
|
rq, _ = NewRequest(c, p)
|
|
rs = NewResponse(c, *rq)
|
|
|
|
// craft a broken request
|
|
rq.FullPath = c.Root + "/non-existent"
|
|
rq.Path = "/non-existent"
|
|
err = nil
|
|
|
|
expectedlines := len(rs.ContentText)
|
|
_, _, _, err = TextFileTransformer(&c, *rq, rs, err)
|
|
|
|
gotlines := len(rs.ContentText)
|
|
|
|
if err == nil {
|
|
t.Errorf("Expected receiving an error after calling TextFileTransformer with a broken path %s in the request, didn't receive any", p)
|
|
}
|
|
|
|
if got != expected {
|
|
t.Errorf("Unexpected number of lines for ContentText in Response instance after an invalid TextFileTransformer call on path %s, expected %d, got %d", p, expectedlines, gotlines)
|
|
}
|
|
|
|
}
|
|
|
|
func TestBinaryFileTransformer(t *testing.T) {
|
|
d, _ := os.Getwd()
|
|
c := Context { Root: d + "/../testdata" }
|
|
p:= "/attic/cat"
|
|
rq, _ := NewRequest(c, p)
|
|
rs := NewResponse(c, *rq)
|
|
|
|
var err error = nil
|
|
BinaryFileTransformer(&c, *rq, rs, err)
|
|
|
|
expected := true
|
|
got := len(rs.ContentBinary) > 0
|
|
|
|
if got != expected {
|
|
t.Errorf("Unexpected empty ContentBinary in Response instance after a valid BinaryFileTransformer call on path %s", p)
|
|
}
|
|
|
|
expected = false
|
|
got = len(rs.ContentText) > 0
|
|
|
|
if got != expected {
|
|
t.Errorf("Unexpected non-empty ContentText in Response instance after a valid BinaryFileTransformer call on path %s", p)
|
|
}
|
|
|
|
p = "/attic/cat"
|
|
rq, _ = NewRequest(c, p)
|
|
rs = NewResponse(c, *rq)
|
|
|
|
// craft a broken request
|
|
rq.FullPath = c.Root + "/non-existent"
|
|
rq.Path = "/non-existent"
|
|
|
|
err = nil
|
|
expectedsize := len(rs.ContentBinary)
|
|
_, _, _, err = BinaryFileTransformer(&c, *rq, rs, err)
|
|
|
|
gotsize := len(rs.ContentBinary)
|
|
|
|
if err == nil {
|
|
t.Errorf("Expected an error after calling BinaryFileTransformer with a broken path %s in the request, didn't get any", p)
|
|
}
|
|
|
|
if gotsize != expectedsize {
|
|
t.Errorf("Unexpected size of ContentBinary in Response instance after an invalid BinaryFileTransformer call on path %s, expected %d, got %d", p, expectedsize, gotsize)
|
|
}
|
|
|
|
}
|
|
|
|
func TestHeaderTransformer(t *testing.T) {
|
|
d, _ := os.Getwd()
|
|
c := Context { Root: d + "/../testdata", Headers: map[string][]string{"default": {"powered", "by", "marmotte"}} }
|
|
k := "default"
|
|
c.ResponseTransformers = append(c.ResponseTransformers, ResponseTransformer{
|
|
Transformer: HeaderTransformerFor(k),
|
|
Predicate: GopherMapPredicate })
|
|
|
|
p:= "/attic/"
|
|
rq, _ := NewRequest(c, p)
|
|
rs := NewResponse(c, *rq)
|
|
|
|
var err error = nil
|
|
HeaderTransformerFor(k)(&c, *rq, rs, err)
|
|
|
|
for i, h := range c.Headers[k] {
|
|
expected := "i" + h + "\tfake.host\t1" + "\r\n"
|
|
got := rs.ContentText[i]
|
|
if got != expected {
|
|
t.Errorf("Unexpected line in ContentText of a Response instance, expected %s, got %s (index %d)", expected, got, i)
|
|
}
|
|
}
|
|
|
|
expected := false
|
|
got := len(rs.ContentBinary) > 0
|
|
|
|
if got != expected {
|
|
t.Errorf("Unexpected non-empty ContentBinary in Response instance after a valid HeaderTransformer call on path %s", p)
|
|
}
|
|
}
|
|
|
|
func TestFooterTransformer(t *testing.T) {
|
|
d, _ := os.Getwd()
|
|
c := Context { Root: d + "/../testdata", Footers: map[string][]string{"default": {"powered", "by", "marmotte"}} }
|
|
k := "default"
|
|
c.ResponseTransformers = append(c.ResponseTransformers, ResponseTransformer{
|
|
Transformer: FooterTransformerFor(k),
|
|
Predicate: GopherMapPredicate })
|
|
|
|
p:= "/attic/"
|
|
rq, _ := NewRequest(c, p)
|
|
rs := NewResponse(c, *rq)
|
|
|
|
var err error = nil
|
|
FooterTransformerFor(k)(&c, *rq, rs, err)
|
|
|
|
for i, h := range c.Footers[k] {
|
|
expected := "i" + h + "\tfake.host\t1" + "\r\n"
|
|
got := rs.ContentText[len(rs.ContentText) - len(c.Footers[k]) + i]
|
|
if got != expected {
|
|
t.Errorf("Unexpected line in ContentText of a Response instance, expected %s, got %s (index %d)", expected, got, i)
|
|
}
|
|
}
|
|
|
|
expected := false
|
|
got := len(rs.ContentBinary) > 0
|
|
|
|
if got != expected {
|
|
t.Errorf("Unexpected non-empty ContentBinary in Response instance after a valid HeaderTransformer call on path %s", p)
|
|
}
|
|
}
|
|
|
|
func TestSelectorRewriteTransformer(t *testing.T) {
|
|
d, _ := os.Getwd()
|
|
c := Context { Root: d + "/../testdata", Footers: map[string][]string{"default": {"powered", "by", "marmotte"}} }
|
|
rt := RequestTransformer{Transformer: SelectorRewriteTransformerFor("^/?~(.+)", "/users_gopherspace/$1"),
|
|
Predicate: FullPathMatchPredicateFor(".*"),
|
|
}
|
|
|
|
p := "~mcahill"
|
|
rq, err := NewRequest(c, p)
|
|
|
|
_, rq, _, err = rt.Transformer(&c, rq, Response{}, nil)
|
|
|
|
if err != nil {
|
|
t.Errorf("Unexpected error returned by NewRequest for a well-formed user space: %s, %s", p, err)
|
|
}
|
|
|
|
expectedRq := Request{ Path: "/users_gopherspace/mcahill", FullPath: filepath.Clean(c.Root + "/users_gopherspace/mcahill"), Type: Map}
|
|
|
|
if ! cmp.Equal(&expectedRq, rq) {
|
|
t.Errorf("Unexpected Request instance returned by NewRequest for a well-formed user space request %s: %s", p, cmp.Diff(&expectedRq, rq))
|
|
t.Errorf("The FullPath is %s", rq.FullPath)
|
|
}
|
|
}
|
|
|
|
func TestExecutableTransformer(t *testing.T) {
|
|
d, _ := os.Getwd()
|
|
c := Context { Root: d + "/../testdata", Footers: map[string][]string{"default": {"powered", "by", "marmotte"}}, ErrorRoot: d + "/../testdata/errors" }
|
|
p:= "/createtextfile.sh"
|
|
rq, _ := NewRequest(c, p)
|
|
_, newRq, rs, err := ExecutableTransformer(&c, *rq, &Response{}, nil)
|
|
if err != nil {
|
|
t.Errorf("Unexpected error when running an ExecutableTransformer for %s: %s", p, err)
|
|
}
|
|
rs = NewResponse(c, newRq)
|
|
|
|
expected := PlainText
|
|
got := rs.Type
|
|
|
|
if int(got) != expected {
|
|
t.Errorf("Unexpected Response type for a file created by an executable: %s, expected: %d, got: %d", newRq.FullPath, expected, got)
|
|
}
|
|
|
|
p = "/bin/ls"
|
|
rq, _ = NewRequest(c, p)
|
|
// overwrite rq.FullPath with an absolute path
|
|
rq.FullPath = "/bin/ls"
|
|
_, newRq, rs, err = ExecutableTransformer(&c, *rq, &Response{}, nil)
|
|
|
|
// This should redirect to an error handling function, which should not fail
|
|
if err != nil {
|
|
t.Errorf("Unexpected error received when running an ExecutableTransformer for %s: %s", p, err.Error())
|
|
}
|
|
|
|
// The selector should become not found
|
|
expectedCode := GopherErrorSelectorNotFound
|
|
gotCode := rs.ErrorCode
|
|
|
|
if gotCode != expectedCode {
|
|
t.Errorf("Failed to receive the expected error code in the response when running an ExecutableTransformer for %s, expected %s, got %s", p, expectedCode, gotCode)
|
|
}
|
|
|
|
}
|
|
|
|
func TestErrorRedirectTransformer(t *testing.T) {
|
|
d, _ := os.Getwd()
|
|
c := Context { Root: d + "/../testdata", Footers: map[string][]string{"default": {"powered", "by", "marmotte"}}, ErrorRoot: d + "/../testdata/errors-empty" }
|
|
p:= "/non-existent"
|
|
rq, _ := NewRequest(c, p)
|
|
// error in rendering pipeline, e.g. in data retrieval
|
|
rs := &Response{ErrorCode: GopherErrorDataRetrieval, ContentText: []string{"Some text retrieved but..."}}
|
|
|
|
_, _, rs, err := ErrorRedirectTransformer(&c, *rq, rs, nil)
|
|
|
|
if err == nil {
|
|
t.Errorf("Failed to receive an error when running an ErrorRedirectTransformer with a missing error template for %s: %s", p, err.Error())
|
|
}
|
|
|
|
expected := []string {fmt.Sprint("Error retrieving the error page for the request. The original error code was ", rs.ErrorCode, " for the selector ", rq.Path)}
|
|
|
|
if ! cmp.Equal(expected, rs.ContentText) {
|
|
t.Errorf("Received unexpected ContentText is Reponse after an ErrorRedirectTransformer with a missing error template for %s: %s", p, cmp.Diff(expected, rs.ContentText))
|
|
}
|
|
|
|
}
|
|
|
|
func TestDynamicRenderingTransformer(t *testing.T) {
|
|
d, _ := os.Getwd()
|
|
c := Context { Root: d + "/../testdata", Footers: map[string][]string{"default": {"powered", "by", "marmotte"}}, ErrorRoot: d + "/../testdata/errors" }
|
|
p:= "/attic/hello.gt"
|
|
rq, _ := NewRequest(c, p)
|
|
|
|
rs := NewResponse(c, *rq)
|
|
|
|
_, _, rs, err := DynamicRenderingTransformer(&c, *rq, rs, nil)
|
|
|
|
if err != nil {
|
|
t.Errorf("Unexpected error received when running a DynamicRenderingTransformer for a valid template %s: %s", p, err.Error())
|
|
}
|
|
|
|
if len(rs.ContentText) == 0 {
|
|
t.Errorf("Unexpected empty ContentText in Response when running a DynamicRenderingTransformer for a valid template %s", p)
|
|
}
|
|
}
|
|
|