Skip to content

Commit

Permalink
refactor(nextcloud_test): drop dependency on neon_http_client and use…
Browse files Browse the repository at this point in the history
… fixture interceptor
  • Loading branch information
Leptopoda committed Sep 5, 2024
1 parent fc21de4 commit eff600e
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 88 deletions.
2 changes: 1 addition & 1 deletion packages/interceptor_http_client/pubspec_overrides.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# melos_managed_dependency_overrides: cookie_store,dynamite_runtime,neon_lints,nextcloud
# melos_managed_dependency_overrides: cookie_store,neon_lints
dependency_overrides:
cookie_store:
path: ../cookie_store
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# melos_managed_dependency_overrides: cookie_store,dynamite_runtime,neon_lints,nextcloud
# melos_managed_dependency_overrides: cookie_store,dynamite_runtime,interceptor_http_client,neon_lints,nextcloud
dependency_overrides:
cookie_store:
path: ../../../cookie_store
Expand Down
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();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import 'dart:io';

import 'package:built_collection/built_collection.dart';
import 'package:cookie_store/cookie_store.dart';
import 'package:http/http.dart' as http;
import 'package:interceptor_http_client/interceptor_http_client.dart';
import 'package:meta/meta.dart';
import 'package:neon_http_client/neon_http_client.dart';
import 'package:nextcloud/nextcloud.dart';
import 'package:nextcloud_test/src/fixture_interceptor.dart';
import 'package:nextcloud_test/src/fixtures.dart';
import 'package:nextcloud_test/src/models/models.dart';
import 'package:nextcloud_test/src/proxy_http_client.dart';
import 'package:nextcloud_test/src/test_target/docker_container.dart';
import 'package:nextcloud_test/src/test_target/local.dart';
import 'package:version/version.dart';
Expand Down Expand Up @@ -74,12 +75,14 @@ abstract class TestTargetInstance {
appPassword = await createAppPassword(username);
}

final httpClient = NeonHttpClient(
baseURL: hostURL,
cookieStore: CookieStore(),
client: getProxyHttpClient(
onRequest: appendFixture,
),
final httpClient = InterceptorHttpClient(
baseClient: http.Client(),
interceptors: BuiltList([
CookieStoreInterceptor(
cookieStore: CookieStore(),
),
const FixtureInterceptor(appendFixture: appendFixture),
]),
);

return NextcloudClient(
Expand Down
6 changes: 3 additions & 3 deletions packages/nextcloud/packages/nextcloud_test/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ dependencies:
path: packages/cookie_store
glob: ^2.1.2
http: ^1.2.0
meta: ^1.0.0
neon_http_client:
interceptor_http_client:
git:
url: https://github.com/nextcloud/neon
path: packages/neon_http_client
path: packages/interceptor_http_client
meta: ^1.0.0
nextcloud: ^7.0.0
path: ^1.9.0
process_run: ^1.0.0+1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
# melos_managed_dependency_overrides: cookie_store,dynamite_runtime,interceptor_http_client,neon_http_client,neon_lints,nextcloud
# melos_managed_dependency_overrides: cookie_store,dynamite_runtime,interceptor_http_client,neon_lints,nextcloud
dependency_overrides:
cookie_store:
path: ../../../cookie_store
dynamite_runtime:
path: ../../../dynamite/packages/dynamite_runtime
interceptor_http_client:
path: ../../../interceptor_http_client
neon_http_client:
path: ../../../neon_framework/packages/neon_http_client
neon_lints:
path: ../../../neon_lints
nextcloud:
Expand Down
4 changes: 1 addition & 3 deletions packages/nextcloud/pubspec_overrides.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# melos_managed_dependency_overrides: cookie_store,dynamite,dynamite_runtime,interceptor_http_client,neon_http_client,neon_lints,nextcloud_test
# melos_managed_dependency_overrides: cookie_store,dynamite,dynamite_runtime,interceptor_http_client,neon_lints,nextcloud_test
dependency_overrides:
cookie_store:
path: ../cookie_store
Expand All @@ -8,8 +8,6 @@ dependency_overrides:
path: ../dynamite/packages/dynamite_runtime
interceptor_http_client:
path: ../interceptor_http_client
neon_http_client:
path: ../neon_framework/packages/neon_http_client
neon_lints:
path: ../neon_lints
nextcloud_test:
Expand Down

0 comments on commit eff600e

Please sign in to comment.