Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow passing uri directly to Spookie #111

Merged
merged 2 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading