fix: propagate QR output failures

This commit is contained in:
2026-07-20 14:06:51 +08:00
parent 43c688af6f
commit 8fea0c3c11
2 changed files with 68 additions and 3 deletions
+43
View File
@@ -12,10 +12,19 @@ import (
"os"
"path/filepath"
"strings"
"sync/atomic"
"testing"
"time"
)
var errQRWriterFailed = errors.New("QR writer failed")
type failingQRWriter struct{}
func (failingQRWriter) Write([]byte) (int, error) {
return 0, errQRWriterFailed
}
func TestParseServiceResponse(t *testing.T) {
var result struct {
Code int `json:"code"`
@@ -244,6 +253,40 @@ func TestLoginQRCoreFlow(t *testing.T) {
}
}
func TestLoginReturnsQROutputErrorBeforeLongPoll(t *testing.T) {
var longPollRequests atomic.Int32
var server *httptest.Server
server = httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
switch request.URL.Path {
case "/serviceLogin":
_, _ = io.WriteString(writer, `&&&START&&&{"code":70016,"location":"`+server.URL+`/prepare"}`)
case "/loginUrl":
_, _ = io.WriteString(writer, `&&&START&&&{"code":0,"loginUrl":"https://qr.example/login","qr":"https://qr.example/image","lp":"`+server.URL+`/lp"}`)
case "/lp":
longPollRequests.Add(1)
_, _ = io.WriteString(writer, `&&&START&&&{"code":70016}`)
default:
http.NotFound(writer, request)
}
}))
defer server.Close()
client, err := NewClient(t.TempDir(), WithHTTPClient(server.Client()), WithQRWriter(failingQRWriter{}))
if err != nil {
t.Fatal(err)
}
client.serviceLoginURL = server.URL + "/serviceLogin"
client.loginURL = server.URL + "/loginUrl"
_, err = client.Login(context.Background())
if !errors.Is(err, errQRWriterFailed) {
t.Fatalf("Login() error = %v, want %v", err, errQRWriterFailed)
}
if requests := longPollRequests.Load(); requests != 0 {
t.Fatalf("long-poll requests = %d, want 0", requests)
}
}
func TestQRLoginRejectsIncompleteCallbackWithoutSaving(t *testing.T) {
var server *httptest.Server
server = httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {