File size: 4,106 Bytes
6a7089a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | package mcp
import (
"context"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
func TestNewClient(t *testing.T) {
c := NewClient("http://localhost:9867", "tok123")
if c.BaseURL != "http://localhost:9867" {
t.Fatalf("BaseURL = %q, want %q", c.BaseURL, "http://localhost:9867")
}
if c.Token != "tok123" {
t.Fatalf("Token = %q, want %q", c.Token, "tok123")
}
if c.HTTPClient == nil {
t.Fatal("HTTPClient is nil")
}
}
func TestClientGet(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Errorf("method = %s, want GET", r.Method)
}
if r.Header.Get("Authorization") != "Bearer testtoken" {
t.Errorf("no auth header")
}
if r.URL.Query().Get("tabId") != "t1" {
t.Errorf("tabId = %q, want t1", r.URL.Query().Get("tabId"))
}
w.WriteHeader(200)
_, _ = io.WriteString(w, `{"ok":true}`)
}))
defer srv.Close()
c := NewClient(srv.URL, "testtoken")
body, code, err := c.Get(context.Background(), "/health", url.Values{"tabId": {"t1"}})
if err != nil {
t.Fatal(err)
}
if code != 200 {
t.Fatalf("code = %d, want 200", code)
}
if !strings.Contains(string(body), `"ok":true`) {
t.Fatalf("body = %q", body)
}
}
func TestClientGetNoQuery(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.RawQuery != "" {
t.Errorf("unexpected query: %s", r.URL.RawQuery)
}
w.WriteHeader(200)
_, _ = io.WriteString(w, `{}`)
}))
defer srv.Close()
c := NewClient(srv.URL, "")
_, code, err := c.Get(context.Background(), "/health", nil)
if err != nil {
t.Fatal(err)
}
if code != 200 {
t.Fatalf("code = %d", code)
}
}
func TestClientPost(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("method = %s, want POST", r.Method)
}
if ct := r.Header.Get("Content-Type"); ct != "application/json" {
t.Errorf("content-type = %q", ct)
}
body, _ := io.ReadAll(r.Body)
if !strings.Contains(string(body), `"url"`) {
t.Errorf("body missing url field: %s", body)
}
w.WriteHeader(200)
_, _ = io.WriteString(w, `{"navigated":true}`)
}))
defer srv.Close()
c := NewClient(srv.URL, "")
body, code, err := c.Post(context.Background(), "/navigate", map[string]any{"url": "https://example.com"})
if err != nil {
t.Fatal(err)
}
if code != 200 {
t.Fatalf("code = %d", code)
}
if !strings.Contains(string(body), "navigated") {
t.Fatalf("body = %q", body)
}
}
func TestClientPostNilPayload(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
if len(body) != 0 {
t.Errorf("expected empty body, got %s", body)
}
w.WriteHeader(200)
_, _ = io.WriteString(w, `{}`)
}))
defer srv.Close()
c := NewClient(srv.URL, "")
_, code, err := c.Post(context.Background(), "/shutdown", nil)
if err != nil {
t.Fatal(err)
}
if code != 200 {
t.Fatalf("code = %d", code)
}
}
func TestClientAuthHeaderAbsentWhenNoToken(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if h := r.Header.Get("Authorization"); h != "" {
t.Errorf("unexpected Authorization header: %s", h)
}
w.WriteHeader(200)
_, _ = io.WriteString(w, `{}`)
}))
defer srv.Close()
c := NewClient(srv.URL, "")
_, _, err := c.Get(context.Background(), "/health", nil)
if err != nil {
t.Fatal(err)
}
}
func TestClientProfileInstancePath(t *testing.T) {
c := NewClient("http://localhost:9867", "")
got := c.profileInstancePath("work profile")
want := "/profiles/work%20profile/instance"
if got != want {
t.Fatalf("profileInstancePath = %q, want %q", got, want)
}
}
func TestClientDashboardProfilesURL(t *testing.T) {
c := NewClient("http://localhost:9867/", "")
got := c.dashboardProfilesURL()
want := "http://localhost:9867/dashboard/profiles"
if got != want {
t.Fatalf("dashboardProfilesURL = %q, want %q", got, want)
}
}
|