我在基于游戏的API中有几个控制器,使用isMaxSizeExceeded来限制请求的大小。 我通过将 parsers.text.maxLength = 100K 添加到项目中的 application.conf 来设置此限制。
当我在调试模式下运行API时它工作正常:当我发送带有大表单数据的请求时,此方法返回true,如果我发送小的,则返回false。
但是当我在单元测试中测试这个控制器时,无论我发送的请求有多大,这个方法都返回false。
我的控制器就像:
@Security.Authenticated(AdminSecure.class)
public static Result MyAction() {
// some auth check
// ...
if (request().body().isMaxSizeExceeded()) {
return ok("413: request too large");
}
// some saving and updating here
// ...
return ok("ok");
}
这是我的测试代码:
@Test
public void MyControllerTest() {
FakeRequest fakeReq = getLoggedInFakeRequest(); // a method from my helper class
Map<String, String> resourceForm = new HashMap<String, String>();
String jsonStr = "SOME REALLY HUGE STRING"; // this value is actually read from a file which contains the same text I used in debug test.
resourceForm.put("resource", jsonStr);
fakeReq.withFormUrlEncodedBody(resourceForm);
Result result =
Helpers.callAction(controllers.routes.ref.Resources.MyController(), fakeReq);
assertThat(isResultContainErrorMessage(result, "413: request too large")); // isResultContainErrorMessage is a method from my helper class
}
顺便说一下,我正在使用游戏2.3.1 JAVA。
这是Play的问题还是我测试控制器的方法有问题?
谢谢!
以下是我在Google群组中提出的原始问题:
https://groups.google.com/forum/#!topic/play-framework/fjbO2hNwXso