OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library shelf.response_test; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:convert'; |
| 9 |
| 10 import 'package:shelf/shelf.dart' hide Request; |
| 11 import 'package:test/test.dart'; |
| 12 |
| 13 import 'test_util.dart'; |
| 14 |
| 15 void main() { |
| 16 group("supports a String body", () { |
| 17 test("readAsString", () { |
| 18 var response = new Response.ok("hello, world"); |
| 19 expect(response.readAsString(), completion(equals("hello, world"))); |
| 20 }); |
| 21 |
| 22 test("read", () { |
| 23 var helloWorldBytes = new List.from(HELLO_BYTES)..addAll(WORLD_BYTES); |
| 24 |
| 25 var response = new Response.ok("hello, world"); |
| 26 expect(response.read().toList(), completion(equals([helloWorldBytes]))); |
| 27 }); |
| 28 }); |
| 29 |
| 30 group("new Response.internalServerError without a body", () { |
| 31 test('sets the body to "Internal Server Error"', () { |
| 32 var response = new Response.internalServerError(); |
| 33 expect( |
| 34 response.readAsString(), completion(equals("Internal Server Error"))); |
| 35 }); |
| 36 |
| 37 test('sets the content-type header to text/plain', () { |
| 38 var response = new Response.internalServerError(); |
| 39 expect(response.headers, containsPair('content-type', 'text/plain')); |
| 40 }); |
| 41 |
| 42 test('preserves content-type parameters', () { |
| 43 var response = new Response.internalServerError( |
| 44 headers: { |
| 45 'content-type': 'application/octet-stream; param=whatever' |
| 46 }); |
| 47 expect(response.headers, |
| 48 containsPair('content-type', 'text/plain; param=whatever')); |
| 49 }); |
| 50 }); |
| 51 |
| 52 group("Response redirect", () { |
| 53 test("sets the location header for a String", () { |
| 54 var response = new Response.found('/foo'); |
| 55 expect(response.headers, containsPair('location', '/foo')); |
| 56 }); |
| 57 |
| 58 test("sets the location header for a Uri", () { |
| 59 var response = new Response.found(new Uri(path: '/foo')); |
| 60 expect(response.headers, containsPair('location', '/foo')); |
| 61 }); |
| 62 }); |
| 63 |
| 64 group("expires", () { |
| 65 test("is null without an Expires header", () { |
| 66 expect(new Response.ok("okay!").expires, isNull); |
| 67 }); |
| 68 |
| 69 test("comes from the Expires header", () { |
| 70 expect(new Response.ok("okay!", |
| 71 headers: { |
| 72 'expires': 'Sun, 06 Nov 1994 08:49:37 GMT' |
| 73 }).expires, equals(DateTime.parse("1994-11-06 08:49:37z"))); |
| 74 }); |
| 75 }); |
| 76 |
| 77 group("lastModified", () { |
| 78 test("is null without a Last-Modified header", () { |
| 79 expect(new Response.ok("okay!").lastModified, isNull); |
| 80 }); |
| 81 |
| 82 test("comes from the Last-Modified header", () { |
| 83 expect(new Response.ok("okay!", |
| 84 headers: { |
| 85 'last-modified': 'Sun, 06 Nov 1994 08:49:37 GMT' |
| 86 }).lastModified, equals(DateTime.parse("1994-11-06 08:49:37z"))); |
| 87 }); |
| 88 }); |
| 89 |
| 90 group('change', () { |
| 91 test('with no arguments returns instance with equal values', () { |
| 92 var controller = new StreamController(); |
| 93 |
| 94 var request = new Response(345, |
| 95 body: 'hèllo, world', |
| 96 encoding: LATIN1, |
| 97 headers: {'header1': 'header value 1'}, |
| 98 context: {'context1': 'context value 1'}); |
| 99 |
| 100 var copy = request.change(); |
| 101 |
| 102 expect(copy.statusCode, request.statusCode); |
| 103 expect(copy.readAsString(), completion('hèllo, world')); |
| 104 expect(copy.headers, same(request.headers)); |
| 105 expect(copy.encoding, request.encoding); |
| 106 expect(copy.context, same(request.context)); |
| 107 |
| 108 controller.add(HELLO_BYTES); |
| 109 return new Future(() { |
| 110 controller |
| 111 ..add(WORLD_BYTES) |
| 112 ..close(); |
| 113 }); |
| 114 }); |
| 115 |
| 116 |
| 117 test("allows the original response to be read", () { |
| 118 var response = new Response.ok(null); |
| 119 var changed = response.change(); |
| 120 |
| 121 expect(response.read().toList(), completion(isEmpty)); |
| 122 expect(changed.read, throwsStateError); |
| 123 }); |
| 124 |
| 125 test("allows the changed response to be read", () { |
| 126 var response = new Response.ok(null); |
| 127 var changed = response.change(); |
| 128 |
| 129 expect(changed.read().toList(), completion(isEmpty)); |
| 130 expect(response.read, throwsStateError); |
| 131 }); |
| 132 |
| 133 test("allows another changed response to be read", () { |
| 134 var response = new Response.ok(null); |
| 135 var changed1 = response.change(); |
| 136 var changed2 = response.change(); |
| 137 |
| 138 expect(changed2.read().toList(), completion(isEmpty)); |
| 139 expect(changed1.read, throwsStateError); |
| 140 expect(response.read, throwsStateError); |
| 141 }); |
| 142 }); |
| 143 } |
OLD | NEW |