Chromium Code Reviews| 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_change.dart'; | |
| 14 import 'test_util.dart'; | |
| 15 | |
| 13 void main() { | 16 void main() { |
| 14 group("readAsString", () { | 17 group("supports a String body", () { |
| 15 test("supports a null body", () { | 18 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"); | 19 var response = new Response.ok("hello, world"); |
| 22 expect(response.readAsString(), completion(equals("hello, world"))); | 20 expect(response.readAsString(), completion(equals("hello, world"))); |
| 23 }); | 21 }); |
| 24 | 22 |
| 25 test("supports a Stream<List<int>> body", () { | 23 test("read", () { |
| 26 var controller = new StreamController(); | 24 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 | 25 |
| 30 controller.add([104, 101, 108, 108, 111, 44]); | 26 var response = new Response.ok("hello, world"); |
| 31 return new Future(() { | 27 expect(response.read().toList(), completion(equals([helloWorldBytes]))); |
| 32 controller | |
| 33 ..add([32, 119, 111, 114, 108, 100]) | |
| 34 ..close(); | |
| 35 }); | |
| 36 }); | 28 }); |
| 37 }); | 29 }); |
| 38 | 30 |
| 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", () { | 31 group("new Response", () { |
| 70 test("defaults to encoding a String as UTF-8", () { | 32 test("defaults to encoding a String as UTF-8", () { |
| 71 expect(new Response.ok("è").read().toList(), | 33 expect(new Response.ok("è").read().toList(), |
| 72 completion(equals([[195, 168]]))); | 34 completion(equals([[195, 168]]))); |
| 73 }); | 35 }); |
| 74 | 36 |
| 75 test("uses the explicit encoding if available", () { | 37 test("uses the explicit encoding if available", () { |
| 76 expect(new Response.ok("è", encoding: LATIN1).read().toList(), | 38 expect(new Response.ok("è", encoding: LATIN1).read().toList(), |
| 77 completion(equals([[232]]))); | 39 completion(equals([[232]]))); |
| 78 }); | 40 }); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 150 test("is null without a Last-Modified header", () { | 112 test("is null without a Last-Modified header", () { |
| 151 expect(new Response.ok("okay!").lastModified, isNull); | 113 expect(new Response.ok("okay!").lastModified, isNull); |
| 152 }); | 114 }); |
| 153 | 115 |
| 154 test("comes from the Last-Modified header", () { | 116 test("comes from the Last-Modified header", () { |
| 155 expect(new Response.ok("okay!", headers: { | 117 expect(new Response.ok("okay!", headers: { |
| 156 'last-modified': 'Sun, 06 Nov 1994 08:49:37 GMT' | 118 'last-modified': 'Sun, 06 Nov 1994 08:49:37 GMT' |
| 157 }).lastModified, equals(DateTime.parse("1994-11-06 08:49:37z"))); | 119 }).lastModified, equals(DateTime.parse("1994-11-06 08:49:37z"))); |
| 158 }); | 120 }); |
| 159 }); | 121 }); |
| 122 | |
| 123 group('change', () { | |
| 124 test('with no arguments returns instance with equal values', () { | |
|
nweiz
2014/04/29 19:48:48
Why isn't this part of [testChange]?
kevmoo
2014/04/30 14:27:52
Because I'm ensuring that all of the non-common fi
| |
| 125 var controller = new StreamController(); | |
| 126 | |
| 127 var request = new Response(345, body: 'hèllo, world', encoding: LATIN1, | |
| 128 headers: {'header1': 'header value 1'}, | |
| 129 context: {'context1': 'context value 1'}); | |
| 130 | |
| 131 var copy = request.change(); | |
| 132 | |
| 133 expect(copy.statusCode, request.statusCode); | |
| 134 expect(copy.readAsString(), completion('hèllo, world')); | |
| 135 expect(copy.headers, same(request.headers)); | |
| 136 expect(copy.encoding, request.encoding); | |
| 137 expect(copy.context, same(request.context)); | |
| 138 | |
| 139 controller.add(HELLO_BYTES); | |
| 140 return new Future(() { | |
| 141 controller | |
| 142 ..add(WORLD_BYTES) | |
| 143 ..close(); | |
| 144 }); | |
| 145 }); | |
| 146 | |
| 147 testChange(({headers, context}) { | |
| 148 return new Response.ok(null, headers: headers, | |
| 149 context: context); | |
| 150 }); | |
| 151 }); | |
| 160 } | 152 } |
| OLD | NEW |