platform/packages/framework/test/exception_test.dart

71 lines
2.3 KiB
Dart
Raw Normal View History

2017-06-29 22:17:42 +00:00
import 'package:angel_framework/angel_framework.dart';
2018-08-20 03:06:29 +00:00
import 'dart:convert';
2017-06-29 22:17:42 +00:00
import 'package:matcher/matcher.dart';
import 'package:test/test.dart';
main() {
test('named constructors', () {
2019-05-02 22:48:31 +00:00
expect(
AngelHttpException.badRequest(), isException(400, '400 Bad Request'));
expect(AngelHttpException.notAuthenticated(),
2018-06-23 03:29:38 +00:00
isException(401, '401 Not Authenticated'));
2019-05-02 22:48:31 +00:00
expect(AngelHttpException.paymentRequired(),
2018-06-23 03:29:38 +00:00
isException(402, '402 Payment Required'));
2019-05-02 22:48:31 +00:00
expect(AngelHttpException.forbidden(), isException(403, '403 Forbidden'));
expect(AngelHttpException.notFound(), isException(404, '404 Not Found'));
expect(AngelHttpException.methodNotAllowed(),
2018-06-23 03:29:38 +00:00
isException(405, '405 Method Not Allowed'));
2019-05-02 22:48:31 +00:00
expect(AngelHttpException.notAcceptable(),
2018-06-23 03:29:38 +00:00
isException(406, '406 Not Acceptable'));
2019-05-02 22:48:31 +00:00
expect(AngelHttpException.methodTimeout(), isException(408, '408 Timeout'));
expect(AngelHttpException.conflict(), isException(409, '409 Conflict'));
expect(AngelHttpException.notProcessable(),
2017-06-29 22:17:42 +00:00
isException(422, '422 Not Processable'));
2019-05-02 22:48:31 +00:00
expect(AngelHttpException.notImplemented(),
2018-06-23 03:29:38 +00:00
isException(501, '501 Not Implemented'));
2019-05-02 22:48:31 +00:00
expect(
AngelHttpException.unavailable(), isException(503, '503 Unavailable'));
2017-06-29 22:17:42 +00:00
});
test('fromMap', () {
2019-05-02 22:48:31 +00:00
expect(AngelHttpException.fromMap({'status_code': -1, 'message': 'ok'}),
2017-06-29 22:17:42 +00:00
isException(-1, 'ok'));
});
test('toMap = toJson', () {
2019-05-02 22:48:31 +00:00
var exc = AngelHttpException.badRequest();
2017-06-29 22:17:42 +00:00
expect(exc.toMap(), exc.toJson());
2018-06-08 07:06:26 +00:00
var json_ = json.encode(exc.toJson());
2019-05-02 22:48:31 +00:00
var exc2 = AngelHttpException.fromJson(json_);
2017-06-29 22:17:42 +00:00
expect(exc2.toJson(), exc.toJson());
});
test('toString', () {
expect(
2019-05-02 22:48:31 +00:00
AngelHttpException(null, statusCode: 420, message: 'Blaze It')
2017-06-29 22:17:42 +00:00
.toString(),
'420: Blaze It');
});
}
Matcher isException(int statusCode, String message) =>
2019-05-02 22:48:31 +00:00
_IsException(statusCode, message);
2017-06-29 22:17:42 +00:00
class _IsException extends Matcher {
final int statusCode;
final String message;
_IsException(this.statusCode, this.message);
@override
Description describe(Description description) =>
description.add('has status code $statusCode and message "$message"');
@override
2018-06-08 07:06:26 +00:00
bool matches(item, Map matchState) {
return item is AngelHttpException &&
item.statusCode == statusCode &&
item.message == message;
2017-06-29 22:17:42 +00:00
}
}