-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(nextcloud_test): drop dependency on neon_http_client and use…
… fixture interceptor
- Loading branch information
Showing
8 changed files
with
114 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/neon_framework/packages/neon_http_client/pubspec_overrides.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
packages/nextcloud/packages/nextcloud_test/lib/src/fixture_interceptor.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import 'dart:convert'; | ||
import 'dart:io' show HttpHeaders; | ||
import 'dart:typed_data'; | ||
|
||
import 'package:http/http.dart' as http; | ||
import 'package:interceptor_http_client/interceptor_http_client.dart'; | ||
|
||
/// An http interceptor that records every request and adds them to a fixture. | ||
final class FixtureInterceptor implements HttpInterceptor { | ||
/// Creates a new fixture interceptor. | ||
const FixtureInterceptor({ | ||
required this.appendFixture, | ||
}); | ||
|
||
/// Callback for adding a recorded request to the fixture. | ||
final void Function(String fixture) appendFixture; | ||
|
||
@override | ||
bool shouldInterceptRequest(http.BaseRequest request) { | ||
// TODO: use resetFixture and intercept all requests | ||
return request.url.path != '/index.php/csrftoken'; | ||
} | ||
|
||
@override | ||
Future<http.BaseRequest> interceptRequest({required http.BaseRequest request}) async { | ||
assert( | ||
shouldInterceptRequest(request), | ||
'Request should not be intercepted.', | ||
); | ||
|
||
final bodyBytes = switch (request) { | ||
http.Request() => request.bodyBytes, | ||
_ => await request.finalize().toBytes(), | ||
}; | ||
|
||
final fixture = _formatHttpRequest(request, bodyBytes); | ||
appendFixture(fixture); | ||
|
||
return switch (request) { | ||
http.Request() => request, | ||
_ => http.Request(request.method, request.url) | ||
..persistentConnection = request.persistentConnection | ||
..followRedirects = request.followRedirects | ||
..maxRedirects = request.maxRedirects | ||
..headers.addAll(request.headers) | ||
..bodyBytes = bodyBytes, | ||
}; | ||
} | ||
|
||
@override | ||
bool shouldInterceptResponse(http.StreamedResponse response) { | ||
return false; | ||
} | ||
|
||
@override | ||
Never interceptResponse({required http.StreamedResponse response, required Uri url}) { | ||
throw UnsupportedError('Fixtures may not intercept responses.'); | ||
} | ||
|
||
static String _formatHttpRequest(http.BaseRequest request, Uint8List body) { | ||
final buffer = StringBuffer('${request.method.toUpperCase()} ${request.url.replace(port: 80)}'); | ||
|
||
final headers = <String>[]; | ||
for (final header in request.headers.entries) { | ||
final name = header.key.toLowerCase(); | ||
var value = header.value; | ||
|
||
if (name == HttpHeaders.hostHeader) { | ||
continue; | ||
} else if (name == HttpHeaders.cookieHeader) { | ||
continue; | ||
} else if (name == HttpHeaders.authorizationHeader) { | ||
value = '${value.split(' ').first} mock'; | ||
} else if (name == 'requesttoken') { | ||
value = 'token'; | ||
} else if (name == 'destination') { | ||
value = Uri.parse(value).replace(port: 80).toString(); | ||
} | ||
|
||
headers.add('\n$name: $value'); | ||
} | ||
|
||
headers.sort(); | ||
buffer.writeAll(headers); | ||
|
||
if (body.isNotEmpty) { | ||
try { | ||
buffer.write('\n${utf8.decode(body)}'); | ||
} catch (_) { | ||
buffer.write('\n${base64.encode(body)}'); | ||
} | ||
} | ||
|
||
return buffer.toString(); | ||
} | ||
} |
69 changes: 0 additions & 69 deletions
69
packages/nextcloud/packages/nextcloud_test/lib/src/proxy_http_client.dart
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 1 addition & 3 deletions
4
packages/nextcloud/packages/nextcloud_test/pubspec_overrides.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters