提问者:小点点

Spring云合约测试主体不匹配


我试图测试我的多部分文件上传API与Spring云合约测试。

但是在消费者方面,由于请求主体不匹配,我的测试失败了。

但是看着给定的错误消息,我很难理解为什么:

+[                                                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

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

有什么想法吗?

P. S:

这是我的生产者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>

共1个答案

匿名用户

如果没有看到groovy/yaml合约,很难知道,但可能是你的正则表达式有点不对劲(比如一个额外的空间或什么?它在regex101.com上看起来不错),或者你在一个硬编码的字符串上匹配 ".--(.)" 而不是正则表达式。

如果您正在使用groovy,请确保您有

value(regex(".*--(.*)"))

而不仅仅是

".*--(.*)"

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