platform/lib/src/matchers.dart

41 lines
1 KiB
Dart
Raw Normal View History

2016-12-10 17:05:45 +00:00
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:matcher/matcher.dart';
/// Expects a given response, when parsed as JSON,
2016-12-10 18:11:27 +00:00
/// to equal a desired value.
Matcher isJson(value) => new _IsJson(value);
/// Expects a response to have the given status code.
Matcher hasStatus(int status) => new _HasStatus(status);
class _IsJson extends Matcher {
var value;
_IsJson(this.value);
@override
Description describe(Description description) {
2017-01-28 21:41:03 +00:00
return description.add('should equal the desired JSON response: $value');
2016-12-10 18:11:27 +00:00
}
@override
bool matches(http.Response item, Map matchState) =>
equals(value).matches(JSON.decode(item.body), matchState);
}
class _HasStatus extends Matcher {
int status;
_HasStatus(this.status);
@override
Description describe(Description description) {
2017-01-28 21:41:03 +00:00
return description.add('should have status code $status');
2016-12-10 18:11:27 +00:00
}
@override
bool matches(http.Response item, Map matchState) =>
equals(status).matches(item.statusCode, matchState);
}