decodeBody test

This commit is contained in:
Tobe O 2019-10-12 10:24:54 -04:00
parent 673754ac84
commit ed8817ff5e

View file

@ -69,6 +69,14 @@ void main() {
expect(todo.completed, false);
});
test('decodeBody', () async {
var req = await request(
asJson: true, bodyFields: {'text': 'Hey', 'complete': false});
var todo = await req.decodeBody(TodoCodec());
expect(todo.text, 'Hey');
expect(todo.completed, false);
});
test('throws when body has not been parsed', () async {
var req = await request(parse: false);
expect(() => req.bodyAsObject, throwsStateError);
@ -115,3 +123,16 @@ class Todo {
static Todo fromMap(Map m) =>
Todo(text: m['text'] as String, completed: m['complete'] as bool);
}
class TodoCodec extends Codec<Todo, Map> {
@override
Converter<Map, Todo> get decoder => TodoDecoder();
@override
Converter<Todo, Map> get encoder => throw UnsupportedError('no encoder');
}
class TodoDecoder extends Converter<Map, Todo> {
@override
Todo convert(Map input) => Todo.fromMap(input);
}