Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions agent/internal/publichttp/disconnect_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package publichttp

import (
"context"
"net/http"
"net/http/httptest"
"testing"

"github.com/muxable/rtchat/agent/internal/webhook"
"go.uber.org/zap"
"go.uber.org/zap/zaptest/observer"
)

// The app's WebView aborts /embed when the preview closes or switches
// channel. The provider lookup then fails at once on the canceled request;
// a caller that left is not a server failure.
func TestEmbedAbortedByClientIsNotAnError(t *testing.T) {
core, logs := observer.New(zap.InfoLevel)
defer zap.ReplaceGlobals(zap.New(core))()
s, _, mux := fixture()
s.HTTP = &http.Client{Transport: transport(func(r *http.Request) (*http.Response, error) {
if err := r.Context().Err(); err != nil {
return nil, err
}
return response(200, `{"data":[{"id":"1","login":"streamer"}]}`), nil
})}
ctx, hangUp := context.WithCancel(context.Background())
hangUp()
request := httptest.NewRequestWithContext(ctx, http.MethodGet, "/embed?provider=twitch&channelId=1", nil)
webhook.LoggingMiddleware(mux).ServeHTTP(httptest.NewRecorder(), request)
entries := logs.FilterMessage("http request").All()
if len(entries) != 1 || entries[0].Level != zap.InfoLevel || entries[0].ContextMap()["status"] != int64(499) {
t.Fatalf("aborted embed logged as %v", entries)
}
}
7 changes: 7 additions & 0 deletions agent/internal/webhook/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package webhook

import (
"context"
"errors"
"net/http"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -73,6 +75,11 @@ func LoggingMiddleware(next http.Handler) http.Handler {
start := time.Now()
writer := &statusWriter{ResponseWriter: w, status: http.StatusOK}
next.ServeHTTP(writer, r)
// A caller that hung up cancels every downstream call, so whatever the
// handler wrote describes the hang-up, not the server.
if errors.Is(r.Context().Err(), context.Canceled) {
writer.status = 499
}
level := zap.InfoLevel
if writer.status >= 500 {
level = zap.ErrorLevel
Expand Down
Loading