Add a new llm service that proxies chat-completion requests from AI web extensions to a configured OpenAI-compatible endpoint, replacing the standalone Node.js sidecar. It is part of the oCIS single binary, opt-in via OCIS_ADD_RUN_SERVICES=llm, and exposed through the proxy at POST /graph/v1beta1/extensions/org.libregraph/llm/chat/completions. Authentication is delegated to oCIS. Requests are rate-limited per user with a sliding window backed by the shared store, and the request body is sanitized before forwarding. Streaming requests are rejected. Signed-off-by: Lukas Hirt <info@hirt.cz>
41 lines
1.2 KiB
Go
41 lines
1.2 KiB
Go
package http
|
|
|
|
import (
|
|
nethttp "net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestRequireIdentity_RejectsWhenAccountMiddlewareStopsTheRequest(t *testing.T) {
|
|
// stands in for ExtractAccountUUID returning early on an invalid token
|
|
extract := func(next nethttp.Handler) nethttp.Handler {
|
|
return nethttp.HandlerFunc(func(w nethttp.ResponseWriter, r *nethttp.Request) {})
|
|
}
|
|
called := false
|
|
h := requireIdentity(extract)(nethttp.HandlerFunc(func(w nethttp.ResponseWriter, r *nethttp.Request) { called = true }))
|
|
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest(nethttp.MethodPost, "/", nil))
|
|
|
|
if rec.Code != nethttp.StatusUnauthorized {
|
|
t.Fatalf("expected 401, got %d", rec.Code)
|
|
}
|
|
if called {
|
|
t.Fatal("next handler must not run")
|
|
}
|
|
}
|
|
|
|
func TestRequireIdentity_PassesThroughWhenAccountMiddlewareContinues(t *testing.T) {
|
|
extract := func(next nethttp.Handler) nethttp.Handler { return next }
|
|
h := requireIdentity(extract)(nethttp.HandlerFunc(func(w nethttp.ResponseWriter, r *nethttp.Request) {
|
|
w.WriteHeader(nethttp.StatusNoContent)
|
|
}))
|
|
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest(nethttp.MethodPost, "/", nil))
|
|
|
|
if rec.Code != nethttp.StatusNoContent {
|
|
t.Fatalf("expected 204, got %d", rec.Code)
|
|
}
|
|
}
|