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) {
|
|
|
|
return description.add(' should equal the desired JSON response: $value');
|
|
|
|
}
|
|
|
|
|
|
|
|
@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) {
|
|
|
|
return description.add(' should have statuc code $status');
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool matches(http.Response item, Map matchState) =>
|
|
|
|
equals(status).matches(item.statusCode, matchState);
|
|
|
|
}
|