Skip to content

Commit

Permalink
🐛 properly escape single quote in cURL interceptor output (#635)
Browse files Browse the repository at this point in the history
  • Loading branch information
techouse committed Sep 4, 2024
1 parent 7a1f7c2 commit b7ebf67
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 25 deletions.
4 changes: 2 additions & 2 deletions chopper/lib/src/interceptors/curl_interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CurlInterceptor implements Interceptor {
if (baseRequest is http.Request) {
final String body = baseRequest.body;
if (body.isNotEmpty) {
curlParts.add("-d '$body'");
curlParts.add("-d '${body.replaceAll("'", r"'\''")}'");
}
}
if (baseRequest is http.MultipartRequest) {
Expand All @@ -36,7 +36,7 @@ class CurlInterceptor implements Interceptor {
curlParts.add("-f '${file.field}: ${file.filename ?? ''}'");
}
}
curlParts.add('"${baseRequest.url}"');
curlParts.add("'${baseRequest.url}'");
chopperLogger.info(curlParts.join(' '));

return chain.proceed(chain.request);
Expand Down
67 changes: 44 additions & 23 deletions chopper/test/interceptors_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ void main() {
);
});

final fakeRequest = Request(
'POST',
Uri.parse('/'),
Uri.parse('base'),
body: 'test',
headers: {'foo': 'bar'},
);

test('Curl interceptors', () async {
final fakeRequest = Request(
'POST',
Uri.parse('/'),
Uri.parse('base'),
body: 'test',
headers: {'foo': 'bar'},
);

final curl = CurlInterceptor();
var log = '';
chopperLogger.onRecord.listen((r) => log = r.message);
Expand All @@ -108,27 +108,48 @@ void main() {
expect(
log,
equals(
"curl -v -X POST -H 'foo: bar' -H 'content-type: text/plain; charset=utf-8' -d 'test' \"base/\"",
r"curl -v -X POST -H 'foo: bar' -H 'content-type: text/plain; charset=utf-8' -d 'test' 'base/'",
),
);
});

final fakeRequestMultipart = Request(
'POST',
Uri.parse('/'),
Uri.parse('base'),
headers: {'foo': 'bar'},
parts: [
PartValue<int>('p1', 123),
PartValueFile<http.MultipartFile>(
'p2',
http.MultipartFile.fromBytes('file', [0], filename: 'filename'),
test('Curl interceptor with escaped text', () async {
final fakeRequest = Request(
'POST',
Uri.parse('/'),
Uri.parse('base'),
body: r"""Lorem's ipsum "dolor" sit amet""",
);

final curl = CurlInterceptor();
var log = '';
chopperLogger.onRecord.listen((r) => log = r.message);
await curl.intercept(FakeChain(fakeRequest));

expect(
log,
equals(
r"""curl -v -X POST -H 'content-type: text/plain; charset=utf-8' -d 'Lorem'\''s ipsum "dolor" sit amet' 'base/'""",
),
],
multipart: true,
);
);
});

test('Curl interceptors Multipart', () async {
final fakeRequestMultipart = Request(
'POST',
Uri.parse('/'),
Uri.parse('base'),
headers: {'foo': 'bar'},
parts: [
PartValue<int>('p1', 123),
PartValueFile<http.MultipartFile>(
'p2',
http.MultipartFile.fromBytes('file', [0], filename: 'filename'),
),
],
multipart: true,
);

final curl = CurlInterceptor();
var log = '';
chopperLogger.onRecord.listen((r) => log = r.message);
Expand All @@ -137,7 +158,7 @@ void main() {
expect(
log,
equals(
"curl -v -X POST -H 'foo: bar' -f 'p1: 123' -f 'file: filename' \"base/\"",
r"curl -v -X POST -H 'foo: bar' -f 'p1: 123' -f 'file: filename' 'base/'",
),
);
});
Expand Down

0 comments on commit b7ebf67

Please sign in to comment.