package web_tests import ( "io" "net" "net/http" "strings" "syscall" "testing" web "git.sharkk.net/Go/Web" ) const port = ":8888" func TestRun(t *testing.T) { s := web.NewServer() go func() { defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM) _, err := http.Get("http://127.0.0.1" + port + "/") if err != nil { t.Errorf("Error: %s", err) } }() s.Run(port) } func TestPanic(t *testing.T) { s := web.NewServer() s.Get("/panic", func(ctx web.Context) error { panic("Something unbelievable happened") }) defer func() { r := recover() if r == nil { t.Error("Didn't panic") } }() s.Request("GET", "/panic", nil, nil) } func TestBadRequest(t *testing.T) { s := web.NewServer() go func() { defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM) conn, err := net.Dial("tcp", port) if err != nil { t.Errorf("Error: %s", err) } defer conn.Close() _, err = io.WriteString(conn, "BadRequest\r\n\r\n") if err != nil { t.Errorf("Error: %s", err) } response, err := io.ReadAll(conn) if err != nil { t.Errorf("Error: %s", err) } if string(response) != "HTTP/1.1 400 Bad Request\r\n\r\n" { t.Errorf("Error: %s", string(response)) } }() s.Run(port) } func TestBadRequestHeader(t *testing.T) { s := web.NewServer() s.Get("/", func(ctx web.Context) error { return ctx.String("Hello") }) go func() { defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM) conn, err := net.Dial("tcp", port) if err != nil { t.Errorf("Error: %s", err) } defer conn.Close() _, err = io.WriteString(conn, "GET / HTTP/1.1\r\nBadHeader\r\nGood: Header\r\n\r\n") if err != nil { t.Errorf("Error: %s", err) } buffer := make([]byte, len("HTTP/1.1 200")) _, err = conn.Read(buffer) if err != nil { t.Errorf("Error: %s", err) } if string(buffer) != "HTTP/1.1 200" { t.Errorf("Error: %s", string(buffer)) } }() s.Run(port) } func TestBadRequestMethod(t *testing.T) { s := web.NewServer() go func() { defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM) conn, err := net.Dial("tcp", port) if err != nil { t.Errorf("Error: %s", err) } defer conn.Close() _, err = io.WriteString(conn, "BAD-METHOD / HTTP/1.1\r\n\r\n") if err != nil { t.Errorf("Error: %s", err) } response, err := io.ReadAll(conn) if err != nil { t.Errorf("Error: %s", err) } if string(response) != "HTTP/1.1 400 Bad Request\r\n\r\n" { t.Errorf("Error: %s", string(response)) } }() s.Run(port) } func TestBadRequestProtocol(t *testing.T) { s := web.NewServer() s.Get("/", func(ctx web.Context) error { return ctx.String("Hello") }) go func() { defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM) conn, err := net.Dial("tcp", port) if err != nil { t.Errorf("Error: %s", err) } defer conn.Close() _, err = io.WriteString(conn, "GET /\r\n\r\n") if err != nil { t.Errorf("Error: %s", err) } buffer := make([]byte, len("HTTP/1.1 200")) _, err = conn.Read(buffer) if err != nil { t.Errorf("Error: %s", err) } if string(buffer) != "HTTP/1.1 200" { t.Errorf("Error: %s", string(buffer)) } }() s.Run(port) } func TestEarlyClose(t *testing.T) { s := web.NewServer() go func() { defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM) conn, err := net.Dial("tcp", port) if err != nil { t.Errorf("Error: %s", err) } _, err = io.WriteString(conn, "GET /\r\n") if err != nil { t.Errorf("Error: %s", err) } err = conn.Close() if err != nil { t.Errorf("Error: %s", err) } }() s.Run(port) } func TestUnavailablePort(t *testing.T) { listener, err := net.Listen("tcp", port) if err != nil { t.Errorf("Error: %s", err) } defer listener.Close() s := web.NewServer() s.Run(port) } func TestBodyContent(t *testing.T) { s := web.NewServer() s.Post("/", func(ctx web.Context) error { body := ctx.Request().Body() return ctx.String(string(body)) }) response := s.Request("POST", "/", nil, io.NopCloser(strings.NewReader("Hello"))) if response.Status() != 200 { t.Errorf("Error: %s", response.Body()) } if string(response.Body()) != "Hello" { t.Errorf("Error: %s", response.Body()) } }