【问题标题】:Rest Assured: Getting org.apache.http.ConnectionClosedException: Premature end of Content-Length delimited message body放心:获取 org.apache.http.ConnectionClosedException:内容长度分隔的消息正文过早结束
【发布时间】:2020-06-09 20:15:34
【问题描述】:

我试图在我的代码中使用原始正文调用“put”api 调用,但出现以下错误

org.apache.http.ConnectionClosedException: Premature end of Content-Length delimited message body (expected: 69; received: 68
    at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:178)
    at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:135)
    at org.apache.http.conn.EofSensorInputStream$read.call(Unknown Source)
    at io.restassured.internal.RestAssuredResponseOptionsGroovyImpl.convertStreamToByteArray(RestAssuredResponseOptionsGroovyImpl.groovy:470)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:101)
    at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite$StaticMetaMethodSiteNoUnwrapNoCoerce.invoke(StaticMetaMethodSite.java:149)
    at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.callStatic(StaticMetaMethodSite.java:100)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:208)
    at io.restassured.internal.RestAssuredResponseOptionsGroovyImpl.charsetToString(RestAssuredResponseOptionsGroovyImpl.groovy:522)
    at io.restassured.internal.RestAssuredResponseOptionsGroovyImpl$charsetToString$4.callCurrent(Unknown Source)
    at io.restassured.internal.RestAssuredResponseOptionsGroovyImpl.asString(RestAssuredResponseOptionsGroovyImpl.groovy:183)
    at io.restassured.internal.RestAssuredResponseOptionsGroovyImpl.asString(RestAssuredResponseOptionsGroovyImpl.groovy:179)
    at io.restassured.internal.RestAssuredResponseOptionsImpl.asString(RestAssuredResponseOptionsImpl.java:234)
    at com.NexusPortalAutomation.Utilities.Java.restAssured.cancelSpa(restAssured.java:70)
    at com.NexusPortalAutomation.Utilities.Java.restAssured.test(restAssured.java:57)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

代码:

        char q ='"';
    RestAssured.baseURI = "http://localhost:3000/api/v1/spa/cancel/" + spaIndex;
    String rawbody ="{"+q+"SpaCancel"+q+":[{"+q+"CustomerId"+q+":"+q+Customer+q+","+q+"SpaIndex"+q+spaIndex+q+"CancelDate"+q+":"+q+"2020-06-08"+q+","+q+"CancelReason"+q+":"+q+"Test"+q+","+q+"ReasonCode"+q+":"+q+" "+q+"}]}";
    RequestSpecification httpRequest = RestAssured.given().headers("Authorization", "Bearer " + getToken(),
        "Content-Type", ContentType.JSON, "Accept", ContentType.JSON) ;

    Response response = httpRequest.body(rawbody).put();

    String body = response.getBody().asString();
    System.out.println(body);

【问题讨论】:

  • 这看起来很糟糕,你为什么还要使用" 当你可以使用它作为字符串时
  • 对不起,我没明白你的意思?能举个例子吗
  • 这不是您的客户端代码的问题。您呼叫的服务器有问题。它返回标头 Content-Length: 69,响应正文长度为 68 个字符。看看这个类似的问题 - stackoverflow.com/questions/23152816/…。你应该检查你的应用程序在localhost:3000 上运行的正确性

标签: java rest-assured rest-assured-jsonpath


【解决方案1】:

我针对测试 PUT 端点测试了您的代码,它在我的机器上运行良好。我敢打赌,您看到的错误消息表明您的http://localhost:3000/api/v1/spa/cancel/ 的回复有问题。如错误所示,内容长度与实际返回的内容不匹配。

【讨论】:

  • 我在 String body = response.getBody().asString(); 处遇到错误休息正常
  • 是的。意味着返回的响应无效。您正在调用http://localhost:3000/api/v1/spa/cancel/,它返回无效响应,因此getBody() 失败。
【解决方案2】:

我找到了问题,它是不正确的正文,因为在“SpaIndex”之后缺少“:”

String rawbody ="{"+q+"SpaCancel"+q+":[{"+q+"CustomerId"+q+":"+q+Customer+q+","+q+"SpaIndex"+q+spaIndex+ q+"CancelDate"+q+":"+q+"2020-06-08"+q+","+q+"CancelReason"+q+":"+q+"测试"+q+","+q+"原因代码"+q+ ":"+q+" "+q+"}]}";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-19
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多