From a48787c0bed228db9b95a8c2272176c1910a9f4b Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Thu, 3 Dec 2026 13:04:32 +0100 Subject: [PATCH] Test that a retried POST keeps its body after a token refresh --- test/core/network/auth_http_client_test.dart | 39 ++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/test/core/network/auth_http_client_test.dart b/test/core/network/auth_http_client_test.dart index b13d756b4..7b81f11a0 100644 --- a/test/core/network/auth_http_client_test.dart +++ b/test/core/network/auth_http_client_test.dart @@ -258,6 +258,45 @@ void main() { ); }); + test('replayable Request: a retried POST carries the original body', () async { + // Without the copied body the retry would reach the server as an empty + // POST: a save made just as the token expired would be lost + auth = AuthState( + credential: JwtCredential( + accessToken: 'old-access', + expiresAt: DateTime.now().toUtc().add(const Duration(hours: 1)), + ), + ); + onRefresh = () async { + auth = AuthState( + credential: JwtCredential( + accessToken: 'new-access', + expiresAt: DateTime.now().toUtc().add(const Duration(hours: 1)), + ), + ); + }; + await stubTwoResponses( + http.StreamedResponse(Stream.value([]), 401), + http.StreamedResponse(Stream.value('{"id":7}'.codeUnits), 201), + ); + + const payload = '{"name":"Bankdrücken","weight":80.5}'; + final url = Uri.parse('https://wger.example/api/v2/routine/'); + final request = http.Request('POST', url) + ..headers[HttpHeaders.contentTypeHeader] = 'application/json; charset=utf-8' + ..body = payload; + final response = await buildClient().send(request); + + expect(response.statusCode, 201); + final retry = verify(inner.send(captureAny)).captured[1] as http.Request; + expect(retry, isNot(same(request))); + expect(retry.method, 'POST'); + expect(retry.url, url); + expect(retry.body, payload); + expect(retry.headers[HttpHeaders.contentTypeHeader], 'application/json; charset=utf-8'); + expect(retry.headers[HttpHeaders.authorizationHeader], 'Bearer new-access'); + }); + test('replayable Request: retry also 401 → session expired + synthetic 401', () async { auth = AuthState( credential: JwtCredential(