147 lines
4.1 KiB
Go
147 lines
4.1 KiB
Go
package mainserver
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strconv"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
const testSecret = "test-secret-key-base"
|
|
|
|
// known vector shared with the main-server (Ruby) test suite
|
|
const testVector = "e0a7f8b5508639b1afe604b41f00070dd282d02b6444db5b4856959f175e8361"
|
|
|
|
func TestSignMatchesSharedVector(t *testing.T) {
|
|
body := `{"status":"player1_won","duration_seconds":42}`
|
|
path := "/api/internal/matches/abc/result"
|
|
got := Sign(1_700_000_000, path, []byte(body), []byte(testSecret))
|
|
if got != testVector {
|
|
t.Fatalf("Sign = %q, want %q", got, testVector)
|
|
}
|
|
}
|
|
|
|
func newTestReporter(t *testing.T, baseURL string) *Reporter {
|
|
t.Helper()
|
|
rep, err := NewReporter(baseURL, []byte(testSecret), slog.New(slog.DiscardHandler))
|
|
if err != nil {
|
|
t.Fatalf("NewReporter: %v", err)
|
|
}
|
|
return rep
|
|
}
|
|
|
|
func TestReportMatchResult(t *testing.T) {
|
|
var gotPath, gotTS, gotSig, gotBody string
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
data, _ := io.ReadAll(r.Body)
|
|
gotPath = r.URL.Path
|
|
gotTS = r.Header.Get("X-Request-Timestamp")
|
|
gotSig = r.Header.Get("X-Request-Signature")
|
|
gotBody = string(data)
|
|
w.WriteHeader(http.StatusOK)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
rep := newTestReporter(t, srv.URL)
|
|
err := rep.ReportMatchResult(context.Background(), Result{
|
|
MatchID: "abc",
|
|
Status: StatusPlayer1Won,
|
|
DurationSeconds: 42,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("ReportMatchResult: %v", err)
|
|
}
|
|
|
|
if gotPath != "/api/internal/matches/abc/result" {
|
|
t.Fatalf("path = %q", gotPath)
|
|
}
|
|
if gotBody != `{"status":"player1_won","duration_seconds":42}` {
|
|
t.Fatalf("body = %q", gotBody)
|
|
}
|
|
ts, err := strconv.ParseInt(gotTS, 10, 64)
|
|
if err != nil {
|
|
t.Fatalf("timestamp header = %q: %v", gotTS, err)
|
|
}
|
|
if now := time.Now().Unix(); ts < now-60 || ts > now+60 {
|
|
t.Fatalf("timestamp = %d, want close to %d", ts, now)
|
|
}
|
|
want := "sha256=" + Sign(ts, gotPath, []byte(gotBody), []byte(testSecret))
|
|
if gotSig != want {
|
|
t.Fatalf("signature = %q, want %q", gotSig, want)
|
|
}
|
|
}
|
|
|
|
func TestReportMatchResultRetriesUntilSuccess(t *testing.T) {
|
|
var attempts atomic.Int32
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if attempts.Add(1) < 3 {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusOK)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
rep := newTestReporter(t, srv.URL)
|
|
err := rep.ReportMatchResult(context.Background(), Result{MatchID: "abc", Status: StatusDraw})
|
|
if err != nil {
|
|
t.Fatalf("ReportMatchResult: %v", err)
|
|
}
|
|
if got := attempts.Load(); got != 3 {
|
|
t.Fatalf("attempts = %d, want 3", got)
|
|
}
|
|
}
|
|
|
|
func TestReportMatchResultFailsAfterRetries(t *testing.T) {
|
|
var attempts atomic.Int32
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
attempts.Add(1)
|
|
w.WriteHeader(http.StatusConflict)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
rep := newTestReporter(t, srv.URL)
|
|
err := rep.ReportMatchResult(context.Background(), Result{MatchID: "abc", Status: StatusAbandoned})
|
|
if err == nil {
|
|
t.Fatal("expected an error after all attempts failed")
|
|
}
|
|
if got := attempts.Load(); got != 3 {
|
|
t.Fatalf("attempts = %d, want 3", got)
|
|
}
|
|
}
|
|
|
|
func TestNewReporterRejectsInvalidURL(t *testing.T) {
|
|
for _, url := range []string{"", "localhost:3000", "http://", "::", "ftp://example.com", "http://example.com/?a=b"} {
|
|
if _, err := NewReporter(url, []byte(testSecret), nil); err == nil {
|
|
t.Fatalf("NewReporter(%q): expected an error", url)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestReportMatchResultWithPathPrefix(t *testing.T) {
|
|
var gotPath string
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
gotPath = r.URL.Path
|
|
w.WriteHeader(http.StatusOK)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
rep := newTestReporter(t, srv.URL+"/main/")
|
|
err := rep.ReportMatchResult(context.Background(), Result{
|
|
MatchID: "abc",
|
|
Status: StatusPlayer1Won,
|
|
DurationSeconds: 1,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("ReportMatchResult: %v", err)
|
|
}
|
|
if gotPath != "/main/api/internal/matches/abc/result" {
|
|
t.Fatalf("path = %q, want /main/api/internal/matches/abc/result", gotPath)
|
|
}
|
|
}
|