package markdown
import (
"bytes"
"strings"
"testing"
"time"
"mercemay.top/httptap/internal/parser"
)
func TestWriteRendersTable(t *testing.T) {
req := parser.Message{
StartLine: "GET /api HTTP/1.1",
Headers: [][2]string{{"Host", "example.com"}},
}
resp := parser.Message{
StartLine: "HTTP/1.1 200 OK",
Headers: [][2]string{{"Content-Type", "text/plain"}},
Body: []byte("hi"),
}
var buf bytes.Buffer
if err := Write(&buf, req, resp, time.Unix(0, 0).UTC(),
5*time.Millisecond, Default()); err != nil {
t.Fatal(err)
}
out := buf.String()
for _, want := range []string{
"| Host | example.com |",
"**Request headers**",
"**Response body**",
"→ `200`",
} {
if !strings.Contains(out, want) {
t.Errorf("missing %q in:\n%s", want, out)
}
}
}
func TestEscapePipes(t *testing.T) {
if got := escape("a|b\nc"); got != `a\|b c` {
t.Fatalf("got %q", got)
}
}
func TestBodyTruncation(t *testing.T) {
big := bytes.Repeat([]byte("x"), 20)
var buf bytes.Buffer
opts := Default()
opts.BodyMaxBytes = 5
err := writeBody(&buf, "Body", "text/plain", big, opts.BodyMaxBytes)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(buf.String(), strings.Repeat("x", 5)) {
t.Error("truncated payload missing")
}
if strings.Count(buf.String(), "x") != 5 {
t.Errorf("expected 5 xs, got %d", strings.Count(buf.String(), "x"))
}
}