From d588411d6785fd7ed045353b1c60f6bde4640a6d Mon Sep 17 00:00:00 2001 From: thosakwe Date: Fri, 24 Jun 2016 15:02:35 -0400 Subject: [PATCH] More --- lib/angel_client.dart | 13 ++++++++++++- lib/{src => }/rest.dart | 21 ++++++++++++++------- test/packages | 1 + test/rest.dart | 37 +++++++++++++++++++++++++++++++++++++ test/shared.dart | 6 ++++++ 5 files changed, 70 insertions(+), 8 deletions(-) rename lib/{src => }/rest.dart (83%) create mode 120000 test/packages create mode 100644 test/rest.dart create mode 100644 test/shared.dart diff --git a/lib/angel_client.dart b/lib/angel_client.dart index 7982440c..6a73fde3 100644 --- a/lib/angel_client.dart +++ b/lib/angel_client.dart @@ -2,7 +2,18 @@ library angel_client; import 'dart:async'; -export 'src/rest.dart'; +import 'dart:convert' show JSON; +import 'package:http/http.dart'; +part 'rest.dart'; + + +/// Represents an Angel server that we are querying. +abstract class Angel { + String basePath; + + Angel(String this.basePath); + Service service(Pattern path); +} /// Queries a service on an Angel server, with the same API. abstract class Service { diff --git a/lib/src/rest.dart b/lib/rest.dart similarity index 83% rename from lib/src/rest.dart rename to lib/rest.dart index e1144be7..e68c3424 100644 --- a/lib/src/rest.dart +++ b/lib/rest.dart @@ -1,9 +1,4 @@ -library angel_client.rest; - -import 'dart:async'; -import 'dart:convert' show JSON; -import 'package:http/http.dart'; -import '../angel_client.dart'; +part of angel_client; _buildQuery(Map params) { if (params == null || params == {}) @@ -22,12 +17,24 @@ const Map _writeHeaders = const { "Content-Type": "application/json" }; +class Rest extends Angel { + BaseClient client; + + Rest(String path, BaseClient this.client):super(path); + + @override + RestService service(String path) { + String uri = path.replaceAll(new RegExp(r"(^\/)|(\/+$)"), ""); + return new RestService._base("$basePath/$uri", client); + } +} + /// Queries an Angel service via REST. class RestService extends Service { String basePath; BaseClient client; - RestService(Pattern path, BaseClient this.client) { + RestService._base(Pattern path, BaseClient this.client) { this.basePath = (path is RegExp) ? path.pattern : path; } diff --git a/test/packages b/test/packages new file mode 120000 index 00000000..a16c4050 --- /dev/null +++ b/test/packages @@ -0,0 +1 @@ +../packages \ No newline at end of file diff --git a/test/rest.dart b/test/rest.dart new file mode 100644 index 00000000..055e65a3 --- /dev/null +++ b/test/rest.dart @@ -0,0 +1,37 @@ +import 'dart:io'; +import 'package:angel_client/angel_client.dart' as client; +import 'package:angel_framework/angel_framework.dart' as server; +import 'package:http/http.dart' as http; +import 'package:test/test.dart'; +import 'shared.dart'; + +main() { + group("rest", () { + server.Angel serverApp = new server.Angel(); + server.HookedService serverPostcards; + client.Angel clientApp; + client.Service clientPostcards; + HttpServer httpServer; + + setUp(() async { + httpServer = + await serverApp.startServer(InternetAddress.LOOPBACK_IP_V4, 3000); + serverApp.use("/postcards", new server.MemoryService()); + serverPostcards = serverApp.service("postcards"); + + clientApp = new client.Rest("http://localhost:3000", new http.Client()); + clientPostcards = clientApp.service("postcards"); + }); + + tearDown(() async { + await httpServer.close(force: true); + }); + + test("index", () async { + Postcard niagaraFalls = await serverPostcards.create( + new Postcard(location: "Niagara Falls", message: "Missing you!")); + List indexed = await clientPostcards.index(); + print(indexed); + }); + }); +} diff --git a/test/shared.dart b/test/shared.dart new file mode 100644 index 00000000..4a8d2052 --- /dev/null +++ b/test/shared.dart @@ -0,0 +1,6 @@ +class Postcard { + String location; + String message; + + Postcard({String this.location, String this.message}); +} \ No newline at end of file