首页 > 解决方案 > 上传多部分纯文本时的空手道问题

问题描述

在空手道 0.9.6 中,我必须添加 * configure charset = null 才能使纯文本上传工作,当我升级到 1.0.1 时,我找不到任何适用于纯文本的方法。

我的pom:

      <dependency>
        <groupId>com.intuit.karate</groupId>
        <artifactId>karate-junit5</artifactId>
        <version>${karate.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.13.1</version>
    </dependency>

我的场景:

Given path uuid, 'upload'
And multipart file file = ({read: filePath + fileName, filename: fileName, contentType: contentType})
And configure charset = null
When method POST
Then status 200

这适用于图像和 pdf 文件,但不适用于 contentType = "plain/text" 的 txt 文件

后端是一个 springboot 应用程序。该服务包含参数 @RequestPart(value = "file") MultipartFile content) ,并且像这样验证文件

import org.springframework.http.MediaType;
import org.springframework.web.multipart.MultipartFile

private boolean hasSupportedContentType(MultiPartFile multipartfile) {
    String contentType = multipartFile.getContentType();
    return contentType.equals(MediaType.TEXT_PLAIN_VALUE)
        || contentType.equals(MediaType.APPLICATION_PDF_VALUE);

}

由于 hasSupportedContentType 为 false,因此会引发以下错误。

有没有解决这个问题的方法?

标签: automationkarate

解决方案


这可能是一个错误。您能否按照此处的说明进行一些研究:https ://github.com/intuit/karate/issues/1645#issuecomment-862502881

请注意,您也可以这样做(注意使用 ofvalue代替read

* def value = karate.readAsString('my-file.txt')
* multipart file file = ({value: value, filename: 'test.txt' })

推荐阅读