首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring Cloud合约测试主体不匹配

Spring Cloud合约测试主体不匹配
EN

Stack Overflow用户
提问于 2020-05-22 07:03:50
回答 1查看 385关注 0票数 0

我正在尝试使用Spring Cloud Contract testing来测试我的分块文件上传API。

但是在消费者端,我的测试失败了,因为请求正文不匹配。

但是查看给定的错误消息,我很难理解其中的原因:

代码语言:javascript
复制
+[                                                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--
+[                                                            |
+[ -----------------------------------------------------------------------------------------------------------------------

如您所见,从这一行开始:

代码语言:javascript
复制
.*--(.*)   | --m82DlcHjsDnwxSBXh7XEfqQEMEsdX7i  <<<<< Body does not match

它显示了一个不匹配的请求,这对我来说很难理解为什么!

有什么想法吗?

附言:

这是我的生产者pom依赖部分:

代码语言:javascript
复制
<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>

这是消费者的部分:

代码语言:javascript
复制
<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 dependencyManagement:

代码语言:javascript
复制
<dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
EN

回答 1

Stack Overflow用户

发布于 2020-06-26 12:34:33

如果不看groovy/yaml契约,就很难知道,但可能是您的正则表达式有点问题(比如额外的空格或其他什么?它在regex101.com上看起来不错),或者您可能正在匹配硬编码字符串".--(.)“而不是一个正则表达式。

如果您使用的是groovy,请确保您有

代码语言:javascript
复制
value(regex(".*--(.*)"))

而不是仅仅

代码语言:javascript
复制
".*--(.*)"

不确定yaml中的等价物是什么。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61945196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档