【问题标题】:How to mock server response - client on server side如何模拟服务器响应 - 服务器端的客户端
【发布时间】:2014-10-31 10:04:16
【问题描述】:

我正在尝试飞镖,我正在服务器端编写一个客户端:

new HttpClient().post(InternetAddress.LOOPBACK_IP_V4.host, 7474, '/path').then((HttpClientRequest request) {
request.headers.contentType = ContentType.JSON;
request.headers.add(HttpHeaders.ACCEPT, ContentType.JSON);
request.write(JSON.encode(jsonData));

return request.close();
}).then((HttpClientResponse response) {
response.transform(UTF8.decoder).listen((contents) {
  _logger(contents);
  // stuff
});
});

我想模拟服务器响应。

什么是最好的解决方案?

  • 在我的测试类中创建一个服务器,它将返回我期望的值?
  • 还是模拟 HttpClientResponse ?

感谢您的帮助! (代码将不胜感激;D)

【问题讨论】:

    标签: dart dart-unittest


    【解决方案1】:

    http packages 对此提供支持。

    有关示例,请参阅 http://www.dartdocs.org/documentation/http/0.11.1+1/index.html#http/http-testing。

    import 'dart:convert';
    import 'package:http/testing.dart';
    
    var client = new MockClient((request) {
      if (request.url.path != "/data.json") {
        return new Response("", 404);
      }
      return new Response(JSON.encode({
        'numbers': [1, 4, 15, 19, 214]
      }, 200, headers: {
        'content-type': 'application/json'
      });
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多