2015-11-17 23 views
0

我正在使用Grails的功能测试插件为我的RESTful API项目准备功能测试用例。功能测试:通过Grails中的REST API上传文件

我无法使用适用于我的案例中的其他所有方法的技术上传文件。

class CreateFunctionalSpec{ 

final String CREATE_API = '/document-file' 

def "Upload Document to temporary location"() { 

    File nfile = new File('test/test-data/myfile.jpg') 
    nfile.createNewFile() 

    when: 
    post("$RESTFUL_API_BASE_URL${CREATE_API}") { 
     headers['Accept'] = 'application/json' 
     headers['Authorization'] = authHeader() 
     body{ 
      "file:nfile"  
     } 
    } 


    then: 
     assert file 
}} 

我不确定如何将文件放入正文中,我尝试将它添加为参数,但没有任何作用。

回答

1

此代码有效!

def "Upload Document to temporary location"() { 

    setup: 
    def testDocument = new File('test/test-data/test-document.jpg') 

    when: 
    post("$RESTFUL_API_BASE_URL${BDM_CREATE_API}") { 
     headers['Accept'] = 'application/json' 
     headers['Authorization'] = authHeader() 
     body{ 
      setProperty("file",testDocument) 
     } 
    } 
    then: 
    201 == response.status 
    def jsonData = JSON.parse response.text 
    jsonData["status"]=="Success" 

}