Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(777)

Unified Diff: pkg/appengine/test/assets_test.dart

Issue 804973002: Add appengine/gcloud/mustache dependencies. (Closed) Base URL: git@github.com:dart-lang/pub-dartlang-dart.git@master
Patch Set: Added AUTHORS/LICENSE/PATENTS files Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/appengine/pubspec.yaml ('k') | pkg/appengine/test/dart-python--datastore-compatibility/dart/app.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/appengine/test/assets_test.dart
diff --git a/pkg/appengine/test/assets_test.dart b/pkg/appengine/test/assets_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..a5b3f7c59599ef139a93efe0ac8e62c674f480bd
--- /dev/null
+++ b/pkg/appengine/test/assets_test.dart
@@ -0,0 +1,299 @@
+import 'dart:async';
+import 'dart:convert';
+import 'dart:io';
+
+import 'package:appengine/src/appengine_context.dart';
+import 'package:appengine/src/client_context.dart';
+import 'package:appengine/src/server/assets.dart';
+import 'package:unittest/unittest.dart';
+
+
+class _AssetError extends TypeMatcher {
+ const _AssetError() : super("AssetError");
+ bool matches(item, Map matchState) => item is AssetError;
+}
+
+const isAssetError = const _AssetError();
+
+// HTTP client which requests /test and completes with the binary
+// body.
+Future<List<int>> getAsset(int port, String path,
+ [int expectedStatusCode = 200]) {
+ var client;
+ client = new HttpClient();
+ return client.get('127.0.0.1', port, path)
+ .then((request) {
+ return request.close();
+ })
+ .then((response) {
+ expect(response.statusCode, expectedStatusCode);
+ return response
+ .fold([], (buf, data) => buf..addAll(data));
+ })
+ .whenComplete(() {
+ client.close();
+ });
+}
+
+void main() {
+ if (!FileSystemEntity.isDirectorySync(AssetsManager.root)) {
+ throw new StateError('The directory "${AssetsManager.root}" does not '
+ 'exist in the current directory – "${Directory.current.path}". '
+ 'Try running from the "test" directory.');
+ }
+
+ test('use pub serve', () {
+ var uri = Uri.parse("http://localhost:9090");
+ var context;
+
+ context = new AppengineContext(
+ 'dev', null, null, null, null, uri);
+ expect(context.isDevelopmentEnvironment, isTrue);
+ expect(context.assets.usePubServe, isTrue);
+
+ context = new AppengineContext(
+ 's', null, null, null, null, uri);
+ expect(context.isDevelopmentEnvironment, isFalse);
+ expect(context.assets.usePubServe, isFalse);
+
+ context = new AppengineContext(
+ 'dev', null, null, null, null, null);
+ expect(context.isDevelopmentEnvironment, isTrue);
+ expect(context.assets.usePubServe, isFalse);
+
+ context = new AppengineContext(
+ 's', null, null, null, null, null);
+ expect(context.isDevelopmentEnvironment, isFalse);
+ expect(context.assets.usePubServe, isFalse);
+ });
+
+ group('pub serve proxy', () {
+ var pubServe;
+ var appServer;
+ var appServerPort;
+ var pubServeUri;
+
+ // 'pub serve' mock which expects /test request and returns a
+ // fixed body.
+ Future startPubServeMock() {
+ return HttpServer.bind('127.0.0.1', 0).then((server) {
+ server.listen(expectAsync((request) {
+ if (request.uri.path == '/test') {
+ request.response.statusCode = HttpStatus.NOT_FOUND;
+ } else if (request.uri.path == '/test.html') {
+ request.response.write('pub serve html');
+ } else {
+ expect(request.uri.path, '/test.css');
+ request.response.write('pub serve css');
+ }
+ request.response.close();
+ }));
+ return server;
+ });
+ }
+
+ // Mock application server which passes the Assets instance to the
+ // request handler.
+ Future startAppServer(mockRequestHandler) {
+ assert(appServer == null);
+ return HttpServer.bind('127.0.0.1', 0).then((server) {
+ var appengineContext = new AppengineContext(
+ 'dev', null, null, null, null, pubServeUri);
+ appServer = server;
+ appServerPort = server.port;
+ server.listen((request) {
+ var assets = new AssetsImpl(request, appengineContext);
+ mockRequestHandler(request, assets);
+ });
+ return server;
+ });
+ }
+
+ setUp(() {
+ return startPubServeMock().then((server) {
+ pubServe = server;
+ pubServeUri =
+ new Uri.http('${server.address.address}:${server.port}', '');
+ });
+ });
+
+ tearDown(() {
+ return pubServe.close().then((_) {
+ pubServe = null;
+ return appServer.close().then((_) {
+ appServer = null;
+ });
+ });
+ });
+
+ test('test not found serve', () {
+ void requestHandler(HttpRequest request, Assets assets) {
+ assets.serve();
+ }
+
+ return startAppServer(requestHandler).then((_) {
+ return getAsset(
+ appServerPort, '/test', HttpStatus.NOT_FOUND).then((body) {
+ expect(body, isEmpty);
+ });
+ });
+ });
+
+ test('test not found read', () {
+ void requestHandler(HttpRequest request, Assets assets) {
+ assets.read('/test').then((stream) {
+ fail('Unexpected');
+ }).catchError((e) {
+ expect(e, isAssetError);
+ request.response
+ ..statusCode = HttpStatus.NOT_FOUND
+ ..close();
+ });
+ }
+
+ return startAppServer(requestHandler).then((_) {
+ return getAsset(
+ appServerPort, '/test', HttpStatus.NOT_FOUND).then((body) {
+ expect(body, isEmpty);
+ });
+ });
+ });
+
+ test('test serve', () {
+ void requestHandler(HttpRequest request, Assets assets) {
+ assets.serve();
+ }
+
+ return startAppServer(requestHandler).then((_) {
+ return getAsset(appServerPort, '/test.html').then((body) {
+ expect(LATIN1.decode(body), 'pub serve html');
+ });
+ });
+ });
+
+ test('test serve path', () {
+ void requestHandler(HttpRequest request, Assets assets) {
+ assets.serve('/test.css');
+ }
+
+ return startAppServer(requestHandler).then((_) {
+ return getAsset(appServerPort, '/test.css').then((body) {
+ expect(LATIN1.decode(body), 'pub serve css');
+ });
+ });
+ });
+
+ test('test read', () {
+ void requestHandler(HttpRequest request, Assets assets) {
+ assets.read('/test.html').then((stream) {
+ return stream.pipe(request.response);
+ });
+ }
+
+ return startAppServer(requestHandler).then((_) {
+ return getAsset(appServerPort, '/test.html').then((body) {
+ expect(LATIN1.decode(body), 'pub serve html');
+ });
+ });
+ });
+ });
+
+ group('no pub serve proxy', () {
+ var appServer;
+ var appServerPort;
+
+ // Mock application server which passes the Assets instance to the
+ // request handler.
+ Future startAppServer(mockRequestHandler) {
+ assert(appServer == null);
+ return HttpServer.bind('127.0.0.1', 0).then((server) {
+ var appengineContext = new AppengineContext(
+ null, null, null, null, null, null);
+ appServer = server;
+ appServerPort = server.port;
+ server.listen((request) {
+ var assets = new AssetsImpl(request, appengineContext);
+ mockRequestHandler(request, assets);
+ });
+ return server;
+ });
+ }
+
+ tearDown(() {
+ return appServer.close().then((_) {
+ appServer = null;
+ });
+ });
+
+ test('test not found serve', () {
+ void requestHandler(HttpRequest request, Assets assets) {
+ assets.serve();
+ }
+
+ return startAppServer(requestHandler).then((_) {
+ return getAsset(
+ appServerPort, '/test', HttpStatus.NOT_FOUND).then((body) {
+ expect(body, isEmpty);
+ });
+ });
+ });
+
+ test('test not found read', () {
+ void requestHandler(HttpRequest request, Assets assets) {
+ assets.read('/test').then((stream) {
+ fail('Unexpected');
+ }).catchError((e) {
+ expect(e, isAssetError);
+ request.response
+ ..statusCode = HttpStatus.NOT_FOUND
+ ..close();
+ });
+ }
+
+ return startAppServer(requestHandler).then((_) {
+ return getAsset(
+ appServerPort, '/test', HttpStatus.NOT_FOUND).then((body) {
+ expect(body, isEmpty);
+ });
+ });
+ });
+
+ test('test serve', () {
+ void requestHandler(HttpRequest request, Assets assets) {
+ assets.serve();
+ }
+
+ return startAppServer(requestHandler).then((_) {
+ return getAsset(appServerPort, '/test.html').then((body) {
+ expect(LATIN1.decode(body), 'from build html');
+ });
+ });
+ });
+
+ test('test serve path', () {
+ void requestHandler(HttpRequest request, Assets assets) {
+ assets.serve('/test.css');
+ }
+
+ return startAppServer(requestHandler).then((_) {
+ return getAsset(appServerPort, '/test.html').then((body) {
+ expect(LATIN1.decode(body), 'from build css');
+ });
+ });
+ });
+
+ test('test read', () {
+ void requestHandler(HttpRequest request, Assets assets) {
+ assets.read('/test.html').then((stream) {
+ return stream.pipe(request.response);
+ });
+ }
+
+ return startAppServer(requestHandler).then((_) {
+ return getAsset(appServerPort, '/test.html').then((body) {
+ expect(LATIN1.decode(body), 'from build html');
+ });
+ });
+ });
+ });
+}
« no previous file with comments | « pkg/appengine/pubspec.yaml ('k') | pkg/appengine/test/dart-python--datastore-compatibility/dart/app.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698