【问题标题】:RESTAssured disable URL encoding not working correctlyRESTAssured 禁用 URL 编码无法正常工作
【发布时间】:2019-03-06 09:06:43
【问题描述】:

我正在测试一个带有如下 URL 的 HTTP Post 请求:

https://myurl.com/api/logs/%2Fvar%2Flog%2Fmessages?Api-Token=12332429nmdsafs

我禁用了 URL 编码,这里是我的发帖请求:

RestAssured.given()
.contentType(JSON)
.log()
.all()
.urlEncodingEnabled(false)
.baseUri(RestAssured.baseURI)
.basePath(url)
.pathParam(LOG_PATH_PARAM_NAME, urlEncodeString(requireNonNull(logPath)))
.body(myJsonBody)
.when()
.post("/logs/{logPath}")
.then()
.statusCode(OK.getStatusCode());

我也试过这样:

RestAssured.given()
.contentType(JSON)
.log()
.all()
.urlEncodingEnabled(false)
.baseUri(RestAssured.baseURI)
.basePath(url)
.body(myJsonBody)
.when()
.post("/logs/" + urlEncodeString(requireNonNull(logPath)))
.then()
.statusCode(OK.getStatusCode());

这里是 urlEncodeString 方法:

private static String urlEncodeString(String value) throws UnsupportedEncodingException {
        return URLEncoder.encode(value, StandardCharsets.UTF_8.name()).replaceAll("\\+", "%20");
    }

现在的问题是我上面提到的 URL 被编码为以下内容:

https://myurl.com/api/logs/var/log/messages?Api-Token=12332429nmdsafs

有人知道这里有什么问题吗?或者知道解决方法? 我已经尝试过双重逃脱路径。

编辑:

我刚刚发现禁用 URL 编码仅适用于 URL 参数。

【问题讨论】:

    标签: java url url-encoding rest-assured


    【解决方案1】:

    虽然given().urlEncodingEnabled(isEnabled).spec()... 只会禁用对 URL 参数的编码是正确的,但您也可以使用对 URL 本身执行相同操作

    RequestSpecification mySpec = new RequestSpecBuilder().setUrlEncodingEnabled(false)

    例如,如果我想在不编码的情况下向这个(确切的)URL 发出 get 请求http://api.cheapbooks.com/mathbooks/location/$amazonbooks%2Fscience%2Fmath

    RestAssured 的默认行为会将 URL 双重编码为​​:http://api.cheapbooks.com/mathbooks/location/%24amazonbook%252Fscience%2Fmath

    但是,如果您创建像上面的 mySpec 这样的 RequestSpecification,其中 setUrlEncodingEnabled(false) 并且您通过以下任一方式发出 http 请求:

    given().spec(mySpec)...

    spec.setBaseUri(...)

    你应该这样得到想要的结果

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多