Skip to content

Commit

Permalink
feat: Allow passing uri directly to Spookie (#111)
Browse files Browse the repository at this point in the history
* allow passing uri directly to spookie

* tiny fix
  • Loading branch information
codekeyz authored Feb 2, 2024
1 parent 2827a8a commit 2071f9b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
34 changes: 17 additions & 17 deletions packages/spookie/lib/spookie.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import 'src/http_expectation.dart';

export 'package:test/test.dart';

typedef TestApp = Function(HttpRequest req);
typedef HttpRequestHandler = Function(HttpRequest req);

abstract interface class Spookie {
late final HttpServer _server;
factory Spookie.server(HttpServer server) =>
_$SpookieImpl(getServerUri(server));

Spookie._(HttpServer server) : _server = server;
factory Spookie.uri(Uri uri) => _$SpookieImpl(uri);

Spookie auth(String user, String pass);

Expand Down Expand Up @@ -46,14 +47,14 @@ abstract interface class Spookie {
});
}

class _$SpookieImpl extends Spookie {
Uri get serverUri => getServerUri(_server);
class _$SpookieImpl implements Spookie {
final Uri baseUri;

_$SpookieImpl(super.server) : super._() {
_$SpookieImpl(this.baseUri) {
_headers.clear();
}

Uri getUri(String path) => Uri.parse('$serverUri$path');
Uri getUri(String path) => baseUri.replace(path: path);

final Map<String, String> _headers = {};

Expand Down Expand Up @@ -139,18 +140,18 @@ class _$SpookieImpl extends Spookie {
}

class SpookieAgent {
static _$SpookieImpl? _instance;
static Spookie? _instance;
static HttpServer? _server;

static Future<Spookie> create(TestApp app) async {
static Future<Spookie> create(HttpRequestHandler app) async {
if (_instance != null) {
await _instance!._server.close();
_instance = null;
await _server?.close();
_server = null;
}

final server = await HttpServer.bind('127.0.0.1', 0)
_server = await HttpServer.bind(InternetAddress.anyIPv4, 0)
..listen(app);
_instance = _$SpookieImpl(server);
return _instance!;
return _instance = Spookie.server(_server!);
}
}

Expand All @@ -171,7 +172,6 @@ Uri getServerUri(HttpServer server) {
return Uri(scheme: 'http', host: server.address.address, port: server.port);
}

Future<Spookie> request<T>(T app) async {
final tester = await SpookieAgent.create((app as dynamic).handleRequest);
return tester;
Future<Spookie> request<T extends Object>(T app) async {
return SpookieAgent.create((app as dynamic).handleRequest);
}
11 changes: 8 additions & 3 deletions packages/spookie/test/spookie_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ void main() {

test('should work with an active server', () async {
final app = Pharaoh()
..post('/hello', (req, res) => res.ok('Hello World'));
await (await (request<Pharaoh>(app))).get('/').expectStatus(404).test();
await (await (request<Pharaoh>(app)))
..post('/hello', (req, res) => res.ok('Active Server'));

await app.listen(port: 5050);

await Spookie.uri(app.uri)
.post('/hello', {})
.expectBody('Active Server')
.expectStatus(200)
.test();

await app.shutdown();
});

test('should work with remote server', () async {
Expand Down

0 comments on commit 2071f9b

Please sign in to comment.