我正在尝试测试在shelf_rest上运行的Dart REST 应用程序。假设设置类似于shelf_rest
示例,如何在不实际运行 HTTP 服务器的情况下测试配置的路由?
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as io;
import 'package:shelf_rest/shelf_rest.dart';
void main() {
var myRouter = router()
..get('/accounts/{accountId}', (Request request) {
var account = new Account.build(accountId: getPathParameter(request, 'accountId'));
return new Response.ok(JSON.encode(account));
});
io.serve(myRouter.handler, 'localhost', 8080);
}
class Account {
final String accountId;
Account.build({this.accountId});
Account.fromJson(Map json) : this.accountId = json['accountId'];
Map toJson() => {'accountId': accountId};
}
class AccountResource {
@Get('{accountId}')
Account find(String accountId) => new Account.build(accountId: accountId);
}
在不涉及太多额外逻辑的情况下,如何对 GETaccount
端点进行单元测试?我想运行的一些基本测试是:
GET /accounts/123
返回 200GET /accounts/bogus
返回 404