spring-boot - Spring Cloud Contract 测试主体不匹配
问题描述
我正在尝试使用 Spring Cloud Contract 测试来测试我的多部分文件上传 API。
但是在消费者方面,由于请求正文不匹配,我的测试失败了。
但是查看给定的错误消息,我很难理解为什么:
+[ Request was not matched
+[ =======================
+[
+[ -----------------------------------------------------------------------------------------------------------------------
+[ | Closest stub | Request |
+[ -----------------------------------------------------------------------------------------------------------------------
+[ |
+[ POST | POST
+[ /my-api/(\w|\W)+/objects | /my-api/storage_id/objects
+[ |
+[ Content-Type [matches] : multipart/form-data.* | Content-Type:
+[ | multipart/form-data;charset=UTF-8;boundary=m82DlcHjsDnwxS
+[ | BXh7XEfqQEMEsdX7i
+[ Accept [matches] : application/json.* | Accept: application/json
+[ |
+[ .*--(.*) | --m82DlcHjsDnwxSBXh7XEfqQEMEsdX7i <<<<< Body does not match
+[ Content-Disposition: form-data; name="myId" | Content-Disposition:
+[ (Content-Type: .* | form-data; name="myId"
+[ )?(Content-Transfer-Encoding: .* | Content-Type:
+[ )?(Content-Length: \d+ | text/plain;charset=UTF-8
+[ )? | Content-Length:
+[ ^\s*\S[\S\s]* | 6
+[ --\1.* | someId
+[ | --m82DlcHjsDnwxSBXh7XEfqQEMEsdX7i
+[ | Content-D
+[ | isposition: form-data; name="myPath"
+[ | Content-Type:
+[ | text/plain;charset=UTF-8
+[ | Content-Length:
+[ | 8
+[ | somePath
+[ | --m82DlcHjsDnwxSBXh7XEfqQEMEsdX7i
+[ | Content
+[ | -Disposition: form-data;
+[ | name="ct"
+[ | Content-Type:
+[ | text/plain;charset=UTF-8
+[ | Content-Length:
+[ | 10
+[ | text/plain
+[ | --m82DlcHjsDnwxSBXh7XEfqQEMEsdX7i
+[ | Cont
+[ | ent-Disposition: form-data; name="cs";
+[ | filename="test"
+[ | Content-Type:
+[ | application/octet-stream
+[ | test
+[ | --m82DlcHjsDnwxSBXh7XEf
+[ | qQEMEsdX7i--
+[ .*--(.*) | --m82DlcHjsDnwxSBXh7XEfqQEMEsdX7i <<<<< Body does not match
+[ Content-Disposition: form-data; name="myPath" | Content-Disposition:
+[ (Content-Type: .* | form-data; name="myId"
+[ )?(Content-Transfer-Encoding: .* | Content-Type:
+[ )?(Content-Length: \d+ | text/plain;charset=UTF-8
+[ )? | Content-Length:
+[ ^\s*\S[\S\s]* | 6
+[ --\1.* | someId
+[ | --m82DlcHjsDnwxSBXh7XEfqQEMEsdX7i
+[ | Content-D
+[ | isposition: form-data; name="myPath"
+[ | Content-Type:
+[ | text/plain;charset=UTF-8
+[ | Content-Length:
+[ | 8
+[ | somePath
+[ | --m82DlcHjsDnwxSBXh7XEfqQEMEsdX7i
+[ | Content
+[ | -Disposition: form-data;
+[ | name="ct"
+[ | Content-Type:
+[ | text/plain;charset=UTF-8
+[ | Content-Length:
+[ | 10
+[ | text/plain
+[ | --m82DlcHjsDnwxSBXh7XEfqQEMEsdX7i
+[ | Cont
+[ | ent-Disposition: form-data; name="cs";
+[ | filename="test"
+[ | Content-Type:
+[ | application/octet-stream
+[ | test
+[ | --m82DlcHjsDnwxSBXh7XEf
+[ | qQEMEsdX7i--
+[ .*--(.*) | --m82DlcHjsDnwxSBXh7XEfqQEMEsdX7i <<<<< Body does not match
+[ Content-Disposition: form-data; name="ct" | Content-Disposition:
+[ (Content-Type: .* | form-data; name="myId"
+[ )?(Content-Transfer-Encoding: .* | Content-Type:
+[ )?(Content-Length: \d+ | text/plain;charset=UTF-8
+[ )? | Content-Length:
+[ ^\s*\S[\S\s]* | 6
+[ --\1.* | someId
+[ | --m82DlcHjsDnwxSBXh7XEfqQEMEsdX7i
+[ | Content-D
+[ | isposition: form-data; name="myPath"
+[ | Content-Type:
+[ | text/plain;charset=UTF-8
+[ | Content-Length:
+[ | 8
+[ | somePath
+[ | --m82DlcHjsDnwxSBXh7XEfqQEMEsdX7i
+[ | Content
+[ | -Disposition: form-data;
+[ | name="ct"
+[ | Content-Type:
+[ | text/plain;charset=UTF-8
+[ | Content-Length:
+[ | 10
+[ | text/plain
+[ | --m82DlcHjsDnwxSBXh7XEfqQEMEsdX7i
+[ | Cont
+[ | ent-Disposition: form-data; name="cs";
+[ | filename="test"
+[ | Content-Type:
+[ | application/octet-stream
+[ | test
+[ | --m82DlcHjsDnwxSBXh7XEf
+[ | qQEMEsdX7i--
+[ |
+[ -----------------------------------------------------------------------------------------------------------------------
如您所见,从这一行开始:
.*--(.*) | --m82DlcHjsDnwxSBXh7XEfqQEMEsdX7i <<<<< Body does not match
它显示了一个不匹配的请求,这对我来说很难理解!
有什么想法吗?
PS:
这是我的生产者 pom 依赖部分:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-contract-verifier</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-maven-plugin</artifactId>
<version>2.2.2.RELEASE</version>
<extensions>true</extensions>
<configuration>
<baseClassForTests>
com.backbase.cxs.cs.c3.BaseTest
</baseClassForTests>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
这是消费者部分:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-contract-stub-runner</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-contract-verifier</artifactId>
<scope>test</scope>
</dependency>
我正在使用 spring-cloud 依赖管理:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
解决方案
如果没有看到 groovy/yaml 合同就很难知道,但可能是您的正则表达式有些偏离(比如额外的空格或其他什么?在 regex101.com 上看起来不错),或者您正在匹配硬编码字符串“。--(. )" 而不是正则表达式。
如果您使用的是 groovy,请确保您有
value(regex(".*--(.*)"))
而不仅仅是
".*--(.*)"
不确定yaml中的等价物是什么。
推荐阅读
- docker - WSL 中 jekyll 映像的 Docker 权限问题
- node.js - package.json 中的 webpack 脚本
- docker - 如何在没有控制台访问的情况下监控 docker UCP 日志?
- spring-boot - Spring boot .AnnotationConfigApplicationContext has not been refreshed yet error
- react-native - 如何在 react-native 中实现联系人跟踪?
- r - 查找在 R 中具有至少三年数据的所有航班
- php - PHP 队列停止使用 Twilio 无效的电话号码
- c++ - 0-1 大重量背包
- java - 如何通过 Spring-Expression-Language 或其他方式根据 jobParameters 有条件地注入 bean?
- android - 使用 SQLLite、imageViews 和可回收的 RecyclerView 性能 = false