93 lines
2.3 KiB
Dart
93 lines
2.3 KiB
Dart
import 'dart:convert';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:dio/dio.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:jinzhi_flutter_app/features/mall/data/mall_auth_api.dart';
|
|
|
|
void main() {
|
|
test('sends an empty protobuf body when issuing a mall auth token', () async {
|
|
Object? requestData;
|
|
String? contentType;
|
|
final dio = Dio()
|
|
..interceptors.add(
|
|
InterceptorsWrapper(
|
|
onRequest: (options, handler) {
|
|
requestData = options.data;
|
|
contentType = options.contentType;
|
|
handler.resolve(
|
|
Response<List<int>>(
|
|
requestOptions: options,
|
|
statusCode: 200,
|
|
data: _validResponse(),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
|
|
final token = await MallAuthApi(dio).issueAppMallAuthToken();
|
|
|
|
expect(requestData, isA<Uint8List>());
|
|
expect(requestData, isEmpty);
|
|
expect(contentType, 'application/x-protobuf');
|
|
expect(token.authToken, 'mall-token');
|
|
expect(token.expiresIn, 300);
|
|
expect(token.sourceApp, 'jinzhi');
|
|
expect(token.scene, 'web_mall');
|
|
});
|
|
|
|
test('rejects an incomplete mall auth token response', () async {
|
|
final dio = Dio()
|
|
..interceptors.add(
|
|
InterceptorsWrapper(
|
|
onRequest: (options, handler) {
|
|
handler.resolve(
|
|
Response<List<int>>(
|
|
requestOptions: options,
|
|
statusCode: 200,
|
|
data: _stringField(1, 'mall-token'),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
|
|
await expectLater(
|
|
MallAuthApi(dio).issueAppMallAuthToken(),
|
|
throwsFormatException,
|
|
);
|
|
});
|
|
}
|
|
|
|
List<int> _validResponse() => <int>[
|
|
..._stringField(1, 'mall-token'),
|
|
..._integerField(2, 300),
|
|
..._stringField(3, 'jinzhi'),
|
|
..._stringField(4, 'web_mall'),
|
|
];
|
|
|
|
List<int> _stringField(int field, String value) {
|
|
final bytes = utf8.encode(value);
|
|
return <int>[
|
|
..._varint((field << 3) | 2),
|
|
..._varint(bytes.length),
|
|
...bytes,
|
|
];
|
|
}
|
|
|
|
List<int> _integerField(int field, int value) => <int>[
|
|
..._varint(field << 3),
|
|
..._varint(value),
|
|
];
|
|
|
|
List<int> _varint(int value) {
|
|
final bytes = <int>[];
|
|
while (value > 0x7f) {
|
|
bytes.add((value & 0x7f) | 0x80);
|
|
value >>= 7;
|
|
}
|
|
bytes.add(value);
|
|
return bytes;
|
|
}
|