gpt4 book ai didi

rest - 功能测试:通过Grails中的REST API上传文件

转载 作者:行者123 更新时间:2023-12-02 15:22:04 26 4
gpt4 key购买 nike

我正在使用Grails的功能测试插件为我的RESTful 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
}}

我不确定如何将文件放置在正文中,我尝试将其添加为参数,但没有任何效果。

最佳答案

此代码有效!

   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"

}

关于rest - 功能测试:通过Grails中的REST API上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33753441/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com