4
0

api_client_test.dart 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import 'dart:convert';
  2. import 'package:flutter_test/flutter_test.dart';
  3. import 'package:http/http.dart' as http;
  4. import 'package:http/testing.dart';
  5. import 'package:rackpeek_mobile/data/api_client.dart';
  6. void main() {
  7. group('serverUrlCandidates', () {
  8. test('infers HTTPS first and HTTP as a fallback', () {
  9. expect(serverUrlCandidates('192.168.1.10:8080'), [
  10. 'https://192.168.1.10:8080',
  11. 'http://192.168.1.10:8080',
  12. ]);
  13. });
  14. test('preserves an explicitly supplied scheme', () {
  15. expect(serverUrlCandidates('http://server.local:8080'), [
  16. 'http://server.local:8080',
  17. ]);
  18. });
  19. test('rejects malformed or non-HTTP addresses', () {
  20. expect(serverUrlCandidates(''), isEmpty);
  21. expect(serverUrlCandidates('ftp://server.local'), isEmpty);
  22. expect(serverUrlCandidates('https://'), isEmpty);
  23. });
  24. });
  25. test(
  26. 'status request reads API and app versions without sending an API key',
  27. () async {
  28. final client = MockClient((request) async {
  29. expect(request.url.path, '/api/status');
  30. expect(request.headers.containsKey('X-Api-Key'), isFalse);
  31. return http.Response(
  32. jsonEncode({'apiVersion': '1', 'appVersion': 'v2.3.0'}),
  33. 200,
  34. );
  35. });
  36. final status = await RackPeekApi(
  37. baseUrl: 'https://server.example.test',
  38. apiKey: '',
  39. client: client,
  40. ).getStatus();
  41. expect(status.apiVersion, '1');
  42. expect(status.appVersion, 'v2.3.0');
  43. },
  44. );
  45. }