Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ abstract class MeiliSearchClient {
/// Finds index by matching [uid]. If index is not exists tries to create a
/// new index.
Future<MeiliSearchIndex> getOrCreateIndex(String uid, {String primaryKey});

/// Return health of the MeiliSearch server.
/// Throws an error if containing details if MeiliSearch can't process your request.
Future<Map<String, dynamic>> health();

/// Get health of the MeiliSearch server.
/// Return true or false.
Future<bool> isHealthy();
}
17 changes: 17 additions & 0 deletions lib/src/client_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,21 @@ class MeiliSearchClientImpl implements MeiliSearchClient {
return await createIndex(uid, primaryKey: primaryKey);
}
}

@override
Future<Map<String, dynamic>> health() async {
final response = await http.getMethod<Map<String, dynamic>>('/health');

return response.data;
}

@override
Future<bool> isHealthy() async {
try {
await health();
} on Exception catch (_) {
return false;
}
return true;
}
}
31 changes: 31 additions & 0 deletions test/health_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:test/test.dart';

import 'utils/client.dart';

void main() {
group('Health', () {
setUpClient();

test('of the server when the url is valid', () async {
var health = await client.health();
expect(health, {'status': 'available'});
});

test('of the server when the url is valid with isHealthy', () async {
var health = await client.isHealthy();
expect(health, true);
});
});
group('Health Fail', () {
setUpClientWithWrongUrl();

test('when the url is not valid', () async {
expect(client.health(), throwsException);
});

test('when the url is not valid with isHealthy', () async {
var health = await client.isHealthy();
expect(health, false);
});
});
}
10 changes: 10 additions & 0 deletions test/utils/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ Future<void> setUpHttp() async {
});
}

Future<void> setUpClientWithWrongUrl() async {
setUp(() {
var server = 'http://wrongurl:1234';

print('Using wrong url server on $server for running tests.');

client = MeiliSearchClient(server, 'masterKey');
});
}

String randomUid([String prefix = 'index']) {
return '${prefix}_${random.nextInt(9999)}';
}
Expand Down