Skip to content

Commit 682c6dd

Browse files
authored
feat: support custom base urls (#33)
1 parent 29af6ec commit 682c6dd

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

lib/src/pub_updater.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,20 @@ class PackageInfoRequestFailure implements Exception {}
1111
/// Exception thrown when the provided package information is not found.
1212
class PackageInfoNotFoundFailure implements Exception {}
1313

14+
/// The pub.dev base url for querying package versions
15+
const _defaultBaseUrl = 'https://pub.dev/api/packages/';
16+
1417
/// {@template pub_update}
1518
/// A Dart package which enables checking whether a package is up to date.
1619
/// {@endtemplate}
1720
class PubUpdater {
1821
/// {@macro pub_update}
19-
PubUpdater([http.Client? client]) : _client = client;
22+
PubUpdater([http.Client? client, String baseUrl = _defaultBaseUrl])
23+
: _client = client,
24+
_baseUrl = baseUrl;
2025

21-
/// The pub.dev base url for querying package versions
22-
static const _baseUrl = 'https://pub.dev/api/packages/';
2326
final http.Client? _client;
27+
final String _baseUrl;
2428

2529
Future<http.Response> _get(Uri uri) => _client?.get(uri) ?? http.get(uri);
2630

test/pub_update_test.dart

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@ const emptyResponseBody = '{}';
2121

2222
const command = ['dart', 'pub', 'global', 'activate', 'very_good_cli'];
2323

24+
const customBaseUrl = 'https://custom-domain.com/api/packages/';
25+
2426
void main() {
2527
group('PubUpdater', () {
2628
late Client client;
2729
late Response response;
2830
late PubUpdater pubUpdater;
2931
late ProcessManager processManager;
32+
3033
setUpAll(() {
3134
registerFallbackValue(Uri());
3235
});
@@ -49,6 +52,10 @@ void main() {
4952
expect(PubUpdater(), isNotNull);
5053
});
5154

55+
test('can be instantiated with a custom base url', () {
56+
expect(PubUpdater(null, customBaseUrl), isNotNull);
57+
});
58+
5259
group('isUpToDate', () {
5360
test('makes correct http request', () async {
5461
when(() => response.body).thenReturn(emptyResponseBody);
@@ -70,6 +77,24 @@ void main() {
7077
).called(1);
7178
});
7279

80+
test('makes correct http request with a custom base url', () async {
81+
when(() => response.body).thenReturn(emptyResponseBody);
82+
pubUpdater = PubUpdater(client, customBaseUrl);
83+
84+
try {
85+
await pubUpdater.isUpToDate(
86+
packageName: 'very_good_cli',
87+
currentVersion: '0.3.3',
88+
);
89+
} catch (_) {}
90+
91+
verify(
92+
() => client.get(
93+
Uri.parse('${customBaseUrl}very_good_cli'),
94+
),
95+
).called(1);
96+
});
97+
7398
test('returns false when currentVersion < latestVersion', () async {
7499
expect(
75100
await pubUpdater.isUpToDate(
@@ -132,6 +157,21 @@ void main() {
132157
).called(1);
133158
});
134159

160+
test('makes correct http request with a custom base url', () async {
161+
when(() => response.body).thenReturn(emptyResponseBody);
162+
pubUpdater = PubUpdater(client, customBaseUrl);
163+
164+
try {
165+
await pubUpdater.getLatestVersion('very_good_cli');
166+
} catch (_) {}
167+
168+
verify(
169+
() => client.get(
170+
Uri.parse('${customBaseUrl}very_good_cli'),
171+
),
172+
).called(1);
173+
});
174+
135175
test('returns correct version', () async {
136176
when(() => response.body).thenReturn(validPackageInfoResponseBody);
137177
expect(

0 commit comments

Comments
 (0)