[英]How to mock server response - client on server side
我正在嘗試飛鏢,我正在服務器端編寫一個客戶端:
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
});
});
我想模仿服務器響應。
什么是最好的解決方案?
謝謝你的幫助 ! (代碼將非常感謝; D)
http包提供了對此的支持。
有關示例,請參見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'
});
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.