Test that a retried POST keeps its body after a token refresh

This commit is contained in:
Roland Geider
2026-12-03 13:04:32 +01:00
parent 92c9039b49
commit a48787c0be

View File

@@ -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(<int>[]), 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(