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.message_change_test; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:convert'; |
| 9 |
| 10 import 'package:test/test.dart'; |
| 11 |
| 12 import 'package:shelf/shelf.dart'; |
| 13 import 'package:shelf/src/message.dart'; |
| 14 |
| 15 import 'test_util.dart'; |
| 16 |
| 17 void main() { |
| 18 group('Request', () { |
| 19 _testChange(({body, headers, context}) { |
| 20 return new Request('GET', LOCALHOST_URI, body: body, |
| 21 headers: headers, context: context); |
| 22 }); |
| 23 }); |
| 24 |
| 25 group('Response', () { |
| 26 _testChange(({body, headers, context}) { |
| 27 return new Response.ok(body, headers: headers, context: context); |
| 28 }); |
| 29 }); |
| 30 } |
| 31 |
| 32 /// Shared test method used by [Request] and [Response] tests to validate |
| 33 /// the behavior of `change` with different `headers` and `context` values. |
| 34 void _testChange(Message factory( |
| 35 {body, Map<String, String> headers, Map<String, Object> context})) { |
| 36 group('body', () { |
| 37 test('with String', () async { |
| 38 var request = factory(body: 'Hello, world'); |
| 39 var copy = request.change(body: 'Goodbye, world'); |
| 40 |
| 41 var newBody = await copy.readAsString(); |
| 42 |
| 43 expect(newBody, equals('Goodbye, world')); |
| 44 }); |
| 45 |
| 46 test('with Stream', () async { |
| 47 var request = factory(body: 'Hello, world'); |
| 48 var copy = request.change( |
| 49 body: new Stream.fromIterable(['Goodbye, world']) |
| 50 .transform(UTF8.encoder)); |
| 51 |
| 52 var newBody = await copy.readAsString(); |
| 53 |
| 54 expect(newBody, equals('Goodbye, world')); |
| 55 }); |
| 56 }); |
| 57 |
| 58 test('with empty headers returns indentical instance', () { |
| 59 var request = factory(headers: {'header1': 'header value 1'}); |
| 60 var copy = request.change(headers: {}); |
| 61 |
| 62 expect(copy.headers, same(request.headers)); |
| 63 }); |
| 64 |
| 65 test('with empty context returns identical instance', () { |
| 66 var request = factory(context: {'context1': 'context value 1'}); |
| 67 var copy = request.change(context: {}); |
| 68 |
| 69 expect(copy.context, same(request.context)); |
| 70 }); |
| 71 |
| 72 test('new header values are added', () { |
| 73 var request = factory(headers: {'test': 'test value'}); |
| 74 var copy = request.change(headers: {'test2': 'test2 value'}); |
| 75 |
| 76 expect(copy.headers, {'test': 'test value', 'test2': 'test2 value'}); |
| 77 }); |
| 78 |
| 79 test('existing header values are overwritten', () { |
| 80 var request = factory(headers: {'test': 'test value'}); |
| 81 var copy = request.change(headers: {'test': 'new test value'}); |
| 82 |
| 83 expect(copy.headers, {'test': 'new test value'}); |
| 84 }); |
| 85 |
| 86 test('new context values are added', () { |
| 87 var request = factory(context: {'test': 'test value'}); |
| 88 var copy = request.change(context: {'test2': 'test2 value'}); |
| 89 |
| 90 expect(copy.context, {'test': 'test value', 'test2': 'test2 value'}); |
| 91 }); |
| 92 |
| 93 test('existing context values are overwritten', () { |
| 94 var request = factory(context: {'test': 'test value'}); |
| 95 var copy = request.change(context: {'test': 'new test value'}); |
| 96 |
| 97 expect(copy.context, {'test': 'new test value'}); |
| 98 }); |
| 99 } |
OLD | NEW |