diff --git a/CHANGELOG.md b/CHANGELOG.md index d9f25bb4..53a14ead 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * Changed view engine to use `Map`. * Remove dependency on `package:json_god` by default. * Remove dependency on `package:dart2_constant`. +* Remove `contentType` argument in `ResponseContext.serialize`. # 1.1.5+1 * Patched annoying error that prevented MapServices from working, diff --git a/lib/src/core/response_context.dart b/lib/src/core/response_context.dart index cfced7c3..7bb0eb6c 100644 --- a/lib/src/core/response_context.dart +++ b/lib/src/core/response_context.dart @@ -2,7 +2,8 @@ library angel_framework.http.response_context; import 'dart:async'; import 'dart:convert' show Converter, Encoding; -import 'dart:io' show BytesBuilder, HttpResponse; +import 'dart:convert' as c show json; +import 'dart:io' show BytesBuilder, Cookie, HttpResponse; import 'package:angel_route/angel_route.dart'; import 'package:dart2_constant/convert.dart'; @@ -15,9 +16,6 @@ import '../http/http.dart'; import 'request_context.dart'; import 'server.dart' show Angel; -final RegExp _contentType = - new RegExp(r'([^/\n]+)\/\s*([^;\n]+)\s*(;\s*charset=([^$;\n]+))?'); - final RegExp _straySlashes = new RegExp(r'(^/+)|(/+$)'); /// Serializes response data into a String. @@ -81,7 +79,7 @@ abstract class ResponseContext implements StreamSink>, StringSink { /// ```dart /// app.injectSerializer(JSON.encode); /// ``` - String Function(dynamic) serializer = json.encode; + String Function(dynamic) serializer = c.json.encode; /// This response's status code. int get statusCode => _statusCode; @@ -169,7 +167,9 @@ abstract class ResponseContext implements StreamSink>, StringSink { } /// Serializes JSON to the response. - void json(value) => serialize(value, contentType: 'application/json'); + void json(value) => this + ..contentType = MediaType('application', 'json') + ..serialize(value); /// Returns a JSONP response. /// @@ -302,21 +302,11 @@ abstract class ResponseContext implements StreamSink>, StringSink { } /// Serializes data to the response. - /// - /// [contentType] can be either a [String], or a [ContentType]. - bool serialize(value, {contentType}) { + bool serialize(value) { if (!isOpen) throw closed(); - var text = serializer(value); - if (text.isEmpty) return true; - - if (contentType is String) - headers['content-type'] = contentType; - else if (contentType is ContentType) this.contentType = contentType; - write(text); - end(); return false; } diff --git a/lib/src/core/server.dart b/lib/src/core/server.dart index a0df148f..05f4cea2 100644 --- a/lib/src/core/server.dart +++ b/lib/src/core/server.dart @@ -4,6 +4,7 @@ import 'dart:async'; import 'dart:collection' show HashMap; import 'dart:convert'; import 'dart:io'; + import 'package:angel_container/angel_container.dart'; import 'package:angel_container/mirrors.dart'; import 'package:angel_http_exception/angel_http_exception.dart'; @@ -11,6 +12,7 @@ import 'package:angel_route/angel_route.dart'; import 'package:combinator/combinator.dart'; import 'package:logging/logging.dart'; import 'package:tuple/tuple.dart'; + import '../http/http.dart'; import 'angel_base.dart'; import 'request_context.dart'; @@ -305,8 +307,7 @@ class Angel extends AngelBase { else if (result is bool) { return result; } else if (result != null) { - return res.serialize(result, - contentType: res.headers['content-type'] ?? 'application/json'); + return res.serialize(result); } else return res.isOpen; }); diff --git a/lib/src/core/typed_service.dart b/lib/src/core/typed_service.dart index f3a5ba81..d352deec 100644 --- a/lib/src/core/typed_service.dart +++ b/lib/src/core/typed_service.dart @@ -1,5 +1,6 @@ import 'dart:async'; import 'dart:mirrors'; + import '../../common.dart'; import 'service.dart'; @@ -54,9 +55,11 @@ class TypedService extends Service { } serialize(x) { - if (x is Model) - return json.encodeObject(x); - else if (x is Map) + // TODO: Custom serializer, i.e. json_god? +// if (x is Model) +// return json.encodeObject(x); +// else + if (x is Map) return x; else if (x is Iterable) return x.map(serialize).toList(); diff --git a/test/hooked_test.dart b/test/hooked_test.dart index 677deb8c..539ac1c9 100644 --- a/test/hooked_test.dart +++ b/test/hooked_test.dart @@ -1,3 +1,4 @@ +import 'dart:convert'; import 'dart:io'; import 'package:angel_container/mirrors.dart'; import 'package:angel_framework/angel_framework.dart'; @@ -74,7 +75,7 @@ main() { body: json.encode({"arbitrary": "data"}), headers: headers as Map); print(response.body); - Map result = god.deserialize(response.body); + Map result = json.decode(response.body); expect(result["hello"], equals("hooked world")); }); @@ -92,7 +93,7 @@ main() { var response = await client.get("$url/todos"); print(response.body); - List result = god.deserialize(response.body); + List result = json.decode(response.body); expect(result[0]["angel"], equals("framework")); }); @@ -115,7 +116,7 @@ main() { var response = await client.get('$url/books'); print(response.body); - var result = god.deserialize(response.body); + var result = json.decode(response.body); expect(result, isList); expect(result, isNotEmpty); expect(result[0], equals({'foo': 'bar'})); diff --git a/test/serialize_test.dart b/test/serialize_test.dart index 2e027d30..48ee42f3 100644 --- a/test/serialize_test.dart +++ b/test/serialize_test.dart @@ -1,7 +1,9 @@ import 'dart:io'; + import 'package:angel_container/mirrors.dart'; import 'package:angel_framework/angel_framework.dart'; import 'package:http/http.dart' as http; +import 'package:http_parser/http_parser.dart'; import 'package:test/test.dart'; main() { @@ -13,10 +15,10 @@ main() { setUp(() async { app = new Angel(MirrorsReflector()) ..get('/foo', () => {'hello': 'world'}) - ..get( - '/bar', - (req, ResponseContext res) => - res.serialize({'hello': 'world'}, contentType: 'text/html')); + ..get('/bar', (req, ResponseContext res) async { + res.contentType = new MediaType('text', 'html'); + await res.serialize({'hello': 'world'}); + }); client = new http.Client(); server = await new AngelHttp(app).startServer(); diff --git a/test/services_test.dart b/test/services_test.dart index 2f0a8714..7884ed85 100644 --- a/test/services_test.dart +++ b/test/services_test.dart @@ -54,9 +54,9 @@ main() { var response = await client.post("$url/todos", headers: headers as Map, body: postData); expect(response.statusCode, 201); - var json = god.deserialize(response.body); - print(json); - expect(json['text'], equals('Hello, world!')); + var jsons = json.decode(response.body); + print(jsons); + expect(jsons['text'], equals('Hello, world!')); }); test('can fetch data', () async { @@ -65,9 +65,9 @@ main() { headers: headers as Map, body: postData); var response = await client.get("$url/todos/0"); expect(response.statusCode, 200); - var json = god.deserialize(response.body); - print(json); - expect(json['text'], equals('Hello, world!')); + var jsons = json.decode(response.body); + print(jsons); + expect(jsons['text'], equals('Hello, world!')); }); test('can modify data', () async { @@ -78,9 +78,9 @@ main() { var response = await client.patch("$url/todos/0", headers: headers as Map, body: postData); expect(response.statusCode, 200); - var json = god.deserialize(response.body); - print(json); - expect(json['text'], equals('modified')); + var jsons = json.decode(response.body); + print(jsons); + expect(jsons['text'], equals('modified')); }); test('can overwrite data', () async { @@ -91,10 +91,10 @@ main() { var response = await client.post("$url/todos/0", headers: headers as Map, body: postData); expect(response.statusCode, 200); - var json = god.deserialize(response.body); - print(json); - expect(json['text'], equals(null)); - expect(json['over'], equals('write')); + var jsons = json.decode(response.body); + print(jsons); + expect(jsons['text'], equals(null)); + expect(jsons['over'], equals('write')); }); test('can delete data', () async { @@ -105,7 +105,7 @@ main() { .then((r) => json.decode(r.body)); var response = await client.delete("$url/todos/${created['id']}"); expect(response.statusCode, 200); - var json_ = god.deserialize(response.body); + var json_ = json.decode(response.body); print(json_); expect(json_['text'], equals('Hello, world!')); });