grails - Groovy 脚本向 Grails Web App 发送多部分请求以进行测试

标签 grails groovy gradle multipart

我正在尝试通过从 gradle 构建的独立 groovy 测试脚本创建和发送多部分请求来测试我的 Grails Web 应用程序。但我在挣扎。

  • 我无法附加自定义 Content-ID header
  • 我无法附加运行时创建的随机字节文件(我可以附加现有文件,但我需要许多不同大小的随机文件)

  • 编辑(感谢至强):
  • 我的脚本现在正在发送有效的多部分请求,但由于某种原因,我的 grails Web 应用程序不接受“Content-Type”以外的任何 header 。

  • 这是我的代码:

    独立测试脚本代码:
    void sendMultipartRequest(String url) {
        HTTPBuilder httpBuilder = new HTTPBuilder(url)
        httpBuilder.request(Method.POST){ req ->
    
            MultipartEntityBuilder entityBuilder = new MultipartEntityBuilder()
            entityBuilder.setBoundary("----boundary")
    
            entityBuilder.setMode(HttpMultipartMode.RFC6532)
    
            String randomString = myGenerateRandomStringMethod()
            FormBodyPart formBodyPart = new FormBodyPart(
                "SOME_NAME", 
                new InputStreamBody(new ByteArrayInputStream(randomString.bytes), "attachment", "SOME_NAME")
            )
            formBodyPart.addField("Content-ID", "abc123")
            entityBuilder.addPart(formBodyPart)
    
            response.success = { resp ->
                println("Success with response ${resp.toString()}")
            }
            response.failure = { resp ->
                println("Failure with response ${resp.toString()}")
            }
    
            delegate.setHeaders(["Content-Type":"multipart/related; boundary=----boundary"])
            req.setEntity(entityBuilder.build())
        }
    }
    

    Controller 中用于处理帖子的 Grails web-app 端:
    def submitFiles() {
        if(request instanceof MultipartHttpServletRequest){
            HashMap<String, Byte[]> fileMap = extractMultipartFiles(request)
            someService.doStuffWith(fileMap)
        }
    }
    private HashMap<String, Byte[]> extractMultipartFiles(MultipartHttpServletRequest multipartRequest) {
        HashMap<String, Byte[]> files = new HashMap<>()
        for(element in mulipartRequest.multiFileMap){
            MultipartFile file = element.value.first()
            String contentId = multipartRequest.getMultipartHeaders(element.key).get("Content-ID")?.first()
            if(contentId) files.put(contentId, file.getBytes())
        }
        return files
    }
    

    我正在使用的库:
    ext {
        groovyVersion = "2.3.4"
        commonsLangVersion = "2.6"
        httpBuilderVersion = "0.7.1"
        httpmimeVersion = "4.3.4"
        junitVersion = "4.11"
    }
    dependencies {
        compile "org.codehaus.groovy:groovy-all:${groovyVersion}"
        compile "commons-lang:commons-lang:${commonsLangVersion}"
        compile "org.codehaus.groovy.modules.http-builder:http-builder:${httpBuilderVersion}"
        compile "org.apache.httpcomponents:httpmime:${httpmimeVersion}"
        testCompile group: 'junit', name: 'junit', version: "${junitVersion}"
    } 
    

    最佳答案

    你总是可以使用 ContentBody 的一些子类。界面:

    FormBodyPart(String name, ContentBody body) 
    

    例如使用:InputStreamBody :
    new FormBodyPart("name", new InputStreamBody(new RandomInputStream(...)), ContentType.MULTIPART_FORM_DATA);
    

    您可以使用:RandomInputStream类(class)。

    对于标题,您可能会使用:HTTPBuilder$RequestConfigDelegate.setHeaders(Map headers)因为它被设置为内部闭包的委托(delegate)。

    关于grails - Groovy 脚本向 Grails Web App 发送多部分请求以进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25430966/

    相关文章:

    grails - 读取在屏幕上输入的参数

    grails - Redis(grails插件)不持久化枚举对象

    maven - IntelliJ + Groovy + Spock : no Groovy library is defined for module

    grails - Grails中的验证错误

    eclipse - Grails-IDE Eclipse插件的开发状态?

    java - Maven 不运行由 groovy 编译器生成的 TestNG 测试

    junit - 正在查看junit test的结果?

    gradle - 使用 Sonarrunner 和 Gradle 从 Jacoco 报告中排除包

    java - 将 SNMP4J-Agent 与 Logback 配置结合使用

    grails - Grails find通过多个我喜欢的列