|
| 1 | +import 'dart:convert'; |
| 2 | +import 'dart:typed_data'; |
| 3 | + |
| 4 | +import 'package:http_interceptor/http_interceptor.dart'; |
| 5 | +import 'package:test/test.dart'; |
| 6 | + |
| 7 | +void main() { |
| 8 | + group('StreamedRequest.copyWith', () { |
| 9 | + late StreamedRequest request; |
| 10 | + final Uri testUrl = Uri.parse('https://example.com'); |
| 11 | + final Map<String, String> testHeaders = { |
| 12 | + 'Content-Type': 'application/json', |
| 13 | + }; |
| 14 | + |
| 15 | + setUp(() { |
| 16 | + request = StreamedRequest('POST', testUrl)..headers.addAll(testHeaders); |
| 17 | + |
| 18 | + // Add test data to the request |
| 19 | + final Uint8List testData = utf8.encode('test data'); |
| 20 | + request.sink.add(testData); |
| 21 | + request.sink.close(); |
| 22 | + }); |
| 23 | + |
| 24 | + test( |
| 25 | + 'creates a copy with the same properties when no parameters are provided', |
| 26 | + () async { |
| 27 | + // Buffer the original body before copyWith() ever runs |
| 28 | + final Uint8List originalBytes = await request.finalize().toBytes(); |
| 29 | + |
| 30 | + // Act: pass the buffered stream into copyWith |
| 31 | + final StreamedRequest copy = await request.copyWith( |
| 32 | + stream: Stream.value(originalBytes), |
| 33 | + ); |
| 34 | + |
| 35 | + // Assert basic properties |
| 36 | + expect(copy.method, equals(request.method)); |
| 37 | + expect(copy.url, equals(request.url)); |
| 38 | + expect(copy.headers, equals(request.headers)); |
| 39 | + |
| 40 | + // Only finalize the copy, and compare its bytes to the buffer |
| 41 | + final Uint8List copyBytes = await copy.finalize().toBytes(); |
| 42 | + expect(copyBytes, equals(originalBytes)); |
| 43 | + |
| 44 | + // Assert default flags |
| 45 | + expect(copy.followRedirects, equals(true)); |
| 46 | + expect(copy.maxRedirects, equals(5)); |
| 47 | + expect(copy.persistentConnection, equals(true)); |
| 48 | + }, |
| 49 | + ); |
| 50 | + |
| 51 | + test('overrides method when provided', () async { |
| 52 | + // Act |
| 53 | + final StreamedRequest copy = |
| 54 | + await request.copyWith(method: HttpMethod.PUT); |
| 55 | + |
| 56 | + // Assert |
| 57 | + expect(copy.method, equals('PUT')); |
| 58 | + }); |
| 59 | + |
| 60 | + test('overrides url when provided', () async { |
| 61 | + // Arrange |
| 62 | + final Uri newUrl = Uri.parse('https://example.org'); |
| 63 | + |
| 64 | + // Act |
| 65 | + final StreamedRequest copy = await request.copyWith(url: newUrl); |
| 66 | + |
| 67 | + // Assert |
| 68 | + expect(copy.url, equals(newUrl)); |
| 69 | + }); |
| 70 | + |
| 71 | + test('overrides headers when provided', () async { |
| 72 | + // Arrange |
| 73 | + final Map<String, String> newHeaders = {'Authorization': 'Bearer token'}; |
| 74 | + |
| 75 | + // Act |
| 76 | + final StreamedRequest copy = await request.copyWith(headers: newHeaders); |
| 77 | + |
| 78 | + // Assert |
| 79 | + expect(copy.headers, equals(newHeaders)); |
| 80 | + }); |
| 81 | + |
| 82 | + test('overrides stream when provided', () async { |
| 83 | + // Arrange |
| 84 | + final Uint8List newData = utf8.encode('new data'); |
| 85 | + final Stream<Uint8List> newStream = Stream.value(newData); |
| 86 | + |
| 87 | + // Act |
| 88 | + final StreamedRequest copy = await request.copyWith(stream: newStream); |
| 89 | + |
| 90 | + // Assert |
| 91 | + final Uint8List copyData = await copy.finalize().toBytes(); |
| 92 | + expect(copyData, equals(newData)); |
| 93 | + }); |
| 94 | + |
| 95 | + test('sets followRedirects on original request (bug)', () async { |
| 96 | + // Arrange |
| 97 | + final bool originalValue = request.followRedirects; |
| 98 | + |
| 99 | + // Act |
| 100 | + final StreamedRequest copy = |
| 101 | + await request.copyWith(followRedirects: !originalValue); |
| 102 | + |
| 103 | + // Assert |
| 104 | + expect(request.followRedirects, equals(originalValue)); |
| 105 | + expect(copy.followRedirects, equals(!originalValue)); |
| 106 | + }); |
| 107 | + |
| 108 | + test('sets maxRedirects on original request (bug)', () async { |
| 109 | + // Arrange |
| 110 | + final int newMaxRedirects = 10; |
| 111 | + |
| 112 | + // Act |
| 113 | + final StreamedRequest copy = |
| 114 | + await request.copyWith(maxRedirects: newMaxRedirects); |
| 115 | + |
| 116 | + // Assert |
| 117 | + expect(request.maxRedirects, equals(5)); |
| 118 | + expect(copy.maxRedirects, equals(newMaxRedirects)); |
| 119 | + expect(copy.method, equals(request.method)); |
| 120 | + }); |
| 121 | + |
| 122 | + test('sets persistentConnection on original request (bug)', () async { |
| 123 | + // Arrange |
| 124 | + final bool originalValue = request.persistentConnection; |
| 125 | + |
| 126 | + // Act |
| 127 | + final StreamedRequest copy = |
| 128 | + await request.copyWith(persistentConnection: !originalValue); |
| 129 | + |
| 130 | + // Assert |
| 131 | + expect(request.persistentConnection, equals(originalValue)); |
| 132 | + expect(copy.persistentConnection, equals(!originalValue)); |
| 133 | + }); |
| 134 | + }); |
| 135 | +} |
0 commit comments