xml - RestAssured 为 POST 创建两个后续请求并返回 401 错误
问题描述
我正在尝试使用有效负载(xml)发送 POST 请求。这是我的代码示例:
val xml = """
| <?xml version="1.0" encoding="UTF-8"?>
........
........
""".stripMargin
RestAssured.baseURI = "https://xxxxxxxxx"
val request: RequestSpecification = given().port(1234).auth().basic("test", "test")
request
.header("Accept", ContentType.XML)
.contentType("application/xml")
.body(xml)
val response = request.post("/transformToXML2")
val statusCode = response.getStatusCode
Assert.assertEquals(statusCode, 200) //PASSED
问题:如果我通过 SOAP UI (Rest) 发送相同的 XML,我会得到预期的 XML 响应。通过这段代码,我在日志中看到以下内容:
DEBUG o.a.h.i.c.DefaultClientConnection - Sending request: POST /transformToXML2 HTTP/1.1
DEBUG o.a.h.wire - >> "POST /transformToXML2 HTTP/1.1[\r][\n]
DEBUG o.a.h.wire - >> "Accept: application/xml[\r][\n]
....
DEBUG o.a.h.wire - >> "Content-Length: 17333[\r][\n]
.......
DEBUG o.a.h.wire - >> "Accept-Encoding:gzip,deflate[\r][\n]
DEBUG o.a.h.wire - >> [\r][\n]
DEBUG o.a.h.headers - >> "POST /transformToXML2 HTTP/1.1[\r][\n]
DEBUG o.a.h.headers - >> "Accept: application/xml[\r][\n]
....
DEBUG o.a.h.headers - >> "Content-Length: 17333[\r][\n]
.......
DEBUG o.a.h.headers - >> "Accept-Encoding:gzip,deflate[\r][\n]
DEBUG o.a.h.headers - >> [\r][\n]
日志下面是 XML:
DEBUG o.a.h.wire - >> [\r][\n]
DEBUG o.a.h.wire - >> "<?xml version="1.0" encoding="UTF-8"?>"[\r][\n]
.....
DEBUG o.a.h.wire - >> " "
Q1:为什么我有以下行(考虑到上述代码返回 200)
DEBUG o.a.h.wire - << "HTTP/1.1 401 Unauthorized"[\r][\n]
DEBUG o.a.h.wire - << "Connection: keep-alive"[\r][\n]
DEBUG o.a.h.wire - << "WWW-Authenticate: Basic realm="Realm name"[\r][\n]"
DEBUG o.a.h.wire - << "Content-Length: 0"[\r][\n]
DEBUG o.a.h.i.c.DefaultClientConnection - Receiving request: HTTP/1.1 401 Authorized
Q2:由于行在日志中重复了一点。为什么有效载荷会相应地发送两次以记录:
DEBUG o.a.h.i.c.DefaultHttpClient- Attemp 2 to execute request //Why 2 request?
调试 oahicDefaultClientConnection - 发送请求:POST /transformToXML2 HTTP/1.1
DEBUG o.a.h.wire - >> "POST /transformToXML2 HTTP/1.1[\r][\n]
DEBUG o.a.h.wire - >> "Accept: application/xml[\r][\n]
DEBUG o.a.h.wire - >> "Content-Type: application/xml; charset=ISO-885901[\r][\n]
DEBUG o.a.h.wire - >> "Content-Length: 17333[\r][\n]
DEBUG o.a.h.wire - >> "Host: host+port [\r][\n]
DEBUG o.a.h.wire - >> "Connection: Keep-Alive[\r][\n]
DEBUG o.a.h.wire - >> "User-Agent: Apache-HttpClient/4.5.3..[\r][\n]
DEBUG o.a.h.wire - >> "Autorithation: Basic qwertyqweqwe[\r][\n]
DEBUG o.a.h.wire - >> "Accept-Encoding: gzip,deflate[\r][\n]
DEBUG o.a.h.i.c.DefaultClientConnection - Receiving request: HTTP/1.1 200 OK
DEBUG o.a.h.headers << HTTP/1.1 200 Ok
DEBUG o.a.h.headers << Content-Length: 107
DEBUG o.a.h.headers << Content-Type: application/xml
解决方案
Q1 通过添加 preemptive() 来解决。在 basic() 之前,所以我没有 401 错误
推荐阅读
- xml - Axiom 解析器实体问题
- c++ - SViewTree 的孩子在第一级后不工作
- android - 为什么即使启用了 minifier,Proguard 也会使 APK 文件变大?
- extjs - Sencha Touch:嵌套列表滚动到顶部,我该如何禁用它?
- angular - 动态设置对象名称打字稿
- c# - ajax 在 asp.net webforms 中返回 json 对象的身份验证失败
- linux - 为什么我不能在 ubuntu 上运行没有 -a 参数的 lsb_release?
- swift - 有没有办法快速缩短偏好列表
- spring - JPA如何在一对多的情况下加入?
- javascript - 如何以这种方式比较 3 个值?