| 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' hide Request; | 10 import 'package:shelf/shelf.dart' hide Request; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 completion(equals([[195, 168]]))); | 33 completion(equals([[195, 168]]))); |
| 34 }); | 34 }); |
| 35 | 35 |
| 36 test("uses the explicit encoding if available", () { | 36 test("uses the explicit encoding if available", () { |
| 37 expect(new Response.ok("è", encoding: LATIN1).read().toList(), | 37 expect(new Response.ok("è", encoding: LATIN1).read().toList(), |
| 38 completion(equals([[232]]))); | 38 completion(equals([[232]]))); |
| 39 }); | 39 }); |
| 40 | 40 |
| 41 test("adds an explicit encoding to the content-type", () { | 41 test("adds an explicit encoding to the content-type", () { |
| 42 var response = new Response.ok("è", | 42 var response = new Response.ok("è", |
| 43 encoding: LATIN1, | 43 encoding: LATIN1, headers: {'content-type': 'text/plain'}); |
| 44 headers: {'content-type': 'text/plain'}); | |
| 45 expect(response.headers, | 44 expect(response.headers, |
| 46 containsPair('content-type', 'text/plain; charset=iso-8859-1')); | 45 containsPair('content-type', 'text/plain; charset=iso-8859-1')); |
| 47 }); | 46 }); |
| 48 | 47 |
| 49 test("sets an absent content-type to application/octet-stream in order to " | 48 test("sets an absent content-type to application/octet-stream in order to " |
| 50 "set the charset", () { | 49 "set the charset", () { |
| 51 var response = new Response.ok("è", encoding: LATIN1); | 50 var response = new Response.ok("è", encoding: LATIN1); |
| 52 expect(response.headers, containsPair('content-type', | 51 expect(response.headers, containsPair( |
| 53 'application/octet-stream; charset=iso-8859-1')); | 52 'content-type', 'application/octet-stream; charset=iso-8859-1')); |
| 54 }); | 53 }); |
| 55 | 54 |
| 56 test("overwrites an existing charset if given an explicit encoding", () { | 55 test("overwrites an existing charset if given an explicit encoding", () { |
| 57 var response = new Response.ok("è", | 56 var response = new Response.ok("è", |
| 58 encoding: LATIN1, | 57 encoding: LATIN1, |
| 59 headers: {'content-type': 'text/plain; charset=whatever'}); | 58 headers: {'content-type': 'text/plain; charset=whatever'}); |
| 60 expect(response.headers, | 59 expect(response.headers, |
| 61 containsPair('content-type', 'text/plain; charset=iso-8859-1')); | 60 containsPair('content-type', 'text/plain; charset=iso-8859-1')); |
| 62 }); | 61 }); |
| 63 }); | 62 }); |
| 64 | 63 |
| 65 group("new Response.internalServerError without a body", () { | 64 group("new Response.internalServerError without a body", () { |
| 66 test('sets the body to "Internal Server Error"', () { | 65 test('sets the body to "Internal Server Error"', () { |
| 67 var response = new Response.internalServerError(); | 66 var response = new Response.internalServerError(); |
| 68 expect(response.readAsString(), | 67 expect( |
| 69 completion(equals("Internal Server Error"))); | 68 response.readAsString(), completion(equals("Internal Server Error"))); |
| 70 }); | 69 }); |
| 71 | 70 |
| 72 test('sets the content-type header to text/plain', () { | 71 test('sets the content-type header to text/plain', () { |
| 73 var response = new Response.internalServerError(); | 72 var response = new Response.internalServerError(); |
| 74 expect(response.headers, containsPair('content-type', 'text/plain')); | 73 expect(response.headers, containsPair('content-type', 'text/plain')); |
| 75 }); | 74 }); |
| 76 | 75 |
| 77 test('preserves content-type parameters', () { | 76 test('preserves content-type parameters', () { |
| 78 var response = new Response.internalServerError(headers: { | 77 var response = new Response.internalServerError( |
| 78 headers: { |
| 79 'content-type': 'application/octet-stream; param=whatever' | 79 'content-type': 'application/octet-stream; param=whatever' |
| 80 }); | 80 }); |
| 81 expect(response.headers, | 81 expect(response.headers, |
| 82 containsPair('content-type', 'text/plain; param=whatever')); | 82 containsPair('content-type', 'text/plain; param=whatever')); |
| 83 }); | 83 }); |
| 84 }); | 84 }); |
| 85 | 85 |
| 86 group("Response redirect", () { | 86 group("Response redirect", () { |
| 87 test("sets the location header for a String", () { | 87 test("sets the location header for a String", () { |
| 88 var response = new Response.found('/foo'); | 88 var response = new Response.found('/foo'); |
| 89 expect(response.headers, containsPair('location', '/foo')); | 89 expect(response.headers, containsPair('location', '/foo')); |
| 90 }); | 90 }); |
| 91 | 91 |
| 92 test("sets the location header for a Uri", () { | 92 test("sets the location header for a Uri", () { |
| 93 var response = new Response.found(new Uri(path: '/foo')); | 93 var response = new Response.found(new Uri(path: '/foo')); |
| 94 expect(response.headers, containsPair('location', '/foo')); | 94 expect(response.headers, containsPair('location', '/foo')); |
| 95 }); | 95 }); |
| 96 }); | 96 }); |
| 97 | 97 |
| 98 group("expires", () { | 98 group("expires", () { |
| 99 test("is null without an Expires header", () { | 99 test("is null without an Expires header", () { |
| 100 expect(new Response.ok("okay!").expires, isNull); | 100 expect(new Response.ok("okay!").expires, isNull); |
| 101 }); | 101 }); |
| 102 | 102 |
| 103 test("comes from the Expires header", () { | 103 test("comes from the Expires header", () { |
| 104 expect(new Response.ok("okay!", headers: { | 104 expect(new Response.ok("okay!", |
| 105 headers: { |
| 105 'expires': 'Sun, 06 Nov 1994 08:49:37 GMT' | 106 'expires': 'Sun, 06 Nov 1994 08:49:37 GMT' |
| 106 }).expires, equals(DateTime.parse("1994-11-06 08:49:37z"))); | 107 }).expires, equals(DateTime.parse("1994-11-06 08:49:37z"))); |
| 107 }); | 108 }); |
| 108 }); | 109 }); |
| 109 | 110 |
| 110 group("lastModified", () { | 111 group("lastModified", () { |
| 111 test("is null without a Last-Modified header", () { | 112 test("is null without a Last-Modified header", () { |
| 112 expect(new Response.ok("okay!").lastModified, isNull); | 113 expect(new Response.ok("okay!").lastModified, isNull); |
| 113 }); | 114 }); |
| 114 | 115 |
| 115 test("comes from the Last-Modified header", () { | 116 test("comes from the Last-Modified header", () { |
| 116 expect(new Response.ok("okay!", headers: { | 117 expect(new Response.ok("okay!", |
| 118 headers: { |
| 117 'last-modified': 'Sun, 06 Nov 1994 08:49:37 GMT' | 119 'last-modified': 'Sun, 06 Nov 1994 08:49:37 GMT' |
| 118 }).lastModified, equals(DateTime.parse("1994-11-06 08:49:37z"))); | 120 }).lastModified, equals(DateTime.parse("1994-11-06 08:49:37z"))); |
| 119 }); | 121 }); |
| 120 }); | 122 }); |
| 121 | 123 |
| 122 group('change', () { | 124 group('change', () { |
| 123 test('with no arguments returns instance with equal values', () { | 125 test('with no arguments returns instance with equal values', () { |
| 124 var controller = new StreamController(); | 126 var controller = new StreamController(); |
| 125 | 127 |
| 126 var request = new Response(345, body: 'hèllo, world', encoding: LATIN1, | 128 var request = new Response(345, |
| 129 body: 'hèllo, world', |
| 130 encoding: LATIN1, |
| 127 headers: {'header1': 'header value 1'}, | 131 headers: {'header1': 'header value 1'}, |
| 128 context: {'context1': 'context value 1'}); | 132 context: {'context1': 'context value 1'}); |
| 129 | 133 |
| 130 var copy = request.change(); | 134 var copy = request.change(); |
| 131 | 135 |
| 132 expect(copy.statusCode, request.statusCode); | 136 expect(copy.statusCode, request.statusCode); |
| 133 expect(copy.readAsString(), completion('hèllo, world')); | 137 expect(copy.readAsString(), completion('hèllo, world')); |
| 134 expect(copy.headers, same(request.headers)); | 138 expect(copy.headers, same(request.headers)); |
| 135 expect(copy.encoding, request.encoding); | 139 expect(copy.encoding, request.encoding); |
| 136 expect(copy.context, same(request.context)); | 140 expect(copy.context, same(request.context)); |
| 137 | 141 |
| 138 controller.add(HELLO_BYTES); | 142 controller.add(HELLO_BYTES); |
| 139 return new Future(() { | 143 return new Future(() { |
| 140 controller | 144 controller |
| 141 ..add(WORLD_BYTES) | 145 ..add(WORLD_BYTES) |
| 142 ..close(); | 146 ..close(); |
| 143 }); | 147 }); |
| 144 }); | 148 }); |
| 145 }); | 149 }); |
| 146 } | 150 } |
| OLD | NEW |