Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(90)

Unified Diff: pkg/shelf/test/message_test.dart

Issue 227563010: pkg/shelf: case-insensitive headers, cleaner Request ctor, a lot more tests (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: fixing dependent code, changelog Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/shelf/pubspec.yaml ('k') | pkg/shelf/test/request_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/shelf/test/message_test.dart
diff --git a/pkg/shelf/test/request_test.dart b/pkg/shelf/test/message_test.dart
similarity index 57%
copy from pkg/shelf/test/request_test.dart
copy to pkg/shelf/test/message_test.dart
index ce592c7c28a85eeaec4f5d1be66abb2c456e16c2..57f32c4afe0258ed661063219b4e06ed04cac819 100644
--- a/pkg/shelf/test/request_test.dart
+++ b/pkg/shelf/test/message_test.dart
@@ -2,59 +2,56 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-library shelf.request_test;
+library shelf.message_test;
import 'dart:async';
import 'dart:convert';
-import 'package:shelf/shelf.dart';
+import 'package:shelf/src/message.dart';
import 'package:unittest/unittest.dart';
-Request _request([Map<String, String> headers, Stream<List<int>> body]) {
- if (headers == null) headers = {};
- return new Request("/", "", "GET", "", "1.1", Uri.parse('http://localhost/'),
- headers, body: body);
+class _TestMessage extends Message {
+ _TestMessage(Map<String, String> headers, Stream<List<int>> body)
+ : super(body, headers: headers);
+}
+
+Message _createMessage({Map<String, String> headers, Stream<List<int>> body}) {
+ if (body == null) body = new Stream.fromIterable([]);
+ return new _TestMessage(headers, body);
}
void main() {
- group("contentLength", () {
- test("is null without a content-length header", () {
- var request = _request();
- expect(request.contentLength, isNull);
- });
+ group('headers', () {
+ test('message headers are case insensitive', () {
+ var message = _createMessage(headers: {'foo': 'bar'});
- test("comes from the content-length header", () {
- var request = _request({
- 'content-length': '42'
- });
- expect(request.contentLength, 42);
+ expect(message.headers, containsPair('foo', 'bar'));
+ expect(message.headers, containsPair('Foo', 'bar'));
+ expect(message.headers, containsPair('FOO', 'bar'));
});
- });
- group("ifModifiedSince", () {
- test("is null without an If-Modified-Since header", () {
- var request = _request();
- expect(request.ifModifiedSince, isNull);
+ test('null header value becomes empty, immutable', () {
+ var message = _createMessage();
+ expect(message.headers, isEmpty);
+ expect(() => message.headers['h1'] = 'value1', throwsUnsupportedError);
});
- test("comes from the Last-Modified header", () {
- var request = _request({
- 'if-modified-since': 'Sun, 06 Nov 1994 08:49:37 GMT'
- });
- expect(request.ifModifiedSince,
- equals(DateTime.parse("1994-11-06 08:49:37z")));
+ test('headers are immutable', () {
+ var message = _createMessage(headers: {'h1': 'value1'});
+ expect(() => message.headers['h1'] = 'value1', throwsUnsupportedError);
+ expect(() => message.headers['h1'] = 'value2', throwsUnsupportedError);
+ expect(() => message.headers['h2'] = 'value2', throwsUnsupportedError);
});
});
-
group("readAsString", () {
test("supports a null body", () {
- var request = _request();
+ var request = _createMessage();
expect(request.readAsString(), completion(equals("")));
});
test("supports a Stream<List<int>> body", () {
var controller = new StreamController();
- var request = _request({}, controller.stream);
+ var request = _createMessage(body: controller.stream);
expect(request.readAsString(), completion(equals("hello, world")));
controller.add([104, 101, 108, 108, 111, 44]);
@@ -66,32 +63,34 @@ void main() {
});
test("defaults to UTF-8", () {
- var request = _request({}, new Stream.fromIterable([[195, 168]]));
+ var request = _createMessage(body: new Stream.fromIterable([[195, 168]]));
expect(request.readAsString(), completion(equals("è")));
});
test("the content-type header overrides the default", () {
- var request = _request({'content-type': 'text/plain; charset=iso-8859-1'},
- new Stream.fromIterable([[195, 168]]));
+ var request = _createMessage(
+ headers: {'content-type': 'text/plain; charset=iso-8859-1'},
+ body: new Stream.fromIterable([[195, 168]]));
expect(request.readAsString(), completion(equals("è")));
});
test("an explicit encoding overrides the content-type header", () {
- var request = _request({'content-type': 'text/plain; charset=iso-8859-1'},
- new Stream.fromIterable([[195, 168]]));
+ var request = _createMessage(
+ headers: {'content-type': 'text/plain; charset=iso-8859-1'},
+ body: new Stream.fromIterable([[195, 168]]));
expect(request.readAsString(LATIN1), completion(equals("è")));
});
});
group("read", () {
test("supports a null body", () {
- var request = _request();
+ var request = _createMessage();
expect(request.read().toList(), completion(isEmpty));
});
test("supports a Stream<List<int>> body", () {
var controller = new StreamController();
- var request = _request({}, controller.stream);
+ var request = _createMessage(body: controller.stream);
expect(request.read().toList(), completion(equals([
[104, 101, 108, 108, 111, 44],
[32, 119, 111, 114, 108, 100]
@@ -106,19 +105,33 @@ void main() {
});
});
+ group("contentLength", () {
+ test("is null without a content-length header", () {
+ var request = _createMessage();
+ expect(request.contentLength, isNull);
+ });
+
+ test("comes from the content-length header", () {
+ var request = _createMessage(headers: {
+ 'content-length': '42'
+ });
+ expect(request.contentLength, 42);
+ });
+ });
+
group("mimeType", () {
test("is null without a content-type header", () {
- expect(_request().mimeType, isNull);
+ expect(_createMessage().mimeType, isNull);
});
test("comes from the content-type header", () {
- expect(_request({
+ expect(_createMessage(headers: {
'content-type': 'text/plain'
}).mimeType, equals('text/plain'));
});
test("doesn't include parameters", () {
- expect(_request({
+ expect(_createMessage(headers: {
'content-type': 'text/plain; foo=bar; bar=baz'
}).mimeType, equals('text/plain'));
});
@@ -126,23 +139,23 @@ void main() {
group("encoding", () {
test("is null without a content-type header", () {
- expect(_request().encoding, isNull);
+ expect(_createMessage().encoding, isNull);
});
test("is null without a charset parameter", () {
- expect(_request({
+ expect(_createMessage(headers: {
'content-type': 'text/plain'
}).encoding, isNull);
});
test("is null with an unrecognized charset parameter", () {
- expect(_request({
+ expect(_createMessage(headers: {
'content-type': 'text/plain; charset=fblthp'
}).encoding, isNull);
});
test("comes from the content-type charset parameter", () {
- expect(_request({
+ expect(_createMessage(headers: {
'content-type': 'text/plain; charset=iso-8859-1'
}).encoding, equals(LATIN1));
});
« no previous file with comments | « pkg/shelf/pubspec.yaml ('k') | pkg/shelf/test/request_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698