From e7e711340bcc474fe2c8dddf9e193e85c92c7b94 Mon Sep 17 00:00:00 2001 From: thosakwe Date: Fri, 20 Jan 2017 18:57:42 -0500 Subject: [PATCH] Broken --- lib/base_angel_client.dart | 6 +++++- test/browser.dart | 11 ----------- test/browser_test.dart | 37 +++++++++++++++++++++++++++++++++++++ test/for_browser_tests.dart | 21 +++++++++++++++------ 4 files changed, 57 insertions(+), 18 deletions(-) delete mode 100644 test/browser.dart create mode 100644 test/browser_test.dart diff --git a/lib/base_angel_client.dart b/lib/base_angel_client.dart index d44a1ee4..1614b397 100644 --- a/lib/base_angel_client.dart +++ b/lib/base_angel_client.dart @@ -81,7 +81,7 @@ abstract class BaseAngelClient extends Angel { if (json is! Map || !json.containsKey('data') || !json.containsKey('token')) { - throw new AngelHttpException.NotAuthenticated( + throw new AngelHttpException.notAuthenticated( message: "Auth endpoint '$url' did not return a proper response."); } @@ -200,6 +200,10 @@ class BaseAngelService extends Service { var request = new http.Request(method, url); if (headers != null) request.headers.addAll(headers); + + if (app.authToken?.isNotEmpty == true) + request.headers['Authorization'] = 'Bearer ${app.authToken}'; + if (encoding != null) request.encoding = encoding; if (body != null) { if (body is String) { diff --git a/test/browser.dart b/test/browser.dart deleted file mode 100644 index 5eb50ac0..00000000 --- a/test/browser.dart +++ /dev/null @@ -1,11 +0,0 @@ -import 'package:angel_client/browser.dart'; -import 'package:test/test.dart'; - -main() async { - test("list todos", () async { - Angel app = new Rest("http://localhost:3001"); - Service Todos = app.service("todos"); - - print(await Todos.index()); - }); -} diff --git a/test/browser_test.dart b/test/browser_test.dart new file mode 100644 index 00000000..8eb28db9 --- /dev/null +++ b/test/browser_test.dart @@ -0,0 +1,37 @@ +@TestOn('browser') +import 'package:angel_client/browser.dart'; +import 'package:test/test.dart'; +import 'for_browser_tests.dart'; + +main() { + test("list todos", () async { + var channel = spawnHybridCode(SERVER); + int port = await channel.stream.first; + var url = "http://localhost:$port"; + print(url); + var app = new Rest(url); + var todoService = app.service("todos"); + + var todos = await todoService.index(); + expect(todos, isEmpty); + }); + + test('create todos', () async { + var channel = spawnHybridCode(SERVER); + int port = await channel.stream.first; + var url = "http://localhost:$port"; + print(url); + var app = new Rest(url); + var todoService = app.service("todos"); + + var data = {'hello': 'world'}; + var response = await todoService.create(data); + print('Created response: $response'); + + var todos = await todoService.index(); + expect(todos, hasLength(1)); + + Map todo = todos.first; + expect(todo, equals(data)); + }); +} diff --git a/test/for_browser_tests.dart b/test/for_browser_tests.dart index d020dc92..e23cb79a 100644 --- a/test/for_browser_tests.dart +++ b/test/for_browser_tests.dart @@ -1,21 +1,30 @@ +const String SERVER = ''' import 'dart:io'; import "package:angel_framework/angel_framework.dart"; import "package:angel_framework/src/defs.dart"; +import 'package:stream_channel/stream_channel.dart'; + +hybridMain(StreamChannel channel) async { + var app = new Angel(); -main() async { - Angel app = new Angel(); app.before.add((req, ResponseContext res) { - res.header("Access-Control-Allow-Origin", "*"); + res.headers["Access-Control-Allow-Origin"] = "*"; + return true; }); app.use("/todos", new MemoryService()); - await app.startServer(InternetAddress.LOOPBACK_IP_V4, 3001); - print("Server up on localhost:3001"); + var server = await app.startServer(InternetAddress.LOOPBACK_IP_V4, 0); + + print("Server up; listening at http://localhost:\${server.port}"); + channel.sink.add(server.port); } class Todo extends MemoryModel { String hello; - Todo({String this.hello}); + Todo({int id, this.hello}) { + this.id = id; + } } +''';