geolocation_test.dart
1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import 'dart:async';
import 'package:google_maps_webservice/geolocation.dart';
import 'package:test/test.dart';
Future<void> main() async {
final apiKey = 'MY_API_KEY';
var geolocation = GoogleMapsGeolocation(apiKey: apiKey);
tearDownAll(() {
geolocation.dispose();
});
group('Google Maps Geolocation', () {
group('build url (only api key, everything else over REST/JSON POST', () {
test('default url building with api key', () {
expect(
geolocation.buildUrl(),
'https://www.googleapis.com/geolocation/v1/geolocate?key=MY_API_KEY',
);
});
});
test('Decode response', () {
var response = GeolocationResponse.fromJson(_responseExample);
expect(response.isOkay, isTrue);
expect(response.location?.lat, 33.3632256);
expect(response.location?.lng, -117.0874871);
expect(response.accuracy, 20);
});
test('Decode response with error', () {
var response = GeolocationResponse.fromJson(_errorExample);
expect(response.isOkay, isFalse);
expect(response.error?.errors.length, 1);
expect(response.error?.code, 400);
expect(response.error?.message, 'Parse Error');
});
});
}
final _responseExample = {
'location': {'lat': 33.3632256, 'lng': -117.0874871},
'accuracy': 20
};
final _errorExample = {
'error': {
'errors': [
{
'domain': 'global',
'reason': 'parseError',
'message': 'Parse Error',
}
],
'code': 400,
'message': 'Parse Error'
}
};