diff --git a/test/core/network/base_provider_extra_test.dart b/test/core/network/base_provider_extra_test.dart deleted file mode 100644 index c597ca249..000000000 --- a/test/core/network/base_provider_extra_test.dart +++ /dev/null @@ -1,210 +0,0 @@ -/* - * This file is part of wger Workout Manager . - * Copyright (c) 2026 wger Team - * - * wger Workout Manager is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -import 'dart:async'; -import 'dart:convert'; -import 'dart:io'; - -import 'package:flutter_test/flutter_test.dart'; -import 'package:http/http.dart' as http; -import 'package:http/http.dart'; -import 'package:mockito/annotations.dart'; -import 'package:mockito/mockito.dart'; -import 'package:wger/core/exceptions/http_exception.dart'; -import 'package:wger/core/network/base_provider.dart'; - -import '../../utils.dart'; -import 'base_provider_extra_test.mocks.dart'; - -@GenerateMocks([http.Client]) -void main() { - late MockClient mockClient; - late WgerBaseProvider repo; - - setUp(() { - mockClient = MockClient(); - repo = buildTestBaseProvider(client: mockClient); - }); - - final uri = Uri.https('localhost', '/api/v2/endpoint/'); - - group('getDefaultHeaders', () { - test('returns Content-Type + User-Agent and never sets Authorization', () { - final headers = repo.getDefaultHeaders(); - - expect(headers[HttpHeaders.contentTypeHeader], contains('application/json')); - expect(headers[HttpHeaders.userAgentHeader], isNotNull); - // The Authorization header is injected by the AuthHttpClient layer, - // not built here. - expect(headers.containsKey(HttpHeaders.authorizationHeader), isFalse); - expect(headers.containsKey(HttpHeaders.acceptLanguageHeader), isFalse); - }); - - test('language sets Accept-Language', () { - final headers = repo.getDefaultHeaders(language: 'de'); - - expect(headers[HttpHeaders.acceptLanguageHeader], 'de'); - }); - }); - - group('fetch', () { - test('returns the parsed body on 200', () async { - when(mockClient.get(uri, headers: anyNamed('headers'))).thenAnswer( - (_) async => Response(jsonEncode({'value': 42}), 200), - ); - - final result = await repo.fetch(uri); - - expect(result, {'value': 42}); - }); - - test('throws WgerHttpException on a non-retried 4xx', () async { - when( - mockClient.get(uri, headers: anyNamed('headers')), - ).thenAnswer((_) async => Response('not found', 404)); - - expect(repo.fetch(uri), throwsA(isA())); - }); - - test('retries on a 5xx and eventually succeeds', () async { - var calls = 0; - when(mockClient.get(uri, headers: anyNamed('headers'))).thenAnswer((_) async { - calls++; - if (calls < 3) { - return Response('boom', 502); - } - return Response(jsonEncode({'ok': true}), 200); - }); - - final result = await repo.fetch( - uri, - initialDelay: const Duration(milliseconds: 1), - ); - - expect(result, {'ok': true}); - expect(calls, 3); - }); - - test('gives up and throws after maxRetries on persistent 5xx', () async { - when( - mockClient.get(uri, headers: anyNamed('headers')), - ).thenAnswer((_) async => Response('boom', 502)); - - expect( - repo.fetch( - uri, - maxRetries: 2, - initialDelay: const Duration(milliseconds: 1), - ), - throwsA(isA()), - ); - }); - - test('retries on TimeoutException and eventually succeeds', () async { - var calls = 0; - when(mockClient.get(uri, headers: anyNamed('headers'))).thenAnswer((_) async { - calls++; - if (calls < 2) { - throw TimeoutException('slow'); - } - return Response(jsonEncode({'ok': true}), 200); - }); - - final result = await repo.fetch( - uri, - initialDelay: const Duration(milliseconds: 1), - ); - - expect(result, {'ok': true}); - expect(calls, 2); - }); - - test('rethrows after maxRetries SocketExceptions', () async { - when( - mockClient.get(uri, headers: anyNamed('headers')), - ).thenThrow(const SocketException('no network')); - - expect( - repo.fetch(uri, maxRetries: 1, initialDelay: const Duration(milliseconds: 1)), - throwsA(isA()), - ); - }); - }); - - group('post', () { - test('returns the parsed body on success', () async { - when( - mockClient.post(uri, headers: anyNamed('headers'), body: anyNamed('body')), - ).thenAnswer((_) async => Response(jsonEncode({'id': 7}), 201)); - - final result = await repo.post({'name': 'foo'}, uri); - - expect(result['id'], 7); - }); - - test('throws WgerHttpException on >=400', () async { - when( - mockClient.post(uri, headers: anyNamed('headers'), body: anyNamed('body')), - ).thenAnswer((_) async => Response('bad', 400)); - - expect(repo.post({}, uri), throwsA(isA())); - }); - }); - - group('patch', () { - test('returns the parsed body on success', () async { - when( - mockClient.patch(uri, headers: anyNamed('headers'), body: anyNamed('body')), - ).thenAnswer((_) async => Response(jsonEncode({'id': 7}), 200)); - - final result = await repo.patch({'name': 'foo'}, uri); - - expect(result['id'], 7); - }); - - test('throws WgerHttpException on >=400', () async { - when( - mockClient.patch(uri, headers: anyNamed('headers'), body: anyNamed('body')), - ).thenAnswer((_) async => Response('bad', 422)); - - expect(repo.patch({}, uri), throwsA(isA())); - }); - }); - - group('deleteRequest', () { - test('builds the URL via makeUrl and returns the response on success', () async { - final deleteUri = Uri.https('localhost', '/api/v2/endpoint/5/'); - when( - mockClient.delete(deleteUri, headers: anyNamed('headers')), - ).thenAnswer((_) async => Response('', 204)); - - final response = await repo.deleteRequest('endpoint', 5); - - expect(response.statusCode, 204); - }); - - test('throws WgerHttpException on >=400', () async { - final deleteUri = Uri.https('localhost', '/api/v2/endpoint/5/'); - when( - mockClient.delete(deleteUri, headers: anyNamed('headers')), - ).thenAnswer((_) async => Response('forbidden', 403)); - - expect(repo.deleteRequest('endpoint', 5), throwsA(isA())); - }); - }); -} diff --git a/test/core/network/base_provider_extra_test.mocks.dart b/test/core/network/base_provider_extra_test.mocks.dart deleted file mode 100644 index de6f5a2a7..000000000 --- a/test/core/network/base_provider_extra_test.mocks.dart +++ /dev/null @@ -1,218 +0,0 @@ -// Mocks generated by Mockito 5.4.6 from annotations -// in wger/test/core/network/base_provider_extra_test.dart. -// Do not manually edit this file. - -// ignore_for_file: no_leading_underscores_for_library_prefixes -import 'dart:async' as _i3; -import 'dart:convert' as _i4; -import 'dart:typed_data' as _i6; - -import 'package:http/http.dart' as _i2; -import 'package:mockito/mockito.dart' as _i1; -import 'package:mockito/src/dummies.dart' as _i5; - -// ignore_for_file: type=lint -// ignore_for_file: avoid_redundant_argument_values -// ignore_for_file: avoid_setters_without_getters -// ignore_for_file: comment_references -// ignore_for_file: deprecated_member_use -// ignore_for_file: deprecated_member_use_from_same_package -// ignore_for_file: implementation_imports -// ignore_for_file: invalid_use_of_visible_for_testing_member -// ignore_for_file: must_be_immutable -// ignore_for_file: prefer_const_constructors -// ignore_for_file: unnecessary_parenthesis -// ignore_for_file: camel_case_types -// ignore_for_file: subtype_of_sealed_class -// ignore_for_file: invalid_use_of_internal_member - -class _FakeResponse_0 extends _i1.SmartFake implements _i2.Response { - _FakeResponse_0(Object parent, Invocation parentInvocation) : super(parent, parentInvocation); -} - -class _FakeStreamedResponse_1 extends _i1.SmartFake implements _i2.StreamedResponse { - _FakeStreamedResponse_1(Object parent, Invocation parentInvocation) - : super(parent, parentInvocation); -} - -/// A class which mocks [Client]. -/// -/// See the documentation for Mockito's code generation for more information. -class MockClient extends _i1.Mock implements _i2.Client { - MockClient() { - _i1.throwOnMissingStub(this); - } - - @override - _i3.Future<_i2.Response> head(Uri? url, {Map? headers}) => - (super.noSuchMethod( - Invocation.method(#head, [url], {#headers: headers}), - returnValue: _i3.Future<_i2.Response>.value( - _FakeResponse_0( - this, - Invocation.method(#head, [url], {#headers: headers}), - ), - ), - ) - as _i3.Future<_i2.Response>); - - @override - _i3.Future<_i2.Response> get(Uri? url, {Map? headers}) => - (super.noSuchMethod( - Invocation.method(#get, [url], {#headers: headers}), - returnValue: _i3.Future<_i2.Response>.value( - _FakeResponse_0( - this, - Invocation.method(#get, [url], {#headers: headers}), - ), - ), - ) - as _i3.Future<_i2.Response>); - - @override - _i3.Future<_i2.Response> post( - Uri? url, { - Map? headers, - Object? body, - _i4.Encoding? encoding, - }) => - (super.noSuchMethod( - Invocation.method( - #post, - [url], - {#headers: headers, #body: body, #encoding: encoding}, - ), - returnValue: _i3.Future<_i2.Response>.value( - _FakeResponse_0( - this, - Invocation.method( - #post, - [url], - {#headers: headers, #body: body, #encoding: encoding}, - ), - ), - ), - ) - as _i3.Future<_i2.Response>); - - @override - _i3.Future<_i2.Response> put( - Uri? url, { - Map? headers, - Object? body, - _i4.Encoding? encoding, - }) => - (super.noSuchMethod( - Invocation.method( - #put, - [url], - {#headers: headers, #body: body, #encoding: encoding}, - ), - returnValue: _i3.Future<_i2.Response>.value( - _FakeResponse_0( - this, - Invocation.method( - #put, - [url], - {#headers: headers, #body: body, #encoding: encoding}, - ), - ), - ), - ) - as _i3.Future<_i2.Response>); - - @override - _i3.Future<_i2.Response> patch( - Uri? url, { - Map? headers, - Object? body, - _i4.Encoding? encoding, - }) => - (super.noSuchMethod( - Invocation.method( - #patch, - [url], - {#headers: headers, #body: body, #encoding: encoding}, - ), - returnValue: _i3.Future<_i2.Response>.value( - _FakeResponse_0( - this, - Invocation.method( - #patch, - [url], - {#headers: headers, #body: body, #encoding: encoding}, - ), - ), - ), - ) - as _i3.Future<_i2.Response>); - - @override - _i3.Future<_i2.Response> delete( - Uri? url, { - Map? headers, - Object? body, - _i4.Encoding? encoding, - }) => - (super.noSuchMethod( - Invocation.method( - #delete, - [url], - {#headers: headers, #body: body, #encoding: encoding}, - ), - returnValue: _i3.Future<_i2.Response>.value( - _FakeResponse_0( - this, - Invocation.method( - #delete, - [url], - {#headers: headers, #body: body, #encoding: encoding}, - ), - ), - ), - ) - as _i3.Future<_i2.Response>); - - @override - _i3.Future read(Uri? url, {Map? headers}) => - (super.noSuchMethod( - Invocation.method(#read, [url], {#headers: headers}), - returnValue: _i3.Future.value( - _i5.dummyValue( - this, - Invocation.method(#read, [url], {#headers: headers}), - ), - ), - ) - as _i3.Future); - - @override - _i3.Future<_i6.Uint8List> readBytes( - Uri? url, { - Map? headers, - }) => - (super.noSuchMethod( - Invocation.method(#readBytes, [url], {#headers: headers}), - returnValue: _i3.Future<_i6.Uint8List>.value(_i6.Uint8List(0)), - ) - as _i3.Future<_i6.Uint8List>); - - @override - _i3.Future<_i2.StreamedResponse> send(_i2.BaseRequest? request) => - (super.noSuchMethod( - Invocation.method(#send, [request]), - returnValue: _i3.Future<_i2.StreamedResponse>.value( - _FakeStreamedResponse_1( - this, - Invocation.method(#send, [request]), - ), - ), - ) - as _i3.Future<_i2.StreamedResponse>); - - @override - void close() => super.noSuchMethod( - Invocation.method(#close, []), - returnValueForMissingStub: null, - ); -} diff --git a/test/core/network/base_provider_test.dart b/test/core/network/base_provider_test.dart index 50bf6d461..9a02e6509 100644 --- a/test/core/network/base_provider_test.dart +++ b/test/core/network/base_provider_test.dart @@ -16,11 +16,16 @@ * along with this program. If not, see . */ +import 'dart:async'; +import 'dart:convert'; +import 'dart:io'; + import 'package:flutter_test/flutter_test.dart'; import 'package:http/http.dart' as http; import 'package:http/http.dart'; import 'package:mockito/annotations.dart'; import 'package:mockito/mockito.dart'; +import 'package:wger/core/exceptions/http_exception.dart'; import 'package:wger/core/network/base_provider.dart'; import '../../fixtures/fixture_reader.dart'; @@ -148,4 +153,181 @@ void main() { expect(data[4], {'id': 5, 'value': 'And then steal his helmet.'}); }); }); + + group('http methods', () { + late MockClient mockClient; + late WgerBaseProvider repo; + + setUp(() { + mockClient = MockClient(); + repo = buildTestBaseProvider(client: mockClient); + }); + + final uri = Uri.https('localhost', '/api/v2/endpoint/'); + + group('getDefaultHeaders', () { + test('returns Content-Type + User-Agent and never sets Authorization', () { + final headers = repo.getDefaultHeaders(); + + expect(headers[HttpHeaders.contentTypeHeader], contains('application/json')); + expect(headers[HttpHeaders.userAgentHeader], isNotNull); + // The Authorization header is injected by the AuthHttpClient layer, + // not built here. + expect(headers.containsKey(HttpHeaders.authorizationHeader), isFalse); + expect(headers.containsKey(HttpHeaders.acceptLanguageHeader), isFalse); + }); + + test('language sets Accept-Language', () { + final headers = repo.getDefaultHeaders(language: 'de'); + + expect(headers[HttpHeaders.acceptLanguageHeader], 'de'); + }); + }); + + group('fetch', () { + test('returns the parsed body on 200', () async { + when(mockClient.get(uri, headers: anyNamed('headers'))).thenAnswer( + (_) async => Response(jsonEncode({'value': 42}), 200), + ); + + final result = await repo.fetch(uri); + + expect(result, {'value': 42}); + }); + + test('throws WgerHttpException on a non-retried 4xx', () async { + when( + mockClient.get(uri, headers: anyNamed('headers')), + ).thenAnswer((_) async => Response('not found', 404)); + + expect(repo.fetch(uri), throwsA(isA())); + }); + + test('retries on a 5xx and eventually succeeds', () async { + var calls = 0; + when(mockClient.get(uri, headers: anyNamed('headers'))).thenAnswer((_) async { + calls++; + if (calls < 3) { + return Response('boom', 502); + } + return Response(jsonEncode({'ok': true}), 200); + }); + + final result = await repo.fetch( + uri, + initialDelay: const Duration(milliseconds: 1), + ); + + expect(result, {'ok': true}); + expect(calls, 3); + }); + + test('gives up and throws after maxRetries on persistent 5xx', () async { + when( + mockClient.get(uri, headers: anyNamed('headers')), + ).thenAnswer((_) async => Response('boom', 502)); + + expect( + repo.fetch( + uri, + maxRetries: 2, + initialDelay: const Duration(milliseconds: 1), + ), + throwsA(isA()), + ); + }); + + test('retries on TimeoutException and eventually succeeds', () async { + var calls = 0; + when(mockClient.get(uri, headers: anyNamed('headers'))).thenAnswer((_) async { + calls++; + if (calls < 2) { + throw TimeoutException('slow'); + } + return Response(jsonEncode({'ok': true}), 200); + }); + + final result = await repo.fetch( + uri, + initialDelay: const Duration(milliseconds: 1), + ); + + expect(result, {'ok': true}); + expect(calls, 2); + }); + + test('rethrows after maxRetries SocketExceptions', () async { + when( + mockClient.get(uri, headers: anyNamed('headers')), + ).thenThrow(const SocketException('no network')); + + expect( + repo.fetch(uri, maxRetries: 1, initialDelay: const Duration(milliseconds: 1)), + throwsA(isA()), + ); + }); + }); + + group('post', () { + test('returns the parsed body on success', () async { + when( + mockClient.post(uri, headers: anyNamed('headers'), body: anyNamed('body')), + ).thenAnswer((_) async => Response(jsonEncode({'id': 7}), 201)); + + final result = await repo.post({'name': 'foo'}, uri); + + expect(result['id'], 7); + }); + + test('throws WgerHttpException on >=400', () async { + when( + mockClient.post(uri, headers: anyNamed('headers'), body: anyNamed('body')), + ).thenAnswer((_) async => Response('bad', 400)); + + expect(repo.post({}, uri), throwsA(isA())); + }); + }); + + group('patch', () { + test('returns the parsed body on success', () async { + when( + mockClient.patch(uri, headers: anyNamed('headers'), body: anyNamed('body')), + ).thenAnswer((_) async => Response(jsonEncode({'id': 7}), 200)); + + final result = await repo.patch({'name': 'foo'}, uri); + + expect(result['id'], 7); + }); + + test('throws WgerHttpException on >=400', () async { + when( + mockClient.patch(uri, headers: anyNamed('headers'), body: anyNamed('body')), + ).thenAnswer((_) async => Response('bad', 422)); + + expect(repo.patch({}, uri), throwsA(isA())); + }); + }); + + group('deleteRequest', () { + test('builds the URL via makeUrl and returns the response on success', () async { + final deleteUri = Uri.https('localhost', '/api/v2/endpoint/5/'); + when( + mockClient.delete(deleteUri, headers: anyNamed('headers')), + ).thenAnswer((_) async => Response('', 204)); + + final response = await repo.deleteRequest('endpoint', 5); + + expect(response.statusCode, 204); + }); + + test('throws WgerHttpException on >=400', () async { + final deleteUri = Uri.https('localhost', '/api/v2/endpoint/5/'); + when( + mockClient.delete(deleteUri, headers: anyNamed('headers')), + ).thenAnswer((_) async => Response('forbidden', 403)); + + expect(repo.deleteRequest('endpoint', 5), throwsA(isA())); + }); + }); + }); }