diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000..dc6ee309 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +# 1.2.3 +Fixed #40 and #41, which dealt with paths being improperly served when using a +`publicPath`. \ No newline at end of file diff --git a/lib/src/virtual_directory.dart b/lib/src/virtual_directory.dart index 5c098eca..94c08a92 100644 --- a/lib/src/virtual_directory.dart +++ b/lib/src/virtual_directory.dart @@ -107,8 +107,8 @@ class VirtualDirectory implements AngelPlugin { void serve(Router router) { // _printDebug('Source directory: ${source.absolute.path}'); // _printDebug('Public path prefix: "$_prefix"'); - router.get('$publicPath/*', - (RequestContext req, ResponseContext res) async { + //router.get('$publicPath/*', + router.get('$_prefix/*', (RequestContext req, ResponseContext res) async { var path = req.path.replaceAll(_straySlashes, ''); return servePath(path, req, res); }); @@ -124,7 +124,7 @@ class VirtualDirectory implements AngelPlugin { } close() async { - if (!_transformerLoad.isCompleted) { + if (!_transformerLoad.isCompleted && _transformers.isNotEmpty) { _transformerLoad.completeError(new StateError( 'VirtualDirectory was closed before all transformers loaded.')); } @@ -176,10 +176,13 @@ class VirtualDirectory implements AngelPlugin { servePath(String path, RequestContext req, ResponseContext res) async { if (_prefix.isNotEmpty) { - path = path.replaceAll(new RegExp('^' + _pathify(_prefix)), ''); + // Only replace the *first* incidence + // Resolve: https://github.com/angel-dart/angel/issues/41 + path = path.replaceFirst(new RegExp('^' + _pathify(_prefix)), ''); } if (path.isEmpty) path = '.'; + path = path.replaceAll(_straySlashes, ''); var absolute = source.absolute.uri.resolve(path).toFilePath(); var stat = await FileStat.stat(absolute); diff --git a/pubspec.yaml b/pubspec.yaml index 4e4bebf2..e6e1fed1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -4,7 +4,7 @@ environment: sdk: ">=1.19.0" homepage: https://github.com/angel-dart/static author: Tobe O -version: 1.2.2+4 +version: 1.2.3 dependencies: angel_framework: ^1.0.0-dev cli_util: ^0.1.1 diff --git a/test/issue41_test.dart b/test/issue41_test.dart new file mode 100644 index 00000000..212918a3 --- /dev/null +++ b/test/issue41_test.dart @@ -0,0 +1,61 @@ +import 'dart:async'; +import 'dart:io'; +import 'package:angel_diagnostics/angel_diagnostics.dart'; +import 'package:angel_framework/angel_framework.dart'; +import 'package:angel_static/angel_static.dart'; +import 'package:angel_test/angel_test.dart'; +import 'package:test/test.dart'; + +final Directory swaggerUiDistDir = + new Directory('test/node_modules/swagger-ui-dist'); + +main() async { + TestClient client; + String swaggerUiCssContents, swaggerTestJsContents; + + setUp(() async { + // Load file contents + swaggerUiCssContents = + await new File.fromUri(swaggerUiDistDir.uri.resolve('swagger-ui.css')) + .readAsString(); + swaggerTestJsContents = + await new File.fromUri(swaggerUiDistDir.uri.resolve('test.js')) + .readAsString(); + + // Initialize app + var app = new Angel(); + await Future.forEach([ + new VirtualDirectory(source: swaggerUiDistDir, publicPath: 'swagger/'), + logRequests() + ], app.configure); + + app.dumpTree(); + client = await connectTo(app); + }); + + tearDown(() => client.close()); + + test('prefix is not replaced in file paths', () async { + var response = await client.get('/swagger/swagger-ui.css'); + print('Response: ${response.body}'); + expect(response, hasBody(swaggerUiCssContents)); + }); + + test('get a file without prefix in name', () async { + var response = await client.get('/swagger/test.js'); + print('Response: ${response.body}'); + expect(response, hasBody(swaggerTestJsContents)); + }); + + test('trailing slash at root', () async { + var response = await client.get('/swagger'); + var body1 = response.body; + print('Response #1: $body1'); + + response = await client.get('/swagger/'); + var body2 = response.body; + print('Response #2: $body2'); + + expect(body1, body2); + }); +} diff --git a/test/node_modules/swagger-ui-dist/index.html b/test/node_modules/swagger-ui-dist/index.html new file mode 100644 index 00000000..6607e2bc --- /dev/null +++ b/test/node_modules/swagger-ui-dist/index.html @@ -0,0 +1,10 @@ + + + + Swagger... + + +

Hello!

+Hooray for testing... + + \ No newline at end of file diff --git a/test/node_modules/swagger-ui-dist/swagger-ui.css b/test/node_modules/swagger-ui-dist/swagger-ui.css new file mode 100644 index 00000000..9cabb2d4 --- /dev/null +++ b/test/node_modules/swagger-ui-dist/swagger-ui.css @@ -0,0 +1,3 @@ +html, body { + font-weight: bold; +} \ No newline at end of file diff --git a/test/node_modules/swagger-ui-dist/test.js b/test/node_modules/swagger-ui-dist/test.js new file mode 100644 index 00000000..3c800d3a --- /dev/null +++ b/test/node_modules/swagger-ui-dist/test.js @@ -0,0 +1 @@ +console.log('foo'); \ No newline at end of file