Upgrade and add new testing functions

This commit is contained in:
Zeni Kim 2026-09-13 00:09:50 -05:00
parent d12679df65
commit 9330ed8e74
13 changed files with 1867 additions and 130 deletions

View file

@ -1,7 +1,10 @@
package core
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
@ -152,3 +155,169 @@ func TestCastBasicVarToString(t *testing.T) {
t.Errorf("failed test cast basic var to string")
}
}
func TestCastBasicVarToStringPanicsOnUnsupported(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("expected panic for unsupported type")
}
}()
res := Response{}
res.castBasicVarsToString(struct{ Name string }{Name: "x"})
}
func TestText(t *testing.T) {
res := Response{}
res.Text("plain text")
if res.contentType != CONTENT_TYPE_TEXT {
t.Errorf("expected content type TEXT, got %q", res.contentType)
}
if string(res.body) != "plain text" {
t.Errorf("unexpected body %q", string(res.body))
}
}
func TestHTML(t *testing.T) {
res := Response{}
res.HTML("<h1>Hi</h1>")
if res.contentType != CONTENT_TYPE_HTML {
t.Errorf("expected content type HTML, got %q", res.contentType)
}
if string(res.body) != "<h1>Hi</h1>" {
t.Errorf("unexpected body %q", string(res.body))
}
}
func TestSetStatusCode(t *testing.T) {
res := Response{}
res.SetStatusCode(http.StatusCreated)
if res.statusCode != http.StatusCreated {
t.Errorf("expected status code 201, got %d", res.statusCode)
}
}
func TestSetContentType(t *testing.T) {
res := Response{}
res.SetContentType(CONTENT_TYPE_JSON)
if res.overrideContentType != CONTENT_TYPE_JSON {
t.Errorf("expected override content type JSON, got %q", res.overrideContentType)
}
}
func TestForceSendResponse(t *testing.T) {
res := Response{}
res.ForceSendResponse()
if !res.isTerminated {
t.Errorf("expected response to be terminated")
}
}
func TestTerminatedResponseIgnoresWrites(t *testing.T) {
res := Response{}
res.ForceSendResponse()
res.Text("ignored")
res.SetHeader("x", "y")
res.SetStatusCode(500)
if res.body != nil {
t.Errorf("expected body to be untouched after termination, got %q", string(res.body))
}
if len(res.headers) != 0 {
t.Errorf("expected no headers to be added after termination")
}
if res.statusCode != 0 {
t.Errorf("expected status code to stay 0 after termination, got %d", res.statusCode)
}
}
func TestRedirect(t *testing.T) {
res := Response{}
res.Redirect("https://example.com")
if res.redirectTo != "https://example.com" {
t.Errorf("expected redirect to 'https://example.com', got %q", res.redirectTo)
}
if res.redirectStatusCode != http.StatusTemporaryRedirect {
t.Errorf("expected default 307 redirect, got %d", res.redirectStatusCode)
}
}
func TestRedirectUse303(t *testing.T) {
res := Response{}
res.Redirect("https://example.com", true)
if res.redirectStatusCode != http.StatusSeeOther {
t.Errorf("expected 303 redirect, got %d", res.redirectStatusCode)
}
}
func TestRedirectInvalidUrlGetsLeadingSlash(t *testing.T) {
res := Response{}
// A relative path is treated as an invalid URL by the validator and should
// be normalized to an absolute path.
res.Redirect("dashboard")
if res.redirectTo != "/dashboard" {
t.Errorf("expected '/dashboard', got %q", res.redirectTo)
}
}
func TestResetRestoresDefaults(t *testing.T) {
res := Response{}
res.SetStatusCode(http.StatusTeapot)
res.SetContentType(CONTENT_TYPE_JSON)
res.Redirect("https://example.com")
res.Text("body")
res.reset()
if res.body != nil {
t.Errorf("expected body to be cleared")
}
if res.statusCode != http.StatusOK {
t.Errorf("expected status code to reset to 200, got %d", res.statusCode)
}
if res.contentType != CONTENT_TYPE_HTML {
t.Errorf("expected content type to reset to HTML, got %q", res.contentType)
}
if res.overrideContentType != "" {
t.Errorf("expected override content type to be cleared")
}
if res.redirectTo != "" {
t.Errorf("expected redirect to be cleared")
}
if res.isTerminated {
t.Errorf("expected termination flag to be cleared")
}
}
func TestBufferFile(t *testing.T) {
w := httptest.NewRecorder()
res := Response{HttpResponseWriter: w}
var buf bytes.Buffer
buf.WriteString("file-content")
res.BufferFile("report.csv", "text/csv", buf)
rsp := w.Result()
if ct := rsp.Header.Get(CONTENT_TYPE); ct != "text/csv" {
t.Errorf("expected content type 'text/csv', got %q", ct)
}
if cd := rsp.Header.Get("Content-Disposition"); cd != "attachment; filename=report.csv" {
t.Errorf("unexpected content disposition %q", cd)
}
if w.Body.String() != "file-content" {
t.Errorf("unexpected body %q", w.Body.String())
}
}
func TestBufferInline(t *testing.T) {
w := httptest.NewRecorder()
res := Response{HttpResponseWriter: w}
var buf bytes.Buffer
buf.WriteString("inline-content")
res.BufferInline("image.png", "image/png", buf)
rsp := w.Result()
if ct := rsp.Header.Get(CONTENT_TYPE); ct != "image/png" {
t.Errorf("expected content type 'image/png', got %q", ct)
}
if w.Body.String() != "inline-content" {
t.Errorf("unexpected body %q", w.Body.String())
}
}