test(agentcompat): add protocol clients

Co-authored-by: naiba/CloudCode <hi+cloudcode@nai.ba>
This commit is contained in:
naiba
2026-07-20 04:48:15 +00:00
co-authored by naiba/CloudCode
parent 8a0e84b214
commit 1c3e926819
23 changed files with 3329 additions and 0 deletions
@@ -0,0 +1,35 @@
package client
import (
"context"
"encoding/base64"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/nezhahq/nezha/pkg/agentcompatcontract"
"github.com/stretchr/testify/require"
)
func TestRESTTypedCapabilityHeaderOnlyAttachesWhenRequested(t *testing.T) {
raw := base64.RawURLEncoding.EncodeToString([]byte(strings.Repeat("h", 32)))
capability, err := ParseIOStreamCapability(raw)
require.NoError(t, err)
seen := make(chan string, 2)
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
seen <- request.Header.Get(agentcompatcontract.IOStreamCapabilityHeader)
writer.Header().Set("Content-Type", "application/json")
_, err := writer.Write([]byte(`{"success":true,"data":{}}`))
require.NoError(t, err)
}))
t.Cleanup(server.Close)
transport := newTestClient(t, Config{BaseURL: server.URL, BearerToken: "private-pat"})
_, err = DoREST[struct{}, struct{}](context.Background(), transport, RESTRequest[struct{}]{Method: http.MethodPost, Path: "/ordinary"})
require.NoError(t, err)
_, err = DoREST[struct{}, struct{}](context.Background(), transport, RESTRequest[struct{}]{Method: http.MethodPost, Path: "/create", IOStreamCapability: capability})
require.NoError(t, err)
require.Empty(t, <-seen)
require.Equal(t, raw, <-seen)
}