我正在测试使用 HTTP 服务器的应用程序。我没有 mock ,而是决定使用 HTTP 服务器装置。这意味着我不必模拟任何生产代码。为了实现这个目标,我目前选择免费使用第 3 方库 fixd .
我能够成功创建多个单元测试 - 它们通过 GET 请求运行。大多数都非常简单,即:
@Test
public void verifyConnectionTest()
{
try
{
final String body = FileUtils.readFileToString(RESOURCE);
final String path = "/";
this.server.handle(Method.GET, path).with(
new HttpRequestHandler() {
@Override
public void handle(final HttpRequest request,
final HttpResponse response)
{
response.setStatusCode(200);
response.setContentType("text/xml");
response.setBody(body);
}
});
// Setting up my HTTP client
// Execute some tasks
// asserting of everything was valid
}
catch (final IOException e)
{
fail(e.getMessage());
}
}
但我现在必须发送带有 multipart/form-data 的 POST 请求。除了更改方法和内容类型之外,这没有太大区别:
@Test
public void executeStepTest()
{
try
{
final String body = FileUtils.readFileToString(SERVICE_RESPONSE);
final String path = "/";
this.server.handle(Method.POST, path, "multipart/form-data").with(
new HttpRequestHandler() {
@Override
public void handle(final HttpRequest request,
final HttpResponse response)
{
response.setStatusCode(200);
response.setContentType("text/xml");
response.setBody(body);
}
});
// Setting up my HTTP client
// Execute some tasks
// asserting of everything was valid
}
catch (final IOException e)
{
fail(e.getMessage());
}
}
但是我收到以下错误:[ERROR] 找不到 POST 的处理程序 -/- multipart/form-data; boundary=bqCBI7t-VW1xaJW7BADmTiGMg9w_YM2sHH8ukJYx
我猜 fixd 不识别边界方。由于文档没有显示示例,因此我对这部分很困惑。
我尝试使用一些通配符,例如“*”,但没有成功。因此;我需要一种方法来告诉 fixd 接受该边界或使用一些我尚未发现的通配符。任何帮助都会很棒,谢谢!
最佳答案
我一直在进行一些调试,看来问题出在 fixd
核心。
基本上,fixd
通过 HandlerKey
(包括 ContentType 作为键的一部分)为每个 RequestHandlerImpl
编制索引在 map handlerMap
中。请参阅方法 org.bigtesting.fixd.core.FixtureContainer#resolve
。
...
HandlerKey key = new HandlerKey(method, route, contentType);
RequestHandlerImpl handler = handlerMap.get(key);
if (handler == null) {
// Error
}
...
问题:当请求是multipart/form-data
时,boundary
数据(它在每个请求中动态生成)是内容类型。因此,任何处理程序都可以在 handlerMap
中找到,因为 key 会随着每次运行而变化。
我做了一些测试只是为了检查这是否是问题的原因,在创建多部分请求后将 contentType 传递给 fixd server.handle
,它工作正常。
请看下面的测试:
@Test
public void verifyConnectionTest_multipart() {
try {
// 1. Create multipart request (example with http-commons 3.1)
PostMethod filePost = new PostMethod(url);
Part[] parts = { new StringPart("param", "value") };
MultipartRequestEntity request = new MultipartRequestEntity(parts, filePost.getParams());
filePost.setRequestEntity(request);
// 2. fixd server handle (passing the request content type)
this.server.handle(Method.POST, "/", request.getContentType()).with(
new HttpRequestHandler() {
@Override
public void handle(final HttpRequest request,
final HttpResponse response) {
response.setStatusCode(200);
response.setContentType("text/xml");
}
});
// 3. Execute multipart request
HttpClient client = new HttpClient();
int status = client.executeMethod(filePost);
// 4. Assertions
Assert.assertEquals(200, status);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
希望对您解决问题有所帮助。干杯
关于java - 找不到带边界的 POST 处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25624161/