序列化多部分表单请求以在Play 2.1上进行测试 [英] Serializing Multipart Form requests for testing on Play 2.1
问题描述
我正在play2.1上为使用route函数使用多部分表单的后置控制器编写测试.
I'm working on play2.1 writing a test for a post controller that uses multipart forms using the route function.
route(
FakeRequest(POST,
postControllerRoute().url,
FakeHeaders(Seq(HeaderNames.CONTENT_TYPE -> Seq("multipart/form-data"))),
body = body
).withAuthToken.withAdmin(adminId))
我发现,要使此代码正常工作,我需要定义此类型的Writeable Writeable [MultipartFormData [TemporaryFile]],因为我的主体变量的类型为MultipartFormData [TemporaryFile].我不确定如何序列化多部分请求,或者这是否是正确的方法.有什么建议吗?
I've found that for this code to work I need to define a writeable of this type Writeable[MultipartFormData[TemporaryFile]] since my body variable is of type MultipartFormData[TemporaryFile]. I'm not sure how to serialize a multipart request or if this is even the right approach. Any suggestions?
已编辑
答案:跳过路由器并直接使用控制器,就像在文档中一样: http://www.playframework.com/documentation/2.1.0/ScalaFunctionalTest
Answer: Skip the router and use the controller directly like in the docs: http://www.playframework.com/documentation/2.1.0/ScalaFunctionalTest
推荐答案
- 您有两个选择,对
MultiPartFormData
进行序列化(据我所知)非常棘手.这篇文章可能会有所帮助: https://groups.google .com/forum/?fromgroups =#!topic/play-framework/MPtQlX-cWMQ - 您可以跳过
route
方法.如果要在控制器中测试Action
,则只需绕过路由即可.请参阅文档中的测试控制器.如果不通过路由器,则无需提供Writable
.很少需要通过路由器,从本质上讲,您首先可以将其写为字节(使用Writer
),然后(使用主体解析器)将其转换回Play可以理解的格式. - You have two options, searializing
MultiPartFormData
which is (as far as I can tell) quite tricky to do. This post might help: https://groups.google.com/forum/?fromgroups=#!topic/play-framework/MPtQlX-cWMQ - You can skip going though the
route
method. If you want to test theAction
in your controller, you can simply bypass the route. See Testing your Controllers in the documentation. If you do not go through the router, you don't need to supply aWritable
. It is rarely needed to go through the router which essentially let's you first write it as bytes (usingWriter
) and then (using the body parser) convert it back to something Play understands.
这篇关于序列化多部分表单请求以在Play 2.1上进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!