Skip to content

Commit 597caf2

Browse files
committed
fix: intercepting body causes headers to be reset
1 parent 47a9c1b commit 597caf2

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 2.0.0-beta.2
4+
5+
- 🐞  Fixed: Changing `body` causes that the `headers` also change and ignore previous interceptions (i.e. content-type headers are overriden).
6+
37
## 2.0.0-beta.1
48

59
- ❗️🛠  Changed: Renamed `Method` to use `HttpMethod` and refactored helper functions into extensions (`StringToMethod`, and `MethodToString`).

lib/extensions/request.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ extension RequestCopyWith on Request {
2121
method?.asString ?? this.method,
2222
url ?? this.url,
2323
)
24-
..headers.addAll(headers ?? {})
25-
..encoding = encoding ?? this.encoding
2624
..body = body ?? this.body
25+
..headers.addAll(headers ?? this.headers)
26+
..encoding = encoding ?? this.encoding
2727
..followRedirects = followRedirects ?? this.followRedirects
2828
..maxRedirects = maxRedirects ?? this.maxRedirects
2929
..persistentConnection =

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: http_interceptor
22
description: A lightweight, simple plugin that allows you to intercept request and response objects and modify them if desired.
3-
version: 2.0.0-beta.1
3+
version: 2.0.0-beta.2
44
homepage: https://github.com/CodingAleCR/http_interceptor
55
issue_tracker: https://github.com/CodingAleCR/http_interceptor/issues
66
repository: https://github.com/CodingAleCR/http_interceptor

test/extensions/request_test.dart

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:convert';
2+
import 'dart:io';
23

34
import 'package:http/http.dart';
45
import 'package:http_interceptor/http_interceptor.dart';
@@ -10,7 +11,10 @@ main() {
1011

1112
setUpAll(() {
1213
baseRequest = Request("GET", Uri.https("www.google.com", "/helloworld"))
13-
..body = jsonEncode(<String, String>{'some_param': 'some value'});
14+
..body = jsonEncode(<String, String>{'some_param': 'some value'})
15+
..headers.addAll({
16+
HttpHeaders.contentTypeHeader: 'application/json; charset=utf-8',
17+
});
1418
request = baseRequest as Request;
1519
});
1620

@@ -154,7 +158,7 @@ main() {
154158
test('Request is copied overriding headers', () {
155159
// Arrange
156160
final newHeaders = Map<String, String>.from(request.headers);
157-
newHeaders['content-type'] = 'application/json; charset=utf-8';
161+
newHeaders['content-type'] = 'text/plain; charset=utf-8';
158162

159163
// Act
160164
Request copied = request.copyWith(
@@ -202,10 +206,13 @@ main() {
202206
expect(copied.maxRedirects, equals(request.maxRedirects));
203207
expect(copied.persistentConnection, equals(request.persistentConnection));
204208
});
209+
205210
test('Request is copied with different encoding', () {
206211
// Arrange
207212
final newEncoding = Encoding.getByName('latin1');
208-
final changedHeaders = {'content-type': 'text/plain; charset=iso-8859-1'};
213+
final changedHeaders = {
214+
'content-type': 'application/json; charset=iso-8859-1'
215+
};
209216

210217
// Act
211218
Request copied = request.copyWith(

0 commit comments

Comments
 (0)