Co-authored-by: Mathias <mthbqv@gmail.com>
This commit was merged in pull request #152.
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -42,6 +43,10 @@ func Query(ctx context.Context, baseURL, query string, limit int) (string, error
|
||||
return "", nil
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
// The REST API requires a bearer token since hyperguild#149.
|
||||
if tok := os.Getenv("BRAIN_MCP_TOKEN"); tok != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+tok)
|
||||
}
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
|
||||
@@ -65,3 +65,18 @@ func TestQueryUnavailableServerReturnsEmpty(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, result)
|
||||
}
|
||||
|
||||
// hyperguild#149: the REST API requires a bearer token.
|
||||
func TestQuerySendsBrainMCPToken(t *testing.T) {
|
||||
var auth string
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
auth = r.Header.Get("Authorization")
|
||||
_, _ = w.Write([]byte(`{"results":[]}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
t.Setenv("BRAIN_MCP_TOKEN", "lib-test-token")
|
||||
_, _ = brain.Query(context.Background(), srv.URL, "x", 1)
|
||||
if auth != "Bearer lib-test-token" {
|
||||
t.Fatalf("Authorization = %q, want the bearer token", auth)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user