internal/export/curl/curl_test.go

package curl

import (
	"strings"
	"testing"

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

func TestRenderSimpleGet(t *testing.T) {
	req := parser.Message{
		StartLine: "GET /api HTTP/1.1",
		Headers:   [][2]string{{"Host", "example.com"}, {"User-Agent", "tap/1"}},
	}
	got := Render(req, Options{})
	if !strings.HasPrefix(got, "curl ") {
		t.Errorf("expected curl prefix: %q", got)
	}
	if !strings.Contains(got, "-H 'User-Agent: tap/1'") {
		t.Errorf("missing UA header: %q", got)
	}
}

func TestRenderPostWithBody(t *testing.T) {
	req := parser.Message{
		StartLine: "POST /api HTTP/1.1",
		Headers:   [][2]string{{"Content-Type", "application/json"}},
		Body:      []byte(`{"ok":true}`),
	}
	got := Render(req, Default())
	if !strings.Contains(got, "-X POST") {
		t.Errorf("expected -X POST: %q", got)
	}
	if !strings.Contains(got, "--data-binary") {
		t.Errorf("expected --data-binary: %q", got)
	}
}

func TestRenderInsecure(t *testing.T) {
	req := parser.Message{StartLine: "GET / HTTP/1.1"}
	got := Render(req, Options{Insecure: true})
	if !strings.Contains(got, "-k") {
		t.Errorf("missing -k")
	}
}

func TestShellQuote(t *testing.T) {
	cases := map[string]string{
		"plain":         "plain",
		"":              "''",
		"has space":     "'has space'",
		"it's":          `'it'\''s'`,
		"$with dollar":  "'$with dollar'",
	}
	for in, want := range cases {
		if got := shellQuote(in); got != want {
			t.Errorf("shellQuote(%q)=%q want %q", in, got, want)
		}
	}
}