From ed8817ff5ec02c91f90ec8bf7fc5c48e4119c795 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Sat, 12 Oct 2019 10:24:54 -0400 Subject: [PATCH] decodeBody test --- test/body_test.dart | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/body_test.dart b/test/body_test.dart index 43051770..45a64ba2 100644 --- a/test/body_test.dart +++ b/test/body_test.dart @@ -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 { + @override + Converter get decoder => TodoDecoder(); + + @override + Converter get encoder => throw UnsupportedError('no encoder'); +} + +class TodoDecoder extends Converter { + @override + Todo convert(Map input) => Todo.fromMap(input); +}