package test
import (
"bytes"
"github.com/projectdiscovery/simplehttpserver/pkg/httpserver"
"io"
"net/http/httptest"
"os"
"strings"
"testing"
)
func TestServePythonStyleHtmlPageForDirectories(t *testing.T) {
const want = `
Directory listing for /
Directory listing for /
`
py := httpserver.PythonStyle("./fixture/pythonliststyle")
w := httptest.NewRecorder()
py.ServeHTTP(w, httptest.NewRequest("GET", "http://example.com/", nil))
b, _ := io.ReadAll(w.Result().Body)
body := string(b)
if strings.Compare(want, body) != 0 {
t.Errorf("want:\n%s\ngot:\n%s", want, body)
}
}
func TestServeFileContentForFiles(t *testing.T) {
want, _ := os.ReadFile("./fixture/pythonliststyle/test file.txt")
py := httpserver.PythonStyle("./fixture/pythonliststyle")
w := httptest.NewRecorder()
py.ServeHTTP(w, httptest.NewRequest(
"GET",
"http://example.com/test%20file.txt",
nil,
))
got, _ := io.ReadAll(w.Result().Body)
if !bytes.Equal(want, got) {
t.Errorf("want:\n%x\ngot:\n%x", want, got)
}
}
func TestResponseNotFound(t *testing.T) {
const want = `404 page not found
`
py := httpserver.PythonStyle("./fixture/pythonliststyle")
w := httptest.NewRecorder()
py.ServeHTTP(w, httptest.NewRequest(
"GET",
"http://example.com/does-not-exist.txt",
nil,
))
got, _ := io.ReadAll(w.Result().Body)
if strings.Compare(want, string(got)) != 0 {
t.Errorf("want:\n%s\ngot:\n%s", want, got)
}
}