我有一个最小的(示例)REST端点test/people.cfc
:
component restpath = "test/people/" rest = true { remote void function create( required string first_name restargsource = "Form", required string last_name restargsource = "Form" ) httpmethod = "POST" restpath = "" produces = "application/json" { // Simulate adding person to database. ArrayAppend( Application.people, { "first_name" = first_name, "last_name" = last_name } ); // Simulate getting people from database. var people = Application.people; restSetResponse( { "status" = 201, "content" = SerializeJSON( people ) } ); } }
如此处和ColdFusion文档中所述:
注意:ColdFusion忽略函数的返回值,并使用函数使用响应集
RestSetResponse()
.
因此,void
函数的返回类型似乎对REST函数是正确的.
现在,我知道我可以使用以下命令从CFM页面调用它:
httpService = new http(method = "POST", url = "https://localhost/rest/test/people"); httpService.addParam( name = "first_name", type = "formfield", value = "Alice" ); httpService.addParam( name = "last_name", type = "formfield", value = "Adams" ); result = httpService.send().getPrefix();
但是,我想在不发出HTTP请求的情况下调用该函数.
首先,REST CFC似乎无法从REST目录中访问.这可以通过在ColdFusion管理面板中创建到REST服务的根路径的映射来简单地解决.
然后我可以这样做:
Application.people = []; people = new restmapping.test.People(); people.create( "Alice", "Adams" ); WriteDump( application.people );
这直接调用函数,输出显示它已添加了该函数.但是,REST功能的响应已经消失在以太网中.有谁知道是否有可能检索响应的HTTP状态代码和内容(至少是 - 最好是所有HTTP头)?
更新 - 集成测试场景:
这是一个用例(多个),其中通过HTTP请求调用REST端点具有连锁效应,可以通过直接调用端点作为组件的方法来缓解这种效应.
// Create an instance of the REST end-point component without // calling it via HTTP request. endPoint = new restfiles.test.TestRESTEndPoint(); transaction { try { // Call a method on the end-point without making a HTTP request. endPoint.addValueToDatabase( 1, 'abcd' ); assert( getRESTStatusCode(), 201 ); assert( getRESTResponseText(), '{"id":1,"value":"abcd"}' ); // Call another method on the end-point without making a HTTP request. endPoint.updateValueInDatabase( 1, 'dcba' ); assert( getRESTStatusCode(), 200 ); assert( getRESTResponseText(), '{"id":1,"value":"dcba"}' ); // Call a third method on the end-point without making a HTTP request. endPoint.deleteValueInDatabase( 1 ); assert( getRESTStatusCode(), 204 ); assert( getRESTResponseText(), '' ); } catch ( any e ) { WriteDump( e ); } finally { transaction action="rollback"; } }
通过HTTP请求调用每个REST函数将在每次请求之后将数据提交到数据库 - 在提交数据的测试之间进行清理会变得非常复杂并且经常导致需要将数据库闪回到先前的状态(导致集成)测试无法与闪回期间的任何其他测试和不可用时段并行运行.能够在不进行大量原子HTTP请求的情况下调用REST端点,而是将它们捆绑到可以回滚的单个事务中,这意味着可以在单个用户的会话中执行测试.
那么,如何获取在RestSetResponse()
创建REST组件实例时直接调用表示REST路径的函数(不使用HTTP请求)时设置的HTTP状态代码和响应文本?