Files
bd2/go/internal/server/session/scoped_handler_test.go
T
Flechazo d04791dd31 feat(server): add colored logs, level rewards, hunting dispatch and fiend hunt
Persist claims, AP, teams and presets with session receipts and atomic request rollback. Read rewards and growth from GameData; replace fixed Fiend Hunt user state and preserve the account initialization contract.
2026-10-04 21:27:19 +08:00

50 lines
1.6 KiB
Go

package session
import (
"encoding/base64"
"encoding/json"
"testing"
"bd2server/internal/server/cryptox"
"bd2server/internal/server/protocol"
"bd2server/internal/server/wire"
)
type scopedHandler struct{ sessions []string }
func (h *scopedHandler) Handle(string, []byte) (int, []byte, bool, error) {
panic("session handler fell back to unscoped dispatch")
}
func (h *scopedHandler) HandleSession(path string, request []byte, sessionID string) (int, []byte, bool, error) {
h.sessions = append(h.sessions, sessionID)
return 77, nil, true, nil
}
func TestScopedDispatchUsesAuthorizedSessionInsideBatch(t *testing.T) {
h := &scopedHandler{}
s, err := NewServer(fakeLogin{}, h)
if err != nil {
t.Fatal(err)
}
first := login(t, s)
firstGame := s.sessions[sessionTokenKey(first.Cookie)]
second := login(t, s)
secondGame := s.sessions[sessionTokenKey(second.Cookie)]
for _, test := range []struct {
cookie string
game *gameSession
}{{first.Cookie, firstGame}, {second.Cookie, secondGame}, {first.Cookie, firstGame}} {
requests := []protocol.BatchRequest{{Path: "/Scoped", RequestData: base64.StdEncoding.EncodeToString(wire.AppendVarint(nil, 1, 2))}, {Path: "/ScopedSecond", RequestData: base64.StdEncoding.EncodeToString(wire.AppendVarint(nil, 1, 3))}}
plain, _ := json.Marshal(requests)
body, _ := cryptox.EncryptBase64(plain, test.game.key)
if _, err := s.DispatchRaw("/BatchRequest", []byte(body), "s="+test.cookie); err != nil {
t.Fatal(err)
}
for _, got := range h.sessions[len(h.sessions)-2:] {
if got != test.game.id {
t.Fatalf("session %q want %q", got, test.game.id)
}
}
}
}