323 lines
8.1 KiB
Go
323 lines
8.1 KiB
Go
package core
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestWrite(t *testing.T) {
|
|
res := Response{}
|
|
v := "test-text"
|
|
res.Any(v)
|
|
|
|
if string(res.body) != v {
|
|
t.Errorf("failed writing text")
|
|
}
|
|
}
|
|
|
|
func TestWriteJson(t *testing.T) {
|
|
res := Response{}
|
|
j := "{\"name\": \"test\"}"
|
|
res.Json(j)
|
|
|
|
if string(res.body) != j {
|
|
t.Errorf("failed wrting jsom")
|
|
}
|
|
}
|
|
|
|
func TestSetHeaders(t *testing.T) {
|
|
res := Response{}
|
|
res.SetHeader("testkey", "testval")
|
|
|
|
headers := res.headers
|
|
if len(headers) < 1 {
|
|
t.Errorf("testing set header failed")
|
|
}
|
|
}
|
|
|
|
func TestReset(t *testing.T) {
|
|
res := Response{}
|
|
res.Any("test text")
|
|
if res.body == nil {
|
|
t.Errorf("expecting body to not be empty, found empty")
|
|
}
|
|
j := "{\"name\": \"test\"}"
|
|
res.Json(j)
|
|
if string(res.body) == "" {
|
|
t.Errorf("expecting JsonBody to not be empty, found empty")
|
|
}
|
|
|
|
res.reset()
|
|
|
|
if !(res.body == nil && string(res.body) == "") {
|
|
t.Errorf("failed testing response reset()")
|
|
}
|
|
}
|
|
|
|
func TestCastBasicVarToString(t *testing.T) {
|
|
s := "test str"
|
|
r := Response{}
|
|
c := r.castBasicVarsToString(s)
|
|
if fmt.Sprintf("%T", c) != "string" {
|
|
t.Errorf("failed test cast basic var to string")
|
|
}
|
|
var i int = 3
|
|
r = Response{}
|
|
c = r.castBasicVarsToString(i)
|
|
if fmt.Sprintf("%T", c) != "string" {
|
|
t.Errorf("failed test cast basic var to string")
|
|
}
|
|
var i8 int8 = 3
|
|
r = Response{}
|
|
c = r.castBasicVarsToString(i8)
|
|
if fmt.Sprintf("%T", c) != "string" {
|
|
t.Errorf("failed test cast basic var to string")
|
|
}
|
|
var i16 int16 = 3
|
|
r = Response{}
|
|
c = r.castBasicVarsToString(i16)
|
|
if fmt.Sprintf("%T", c) != "string" {
|
|
t.Errorf("failed test cast basic var to string")
|
|
}
|
|
var i32 int32 = 3
|
|
r = Response{}
|
|
c = r.castBasicVarsToString(i32)
|
|
if fmt.Sprintf("%T", c) != "string" {
|
|
t.Errorf("failed test cast basic var to string")
|
|
}
|
|
var i64 int64 = 3
|
|
r = Response{}
|
|
c = r.castBasicVarsToString(i64)
|
|
if fmt.Sprintf("%T", c) != "string" {
|
|
t.Errorf("failed test cast basic var to string")
|
|
}
|
|
var ui uint = 3
|
|
r = Response{}
|
|
c = r.castBasicVarsToString(ui)
|
|
if fmt.Sprintf("%T", c) != "string" {
|
|
t.Errorf("failed test cast basic var to string")
|
|
}
|
|
var ui8 uint8 = 3
|
|
r = Response{}
|
|
c = r.castBasicVarsToString(ui8)
|
|
if fmt.Sprintf("%T", c) != "string" {
|
|
t.Errorf("failed test cast basic var to string")
|
|
}
|
|
var ui16 uint16 = 3
|
|
r = Response{}
|
|
c = r.castBasicVarsToString(ui16)
|
|
if fmt.Sprintf("%T", c) != "string" {
|
|
t.Errorf("failed test cast basic var to string")
|
|
}
|
|
var ui32 uint32 = 3
|
|
r = Response{}
|
|
c = r.castBasicVarsToString(ui32)
|
|
if fmt.Sprintf("%T", c) != "string" {
|
|
t.Errorf("failed test cast basic var to string")
|
|
}
|
|
var ui64 uint64 = 3
|
|
r = Response{}
|
|
c = r.castBasicVarsToString(ui64)
|
|
if fmt.Sprintf("%T", c) != "string" {
|
|
t.Errorf("failed test cast basic var to string")
|
|
}
|
|
var f32 float32 = 3
|
|
r = Response{}
|
|
c = r.castBasicVarsToString(f32)
|
|
if fmt.Sprintf("%T", c) != "string" {
|
|
t.Errorf("failed test cast basic var to string")
|
|
}
|
|
var f64 float64 = 3
|
|
r = Response{}
|
|
c = r.castBasicVarsToString(f64)
|
|
if fmt.Sprintf("%T", c) != "string" {
|
|
t.Errorf("failed test cast basic var to string")
|
|
}
|
|
var c64 complex64 = 3
|
|
r = Response{}
|
|
c = r.castBasicVarsToString(c64)
|
|
if fmt.Sprintf("%T", c) != "string" {
|
|
t.Errorf("failed test cast basic var to string")
|
|
}
|
|
var c128 complex128 = 3
|
|
r = Response{}
|
|
c = r.castBasicVarsToString(c128)
|
|
if fmt.Sprintf("%T", c) != "string" {
|
|
t.Errorf("failed test cast basic var to string")
|
|
}
|
|
var b bool = true
|
|
r = Response{}
|
|
c = r.castBasicVarsToString(b)
|
|
if fmt.Sprintf("%T", c) != "string" {
|
|
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())
|
|
}
|
|
}
|