From 139aab6a635ddcda42896eb9b146e933a3ef65a3 Mon Sep 17 00:00:00 2001 From: Istvan Soos Date: Wed, 30 Apr 2025 18:39:06 +0200 Subject: [PATCH] Remove fake HTTP GET bridge from the tests used by SearchClient. --- .../utils/http_client_to_shelf_handler.dart | 68 ------------------- app/test/frontend/handlers/listing_test.dart | 14 ---- app/test/shared/test_services.dart | 11 +-- 3 files changed, 1 insertion(+), 92 deletions(-) delete mode 100644 app/lib/tool/utils/http_client_to_shelf_handler.dart diff --git a/app/lib/tool/utils/http_client_to_shelf_handler.dart b/app/lib/tool/utils/http_client_to_shelf_handler.dart deleted file mode 100644 index b275313dde..0000000000 --- a/app/lib/tool/utils/http_client_to_shelf_handler.dart +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -import 'package:gcloud/service_scope.dart'; -import 'package:http/http.dart' as http; -import 'package:http/testing.dart' as http_testing; -import 'package:logging/logging.dart'; -import 'package:shelf/shelf.dart' as shelf; - -import '../../frontend/handlers.dart'; -import '../../shared/handler_helpers.dart'; -import '../../tool/utils/http.dart'; - -/// Returns a HTTP client that bridges HTTP requests and shelf handlers without -/// the actual HTTP transport layer. -/// -/// If [handler] is not specified, it will use the default frontend handler. -http.Client httpClientToShelfHandler({ - shelf.Handler? handler, - String? authToken, - String? sessionId, - String? csrfToken, -}) { - handler ??= createAppHandler(); - handler = wrapHandler( - Logger.detached('test'), - handler, - sanitize: true, - ); - return httpClientWithAuthorization( - tokenProvider: () async => authToken, - sessionIdProvider: () async => sessionId, - csrfTokenProvider: () async => csrfToken, - client: http_testing.MockClient(_wrapShelfHandler(handler)), - ); -} - -String _removeLeadingSlashes(String path) { - while (path.startsWith('/')) { - path = path.substring(1); - } - return path; -} - -http_testing.MockClientHandler _wrapShelfHandler(shelf.Handler handler) { - return (rq) async { - final shelfRq = shelf.Request( - rq.method, - rq.url.replace(path: _removeLeadingSlashes(rq.url.path)), - body: rq.body, - headers: rq.headers, - url: Uri(path: _removeLeadingSlashes(rq.url.path), query: rq.url.query), - handlerPath: '', - ); - late shelf.Response rs; - // Need to fork a service scope to create a separate RequestContext in the - // search service handler. - await fork(() async { - rs = await handler(shelfRq); - }); - return http.Response( - await rs.readAsString(), - rs.statusCode, - headers: rs.headers, - ); - }; -} diff --git a/app/test/frontend/handlers/listing_test.dart b/app/test/frontend/handlers/listing_test.dart index 8084f7210b..c4a1ca87dc 100644 --- a/app/test/frontend/handlers/listing_test.dart +++ b/app/test/frontend/handlers/listing_test.dart @@ -2,9 +2,6 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -import 'package:http/testing.dart'; -import 'package:pub_dev/package/name_tracker.dart'; -import 'package:pub_dev/search/search_client.dart'; import 'package:pub_dev/tool/test_profile/models.dart'; import 'package:test/test.dart'; @@ -72,17 +69,6 @@ void main() { ); }); - testWithProfile('/packages?q=oxyge without working search', fn: () async { - registerSearchClient( - SearchClient(MockClient((_) async => throw Exception()))); - await nameTracker.reloadFromDatastore(); - final content = await expectHtmlResponse( - await issueGet('/packages?q=oxyge'), - status: 503, - ); - expect(content, contains('oxygen is awesome')); - }); - testWithProfile('/packages?page=2', testProfile: TestProfile( defaultUser: 'admin@pub.dev', diff --git a/app/test/shared/test_services.dart b/app/test/shared/test_services.dart index 868d640288..5467ffe847 100644 --- a/app/test/shared/test_services.dart +++ b/app/test/shared/test_services.dart @@ -17,8 +17,6 @@ import 'package:pub_dev/fake/backend/fake_pub_worker.dart'; import 'package:pub_dev/frontend/handlers/pubapi.client.dart'; import 'package:pub_dev/frontend/static_files.dart'; import 'package:pub_dev/package/name_tracker.dart'; -import 'package:pub_dev/search/handlers.dart'; -import 'package:pub_dev/search/search_client.dart'; import 'package:pub_dev/search/top_packages.dart'; import 'package:pub_dev/search/updater.dart'; import 'package:pub_dev/service/async_queue/async_queue.dart'; @@ -35,7 +33,6 @@ import 'package:pub_dev/tool/neat_task/pub_dev_tasks.dart'; import 'package:pub_dev/tool/test_profile/import_source.dart'; import 'package:pub_dev/tool/test_profile/importer.dart'; import 'package:pub_dev/tool/test_profile/models.dart'; -import 'package:pub_dev/tool/utils/http_client_to_shelf_handler.dart'; import 'package:pub_dev/tool/utils/pub_api_client.dart'; import 'package:test/test.dart'; @@ -96,9 +93,6 @@ class FakeAppengineEnv { cloudCompute: _cloudCompute, fn: () async { registerStaticFileCacheForTest(_staticFileCacheForTesting); - registerSearchClient(SearchClient( - httpClientToShelfHandler(handler: searchServiceHandler))); - registerScopeExitCallback(searchClient.close); if (testProfile != null) { await importProfile( @@ -201,10 +195,7 @@ void testWithFakeTime( setupDebugEnvBasedLogging(); await withFakeServices( fn: () async { - registerStaticFileCacheForTest(StaticFileCache.forTests()); - registerSearchClient(SearchClient( - httpClientToShelfHandler(handler: searchServiceHandler))); - registerScopeExitCallback(searchClient.close); + registerStaticFileCacheForTest(_staticFileCacheForTesting); await importProfile( profile: testProfile ?? defaultTestProfile,