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

Adiciona testes iniciais #14

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
32 changes: 24 additions & 8 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ packages:
dependency: transitive
description:
name: _fe_analyzer_shared
sha256: "45cfa8471b89fb6643fe9bf51bd7931a76b8f5ec2d65de4fb176dba8d4f22c77"
sha256: "16e298750b6d0af7ce8a3ba7c18c69c3785d11b15ec83f6dcd0ad2a0009b3cab"
url: "https://pub.dev"
source: hosted
version: "73.0.0"
version: "76.0.0"
_macros:
dependency: transitive
description: dart
source: sdk
version: "0.3.2"
version: "0.3.3"
analyzer:
dependency: transitive
description:
name: analyzer
sha256: "4959fec185fe70cce007c57e9ab6983101dbe593d2bf8bbfb4453aaec0cf470a"
sha256: "1f14db053a8c23e260789e9b0980fa27f2680dd640932cae5e1137cce0e46e1e"
url: "https://pub.dev"
source: hosted
version: "6.8.0"
version: "6.11.0"
args:
dependency: transitive
description:
Expand Down Expand Up @@ -118,6 +118,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.2"
http_methods:
dependency: transitive
description:
name: http_methods
sha256: "6bccce8f1ec7b5d701e7921dca35e202d425b57e317ba1a37f2638590e29e566"
url: "https://pub.dev"
source: hosted
version: "1.1.1"
http_mock_adapter:
dependency: "direct main"
description:
Expand Down Expand Up @@ -186,10 +194,10 @@ packages:
dependency: transitive
description:
name: macros
sha256: "0acaed5d6b7eab89f63350bccd82119e6c602df0f391260d0e32b5e23db79536"
sha256: "1d9e801cd66f7ea3663c45fc708450db1fa57f988142c64289142c9b7ee80656"
url: "https://pub.dev"
source: hosted
version: "0.1.2-main.4"
version: "0.1.3-main.0"
matcher:
dependency: transitive
description:
Expand Down Expand Up @@ -255,7 +263,7 @@ packages:
source: hosted
version: "2.1.4"
shelf:
dependency: transitive
dependency: "direct dev"
description:
name: shelf
sha256: e7dd780a7ffb623c57850b33f43309312fc863fb6aa3d276a754bb299839ef12
Expand All @@ -270,6 +278,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.0.2"
shelf_router:
dependency: "direct dev"
description:
name: shelf_router
sha256: f5e5d492440a7fb165fe1e2e1a623f31f734d3370900070b2b1e0d0428d59864
url: "https://pub.dev"
source: hosted
version: "1.1.4"
shelf_static:
dependency: transitive
description:
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ dependencies:

dev_dependencies:
lints: ^4.0.0
shelf: ^1.4.2
shelf_router: ^1.1.4
test: ^1.25.5
185 changes: 185 additions & 0 deletions test/main_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import 'dart:convert';
import 'dart:io';

import 'package:pop_network/pop_network.dart' hide Response;
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart';
import 'package:shelf_router/shelf_router.dart';
import 'package:test/test.dart';

Future<void> main() async {
final address = InternetAddress.loopbackIPv4.address;
const port = 6969;
final baseUrl = 'http://${address}:$port';

Future<HttpServer> createServer(Router router) async {
return serve(router, address, port);
}

group('base cases', () {
late ApiManager apiManager;
late HttpServer server;

final temporaryFile = File('${Directory.systemTemp}/data.json');

setUp(() async {
apiManager = ApiManager(
baseUrl: baseUrl,
);
server = await createServer(
Router()
..get('/get', (_) => Response.ok('OK'))
..post('/post', (_) => Response.ok('OK'))
..patch('/patch', (_) => Response.ok('OK'))
..put('/put', (_) => Response.ok('OK'))
..delete('/delete', (_) => Response.ok('OK')),
);
});

tearDown(() async {
await Future.wait([
if (await temporaryFile.exists()) temporaryFile.delete(),
server.close(),
]);
});

test('GET works', () async {
final response = await apiManager.get('/get');
expect(response.data, 'OK');
});

test('POST works', () async {
final response = await apiManager.post('/post');
expect(response.data, 'OK');
});

test('PATCH works', () async {
final response = await apiManager.patch('/patch');
expect(response.data, 'OK');
});

test('PUT works', () async {
final response = await apiManager.put('/put');
expect(response.data, 'OK');
});

test('DELETE works', () async {
final response = await apiManager.delete('/delete');
expect(response.data, 'OK');
});

test('download works', () async {
await apiManager.download('/get', temporaryFile.path);
expect(temporaryFile.exists(), completion(true));
expect(temporaryFile.readAsString(), completion('OK'));
});
});

group('mocked cases', () {
const replyParams = const MockReplyParams(mockPath: '/foo/bar/baz.json');
const data = {'message': 'OK'};

late ApiManager apiManager;
late HttpServer server;

setUp(() async {
apiManager = ApiManager(
baseUrl: baseUrl,
loadMockAsset: (assetPath) async {
if (assetPath == replyParams.mockPath) {
return jsonEncode(data);
}
throw Exception('Unknown asset $assetPath');
},
);
server = await createServer(
Router()
..get('/get', (_) => Response.ok('OK'))
..post('/post', (_) => Response.ok('OK'))
..patch('/patch', (_) => Response.ok('OK'))
..put('/put', (_) => Response.ok('OK'))
..delete('/delete', (_) => Response.ok('OK')),
);
});

tearDown(() async {
await server.close();
});

test('GET can be mocked', () async {
final response = await apiManager.get(
'/get',
mockReplyParams: replyParams,
);
expect(response.data, data);
});

test('POST can be mocked', () async {
final response = await apiManager.post(
'/post',
mockReplyParams: replyParams,
);
expect(response.data, data);
});

test('PATCH can be mocked', () async {
final response = await apiManager.patch(
'/patch',
mockReplyParams: replyParams,
);
expect(response.data, data);
});

test('PUT can be mocked', () async {
final response = await apiManager.put(
'/put',
mockReplyParams: replyParams,
);
expect(response.data, data);
});

test('DELETE can be mocked', () async {
final response = await apiManager.delete(
'/delete',
mockReplyParams: replyParams,
);
expect(response.data, data);
});
});

group('custom HttpClient', () {
late CustomHttpClientFactory httpClientFactory;
late ApiManager apiManager;
late HttpServer server;

setUp(() async {
httpClientFactory = CustomHttpClientFactory();
apiManager = ApiManager(
baseUrl: baseUrl,
createHttpClient: httpClientFactory,
);
server = await createServer(
Router()..get('/get', (_) => Response.ok('OK')),
);
});

tearDown(() async {
await server.close();
});

test('works', () async {
final response = await apiManager.get('/get');
expect(response.data, 'OK');
expect(httpClientFactory.called, isTrue);
});
});
}

final class CustomHttpClientFactory {
bool called = false;

HttpClient call() {
called = true;
return HttpClient();
}
}