|
| 1 | +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:convert'; |
| 6 | + |
| 7 | +import 'dart:io'; |
| 8 | + |
| 9 | +import 'package:http/http.dart' as http; |
| 10 | +import 'package:path/path.dart' as p; |
| 11 | +import 'package:pub_integration/src/fake_test_context_provider.dart'; |
| 12 | +import 'package:shelf/shelf.dart' as shelf; |
| 13 | +import 'package:shelf/shelf_io.dart' as shelf_io; |
| 14 | +import 'package:test/test.dart'; |
| 15 | + |
| 16 | +void main() { |
| 17 | + group( |
| 18 | + 'exported bucket works with pub get', |
| 19 | + () { |
| 20 | + late final TestContextProvider fakeTestScenario; |
| 21 | + final httpClient = http.Client(); |
| 22 | + late final Directory temp; |
| 23 | + late final HttpServer proxy; |
| 24 | + |
| 25 | + setUpAll(() async { |
| 26 | + fakeTestScenario = await TestContextProvider.start(); |
| 27 | + temp = await Directory.systemTemp.createTemp('exported-bucket'); |
| 28 | + |
| 29 | + final pubUri = Uri.parse(fakeTestScenario.pubHostedUrl); |
| 30 | + final storageUri = pubUri.replace( |
| 31 | + port: pubUri.port + 1, path: '/fake-exported-apis/latest'); |
| 32 | + |
| 33 | + // read-only proxy server with minimal content rewrite |
| 34 | + proxy = await shelf_io.serve((rq) async { |
| 35 | + try { |
| 36 | + // the proxy url with the appropriate path prefix |
| 37 | + final proxyUri = storageUri.replace(pathSegments: [ |
| 38 | + ...storageUri.pathSegments, |
| 39 | + ...rq.requestedUri.pathSegments |
| 40 | + ]); |
| 41 | + |
| 42 | + // other requests are proxied |
| 43 | + final rs = await http.get(proxyUri); |
| 44 | + |
| 45 | + Map<String, String> copyHeaders(List<String> keys) { |
| 46 | + return Map.fromEntries( |
| 47 | + rs.headers.entries.where((e) => keys.contains(e.key))); |
| 48 | + } |
| 49 | + |
| 50 | + // package listing requests need content rewrite (+ keeping the condition broad for future JSON APIs) |
| 51 | + if (rs.statusCode == 200 && |
| 52 | + rq.requestedUri.toString().contains('/api/packages/')) { |
| 53 | + return shelf.Response( |
| 54 | + rs.statusCode, |
| 55 | + body: rs.body.replaceAll( |
| 56 | + pubUri.toString(), 'http://localhost:${proxy.port}'), |
| 57 | + headers: copyHeaders(['content-type']), |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + // otherwise return the result as-is |
| 62 | + return shelf.Response( |
| 63 | + rs.statusCode, |
| 64 | + body: rs.bodyBytes, |
| 65 | + headers: copyHeaders(['content-type', 'content-encoding']), |
| 66 | + ); |
| 67 | + } catch (e, st) { |
| 68 | + print(e); |
| 69 | + print(st); |
| 70 | + return shelf.Response.internalServerError(); |
| 71 | + } |
| 72 | + }, InternetAddress.loopbackIPv4, 0); |
| 73 | + }); |
| 74 | + |
| 75 | + tearDownAll(() async { |
| 76 | + await proxy.close(); |
| 77 | + await temp.delete(recursive: true); |
| 78 | + await fakeTestScenario.close(); |
| 79 | + httpClient.close(); |
| 80 | + }); |
| 81 | + |
| 82 | + test('bulk tests', () async { |
| 83 | + final origin = fakeTestScenario.pubHostedUrl; |
| 84 | + // init server data |
| 85 | + await httpClient.post( |
| 86 | + Uri.parse('$origin/fake-test-profile'), |
| 87 | + body: json.encode( |
| 88 | + { |
| 89 | + 'testProfile': { |
| 90 | + 'defaultUser': '[email protected]', |
| 91 | + 'packages': [ |
| 92 | + { |
| 93 | + 'name': 'exported_api_pkg', |
| 94 | + 'versions': [ |
| 95 | + {'version': '1.0.0'}, |
| 96 | + {'version': '1.0.2-dev+2'}, |
| 97 | + ], |
| 98 | + }, |
| 99 | + ], |
| 100 | + }, |
| 101 | + }, |
| 102 | + ), |
| 103 | + ); |
| 104 | + |
| 105 | + // create a local project with dependency on exported_api_pkg:1.0.2-dev+2 |
| 106 | + final pubspec = File(p.join(temp.path, 'pubspec.yaml')); |
| 107 | + await pubspec.writeAsString(json.encode({ |
| 108 | + 'name': 'test_pkg', |
| 109 | + 'environment': { |
| 110 | + 'sdk': '>=3.0.0 <4.0.0', |
| 111 | + }, |
| 112 | + 'dependencies': { |
| 113 | + 'exported_api_pkg': '^1.0.1', |
| 114 | + }, |
| 115 | + })); |
| 116 | + |
| 117 | + // run pub get and verify its success |
| 118 | + final pr = await Process.run( |
| 119 | + 'dart', |
| 120 | + ['pub', 'get', '-v'], |
| 121 | + environment: {'PUB_HOSTED_URL': 'http://localhost:${proxy.port}'}, |
| 122 | + workingDirectory: temp.path, |
| 123 | + ); |
| 124 | + expect(pr.exitCode, 0, reason: [pr.stdout, pr.stderr].join('\n')); |
| 125 | + |
| 126 | + final resolvedFile = |
| 127 | + File(p.join(temp.path, '.dart_tool', 'package_config.json')); |
| 128 | + expect(resolvedFile.existsSync(), true); |
| 129 | + }); |
| 130 | + }, |
| 131 | + timeout: Timeout.factor(testTimeoutFactor), |
| 132 | + ); |
| 133 | +} |
0 commit comments