internal/tui/view/request/body_test.go

package request

import (
	"strings"
	"testing"

	"mercemay.top/httptap/internal/parser"
	"mercemay.top/httptap/internal/tui/theme"
)

func TestRenderBodyJSON(t *testing.T) {
	msg := parser.Message{
		Headers: [][2]string{{"Content-Type", "application/json"}},
		Body:    []byte(`{"a":1,"b":[1,2]}`),
	}
	got := RenderBody(theme.Default(), msg)
	if !strings.Contains(got, "\n") {
		t.Errorf("expected indented json, got: %q", got)
	}
	if !strings.Contains(got, "\"a\": 1") {
		t.Errorf("expected pretty a field, got: %q", got)
	}
}

func TestRenderBodyText(t *testing.T) {
	msg := parser.Message{
		Headers: [][2]string{{"Content-Type", "text/plain"}},
		Body:    []byte("hello world"),
	}
	if got := RenderBody(theme.Default(), msg); got != "hello world" {
		t.Errorf("got = %q", got)
	}
}

func TestRenderBodyBinary(t *testing.T) {
	msg := parser.Message{
		Headers: [][2]string{{"Content-Type", "application/octet-stream"}},
		Body:    []byte{0, 1, 2, 3, 4, 5, 6, 7},
	}
	got := RenderBody(theme.Default(), msg)
	if !strings.Contains(got, "00 01 02 03 04 05 06 07") {
		t.Errorf("hex dump missing from %q", got)
	}
}

func TestRenderBodyEmpty(t *testing.T) {
	msg := parser.Message{}
	got := RenderBody(theme.Default(), msg)
	if !strings.Contains(got, "empty body") {
		t.Errorf("empty-body placeholder missing: %q", got)
	}
}

func TestHexDumpAlignment(t *testing.T) {
	out := hexDump([]byte("AB"))
	if !strings.Contains(out, "00000000") {
		t.Errorf("missing offset: %q", out)
	}
	// Line should end with ASCII gutter '|AB|'.
	if !strings.HasSuffix(strings.TrimSpace(out), "|AB|") {
		t.Errorf("bad gutter: %q", out)
	}
}