High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
package orchestrator
import (
"bytes"
"io"
"net/http/httptest"
"strings"
"testing"
)
func TestExtractExplicitTabID_Path(t *testing.T) {
r := httptest.NewRequest("GET", "/x", nil)
r.SetPathValue("id", "tab-from-path")
got, src := ExtractExplicitTabID(r)
if got != "tab-from-path" || src != TabIDSourcePath {
t.Fatalf("got %q/%q; want tab-from-path/path", got, src)
}
}
func TestExtractExplicitTabID_Query(t *testing.T) {
r := httptest.NewRequest("GET", "/x?tabId=tab-q", nil)
got, src := ExtractExplicitTabID(r)
if got != "tab-q" || src != TabIDSourceQuery {
t.Fatalf("got %q/%q; want tab-q/query", got, src)
}
}
func TestExtractExplicitTabID_BodyJSON(t *testing.T) {
body := []byte(`{"url":"about:blank","tabId":"tab-b"}`)
r := httptest.NewRequest("POST", "/navigate", bytes.NewReader(body))
r.Header.Set("Content-Type", "application/json")
r.ContentLength = int64(len(body))
got, src := ExtractExplicitTabID(r)
if got != "tab-b" || src != TabIDSourceBody {
t.Fatalf("got %q/%q; want tab-b/body", got, src)
}
// Body must be readable downstream.
rest, err := io.ReadAll(r.Body)
if err != nil {
t.Fatalf("body read after peek: %v", err)
}
if !bytes.Equal(rest, body) {
t.Fatalf("body after peek = %q; want %q", rest, body)
}
}
func TestExtractExplicitTabID_BodyWithCharset(t *testing.T) {
body := []byte(`{"tabId":"tab-c"}`)
r := httptest.NewRequest("POST", "/navigate", bytes.NewReader(body))
r.Header.Set("Content-Type", "application/json; charset=utf-8")
r.ContentLength = int64(len(body))
got, src := ExtractExplicitTabID(r)
if got != "tab-c" || src != TabIDSourceBody {
t.Fatalf("got %q/%q; want tab-c/body", got, src)
}
}
func TestExtractExplicitTabID_BodySkippedForNonJSON(t *testing.T) {
body := []byte(`{"tabId":"sneaky"}`)
r := httptest.NewRequest("POST", "/x", bytes.NewReader(body))
r.Header.Set("Content-Type", "text/plain")
r.ContentLength = int64(len(body))
got, src := ExtractExplicitTabID(r)
if got != "" || src != TabIDSourceNone {
t.Fatalf("non-JSON body should not be peeked, got %q/%q", got, src)
}
}
func TestExtractExplicitTabID_BodySkippedWhenOversized(t *testing.T) {
r := httptest.NewRequest("POST", "/x", bytes.NewReader([]byte(`{"tabId":"too-big"}`)))
r.Header.Set("Content-Type", "application/json")
r.ContentLength = maxBodyPeek + 1
got, _ := ExtractExplicitTabID(r)
if got != "" {
t.Fatalf("oversized body should be skipped, got %q", got)
}
}
func TestExtractExplicitTabID_BodyReadWhenUnknownLength(t *testing.T) {
r := httptest.NewRequest("POST", "/x", bytes.NewReader([]byte(`{"tabId":"streaming"}`)))
r.Header.Set("Content-Type", "application/json")
r.ContentLength = -1
got, src := ExtractExplicitTabID(r)
if got != "streaming" || src != TabIDSourceBody {
t.Fatalf("an unknown-length body is a chunked client body and must route by its tabId; got %q/%q", got, src)
}
}
func TestExtractExplicitTabID_QueryWinsOverBody(t *testing.T) {
body := []byte(`{"tabId":"from-body"}`)
r := httptest.NewRequest("POST", "/x?tabId=from-query", bytes.NewReader(body))
r.Header.Set("Content-Type", "application/json")
r.ContentLength = int64(len(body))
got, src := ExtractExplicitTabID(r)
if got != "from-query" || src != TabIDSourceQuery {
t.Fatalf("query should win; got %q/%q", got, src)
}
}
func TestExtractExplicitTabID_BadJSONLeavesBodyIntact(t *testing.T) {
body := []byte(`{not-json`)
r := httptest.NewRequest("POST", "/x", bytes.NewReader(body))
r.Header.Set("Content-Type", "application/json")
r.ContentLength = int64(len(body))
got, _ := ExtractExplicitTabID(r)
if got != "" {
t.Fatalf("bad JSON should yield no tab id, got %q", got)
}
rest, _ := io.ReadAll(r.Body)
if !bytes.Equal(rest, body) {
t.Fatalf("body after failed peek = %q; want %q", rest, body)
}
}
func TestExtractExplicitTabID_TrimsWhitespace(t *testing.T) {
r := httptest.NewRequest("GET", "/x?tabId=%20%20", nil)
got, _ := ExtractExplicitTabID(r)
if got != "" {
t.Fatalf("whitespace-only tab id should be empty, got %q", got)
}
}
func TestExtractExplicitTabID_LongOKBody(t *testing.T) {
// Pad body up close to (but under) the budget to exercise the
// LimitReader sentinel path.
pad := strings.Repeat(" ", maxBodyPeek-32)
body := []byte(`{"tabId":"tail"` + pad + `}`)
if len(body) > maxBodyPeek {
t.Fatalf("test body %d > budget %d", len(body), maxBodyPeek)
}
r := httptest.NewRequest("POST", "/x", bytes.NewReader(body))
r.Header.Set("Content-Type", "application/json")
r.ContentLength = int64(len(body))
got, _ := ExtractExplicitTabID(r)
if got != "tail" {
t.Fatalf("got %q, want tail", got)
}
}
func TestExtractRequestedBrowser_Query(t *testing.T) {
r := httptest.NewRequest("GET", "/navigate?browser=cloak", nil)
if got := ExtractRequestedBrowser(r); got != "cloak" {
t.Fatalf("got %q, want cloak", got)
}
}
func TestExtractRequestedBrowser_IgnoresBody(t *testing.T) {
body := []byte(`{"url":"about:blank","browser":"cloak"}`)
r := httptest.NewRequest("POST", "/navigate", bytes.NewReader(body))
r.Header.Set("Content-Type", "application/json")
r.ContentLength = int64(len(body))
if got := ExtractRequestedBrowser(r); got != "" {
t.Fatalf("got %q, want empty (body browser field should be ignored)", got)
}
}