diff --git a/lib/src/pub_updater.dart b/lib/src/pub_updater.dart index a30a166..d810e5c 100644 --- a/lib/src/pub_updater.dart +++ b/lib/src/pub_updater.dart @@ -11,16 +11,20 @@ class PackageInfoRequestFailure implements Exception {} /// Exception thrown when the provided package information is not found. class PackageInfoNotFoundFailure implements Exception {} +/// The pub.dev base url for querying package versions +const _defaultBaseUrl = 'https://pub.dev/api/packages/'; + /// {@template pub_update} /// A Dart package which enables checking whether a package is up to date. /// {@endtemplate} class PubUpdater { /// {@macro pub_update} - PubUpdater([http.Client? client]) : _client = client; + PubUpdater([http.Client? client, String baseUrl = _defaultBaseUrl]) + : _client = client, + _baseUrl = baseUrl; - /// The pub.dev base url for querying package versions - static const _baseUrl = 'https://pub.dev/api/packages/'; final http.Client? _client; + final String _baseUrl; Future _get(Uri uri) => _client?.get(uri) ?? http.get(uri); diff --git a/test/pub_update_test.dart b/test/pub_update_test.dart index 82e746e..81e8500 100644 --- a/test/pub_update_test.dart +++ b/test/pub_update_test.dart @@ -21,12 +21,15 @@ const emptyResponseBody = '{}'; const command = ['dart', 'pub', 'global', 'activate', 'very_good_cli']; +const customBaseUrl = 'https://custom-domain.com/api/packages/'; + void main() { group('PubUpdater', () { late Client client; late Response response; late PubUpdater pubUpdater; late ProcessManager processManager; + setUpAll(() { registerFallbackValue(Uri()); }); @@ -49,6 +52,10 @@ void main() { expect(PubUpdater(), isNotNull); }); + test('can be instantiated with a custom base url', () { + expect(PubUpdater(null, customBaseUrl), isNotNull); + }); + group('isUpToDate', () { test('makes correct http request', () async { when(() => response.body).thenReturn(emptyResponseBody); @@ -70,6 +77,24 @@ void main() { ).called(1); }); + test('makes correct http request with a custom base url', () async { + when(() => response.body).thenReturn(emptyResponseBody); + pubUpdater = PubUpdater(client, customBaseUrl); + + try { + await pubUpdater.isUpToDate( + packageName: 'very_good_cli', + currentVersion: '0.3.3', + ); + } catch (_) {} + + verify( + () => client.get( + Uri.parse('${customBaseUrl}very_good_cli'), + ), + ).called(1); + }); + test('returns false when currentVersion < latestVersion', () async { expect( await pubUpdater.isUpToDate( @@ -132,6 +157,21 @@ void main() { ).called(1); }); + test('makes correct http request with a custom base url', () async { + when(() => response.body).thenReturn(emptyResponseBody); + pubUpdater = PubUpdater(client, customBaseUrl); + + try { + await pubUpdater.getLatestVersion('very_good_cli'); + } catch (_) {} + + verify( + () => client.get( + Uri.parse('${customBaseUrl}very_good_cli'), + ), + ).called(1); + }); + test('returns correct version', () async { when(() => response.body).thenReturn(validPackageInfoResponseBody); expect(