我想编写一个功能测试来检查我的android应用程序是否正在发送包含正确文件的HTTP请求(它是一个多部分http请求)
public class TestClass {
@Rule public final MockWebServer server = new MockWebServer();
@Test
public void testSendValuesTextJobTest() throws Exception {
// setting up a mock response on my fake server
server.enqueue(new MockResponse()
.setResponseCode(200)
//use android instrumentation to send the HTTP request
//containing a multipart file
...
// Retrieve the HTTP request received by the server
RecordedRequest request = server.takeRequest(5, TimeUnit.SECONDS);
String body = request.getBody().readUtf8(); // The raw http request
// Here, I would like to retrieve the file data in the request body
// and compare it to the source file
}
}
这是body的值:
--ec7db176-a2fa-402c-84a5-7e24634be58a
Content-Disposition: form-data; name="data"
Content-Transfer-Encoding: binary
Content-Type: image/jpeg
Content-Length: 54509
������JFIF�� [...more data...]
--ec7db176-a2fa-402c-84a5-7e24634be58a--
使用现有的HTTP请求解析器来获取多部分正文。虽然我找不到一个接受String作为输入的。 (通常它是一个HttpServletRequest)
直接从缓冲区获取数据,但我不知道如何跳过前6行标题和最后一行标题。我甚至不知道是否必须处理某种编码。
您是否知道我可以在其他测试中重复使用的简单方法?