[英]Dart make custom HttpRequest
I want to make a custom HttpRequest to unit test my api code.我想制作一个自定义 HttpRequest 来对我的 api 代码进行单元测试。 The code I want to test:我要测试的代码:
if (WebSocketTransformer.isUpgradeRequest(request)) {
await webSocketRequest.handleRequest(
request: request, streamController: streamController);
}
The method WebSocketTransformer
checks the method and the headers content. WebSocketTransformer
方法检查方法和标头内容。 Therefore I have to hand in a HttpRequest
.因此我必须提交一个HttpRequest
。
HttpRequest mockWSRequest;
mockWSrequest.headers.set('connection', 'upgrade');
But how can I set the HttpRequest
's method?但是如何设置HttpRequest
的方法?
If you're talking about dart:io
's HttpRequest
class, that class is abstract
and cannot be directly instantiated.如果你说的是dart:io
的HttpRequest
class,那 ZA2F2ED4F8DCEBC2CBBDZ1 是不能直接abstract
化的。 If you want to create a mock, then you would have your mock implement the HttpRequest
interface and add an implementation for its method
getter to return 'GET'
or 'POST'
as desired.如果您想创建一个模拟,那么您将让模拟实现HttpRequest
接口并为其method
getter添加一个实现,以根据需要返回'GET'
或'POST'
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.