| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library shelf.response_test; | 5 library shelf.response_test; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 | 9 |
| 10 import 'package:shelf/shelf.dart'; | 10 import 'package:shelf/shelf.dart' hide Request; |
| 11 import 'package:unittest/unittest.dart'; | 11 import 'package:unittest/unittest.dart'; |
| 12 | 12 |
| 13 import 'test_util.dart'; |
| 14 |
| 13 void main() { | 15 void main() { |
| 14 group("readAsString", () { | 16 group("supports a String body", () { |
| 15 test("supports a null body", () { | 17 test("readAsString", () { |
| 16 var response = new Response(200); | |
| 17 expect(response.readAsString(), completion(equals(""))); | |
| 18 }); | |
| 19 | |
| 20 test("supports a String body", () { | |
| 21 var response = new Response.ok("hello, world"); | 18 var response = new Response.ok("hello, world"); |
| 22 expect(response.readAsString(), completion(equals("hello, world"))); | 19 expect(response.readAsString(), completion(equals("hello, world"))); |
| 23 }); | 20 }); |
| 24 | 21 |
| 25 test("supports a Stream<List<int>> body", () { | 22 test("read", () { |
| 26 var controller = new StreamController(); | 23 var helloWorldBytes = new List.from(HELLO_BYTES)..addAll(WORLD_BYTES); |
| 27 var response = new Response.ok(controller.stream); | |
| 28 expect(response.readAsString(), completion(equals("hello, world"))); | |
| 29 | 24 |
| 30 controller.add([104, 101, 108, 108, 111, 44]); | 25 var response = new Response.ok("hello, world"); |
| 31 return new Future(() { | 26 expect(response.read().toList(), completion(equals([helloWorldBytes]))); |
| 32 controller | |
| 33 ..add([32, 119, 111, 114, 108, 100]) | |
| 34 ..close(); | |
| 35 }); | |
| 36 }); | 27 }); |
| 37 }); | 28 }); |
| 38 | 29 |
| 39 group("read", () { | |
| 40 test("supports a null body", () { | |
| 41 var response = new Response(200); | |
| 42 expect(response.read().toList(), completion(isEmpty)); | |
| 43 }); | |
| 44 | |
| 45 test("supports a String body", () { | |
| 46 var response = new Response.ok("hello, world"); | |
| 47 expect(response.read().toList(), completion(equals([[ | |
| 48 104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100 | |
| 49 ]]))); | |
| 50 }); | |
| 51 | |
| 52 test("supports a Stream<List<int>> body", () { | |
| 53 var controller = new StreamController(); | |
| 54 var response = new Response.ok(controller.stream); | |
| 55 expect(response.read().toList(), completion(equals([ | |
| 56 [104, 101, 108, 108, 111, 44], | |
| 57 [32, 119, 111, 114, 108, 100] | |
| 58 ]))); | |
| 59 | |
| 60 controller.add([104, 101, 108, 108, 111, 44]); | |
| 61 return new Future(() { | |
| 62 controller | |
| 63 ..add([32, 119, 111, 114, 108, 100]) | |
| 64 ..close(); | |
| 65 }); | |
| 66 }); | |
| 67 }); | |
| 68 | |
| 69 group("new Response", () { | 30 group("new Response", () { |
| 70 test("defaults to encoding a String as UTF-8", () { | 31 test("defaults to encoding a String as UTF-8", () { |
| 71 expect(new Response.ok("è").read().toList(), | 32 expect(new Response.ok("è").read().toList(), |
| 72 completion(equals([[195, 168]]))); | 33 completion(equals([[195, 168]]))); |
| 73 }); | 34 }); |
| 74 | 35 |
| 75 test("uses the explicit encoding if available", () { | 36 test("uses the explicit encoding if available", () { |
| 76 expect(new Response.ok("è", encoding: LATIN1).read().toList(), | 37 expect(new Response.ok("è", encoding: LATIN1).read().toList(), |
| 77 completion(equals([[232]]))); | 38 completion(equals([[232]]))); |
| 78 }); | 39 }); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 test("is null without a Last-Modified header", () { | 111 test("is null without a Last-Modified header", () { |
| 151 expect(new Response.ok("okay!").lastModified, isNull); | 112 expect(new Response.ok("okay!").lastModified, isNull); |
| 152 }); | 113 }); |
| 153 | 114 |
| 154 test("comes from the Last-Modified header", () { | 115 test("comes from the Last-Modified header", () { |
| 155 expect(new Response.ok("okay!", headers: { | 116 expect(new Response.ok("okay!", headers: { |
| 156 'last-modified': 'Sun, 06 Nov 1994 08:49:37 GMT' | 117 'last-modified': 'Sun, 06 Nov 1994 08:49:37 GMT' |
| 157 }).lastModified, equals(DateTime.parse("1994-11-06 08:49:37z"))); | 118 }).lastModified, equals(DateTime.parse("1994-11-06 08:49:37z"))); |
| 158 }); | 119 }); |
| 159 }); | 120 }); |
| 121 |
| 122 group('change', () { |
| 123 test('with no arguments returns instance with equal values', () { |
| 124 var controller = new StreamController(); |
| 125 |
| 126 var request = new Response(345, body: 'hèllo, world', encoding: LATIN1, |
| 127 headers: {'header1': 'header value 1'}, |
| 128 context: {'context1': 'context value 1'}); |
| 129 |
| 130 var copy = request.change(); |
| 131 |
| 132 expect(copy.statusCode, request.statusCode); |
| 133 expect(copy.readAsString(), completion('hèllo, world')); |
| 134 expect(copy.headers, same(request.headers)); |
| 135 expect(copy.encoding, request.encoding); |
| 136 expect(copy.context, same(request.context)); |
| 137 |
| 138 controller.add(HELLO_BYTES); |
| 139 return new Future(() { |
| 140 controller |
| 141 ..add(WORLD_BYTES) |
| 142 ..close(); |
| 143 }); |
| 144 }); |
| 145 }); |
| 160 } | 146 } |
| OLD | NEW |