在rest assured中使用POST方法时获得302
我已经写了我的测试用例如下,并得到以下错误 日志中的错误: 失败:setpassword java.lang.AssertionError:1预期失败。 应为状态代码,但为 代码:在rest assured中使用POST方法时获得302,post,methods,rest-assured,Post,Methods,Rest Assured,我已经写了我的测试用例如下,并得到以下错误 日志中的错误: 失败:setpassword java.lang.AssertionError:1预期失败。 应为状态代码,但为 代码: @Test(enabled=true) public void setpassword(){ RequestSpecification requestSpecification= new Config().getRequestSpecification(); RequestSpecification
@Test(enabled=true)
public void setpassword(){
RequestSpecification requestSpecification= new Config().getRequestSpecification();
RequestSpecification requestHeaders= new Config().getHeaders();
Map<String,String> password = new HashMap<String, String>();
password.put("password", "Password@2017");
given().spec(requestSpecification).spec(requestHeaders).body(password).when().post("https://example.com/testplatform@example.com/password").
then().statusCode(200).log().all();
}
@Test(enabled=true)
public void setpassword(){
RequestSpecification RequestSpecification=new Config().getRequestSpecification();
RequestSpecification requestHeaders=new Config().getHeaders();
Map password=newhashmap();
password.put(“password”Password@2017");
给定().spec(requestSpecification).spec(requestHeaders).body(密码).when().post(“https://example.com/testplatform@example.com/password”)。
然后().statusCode(200.log().all();
}
我解决了这个问题。我收到一个302错误,因为api url正在被编码。我使用urlEncodingEnabled(false)解决了这个问题。这对我有帮助:
given().log().all().contentType("application/x-www-form-urlencoded; charset=UTF-8")
.redirects().follow(false).redirects().max(100)
.queryParam()...
另外,在我的例子中,我使用带有JWT令牌的url来测试lin/网站。它引起了我的注意,我没有使用url+JWT作为完整的链接。我忽略了JWT,只使用了链接,因此它没有重定向网站。这里有什么问题?您的测试失败,因为您希望收到200 HTTP响应,但收到302重定向。这似乎是它失败的一个合理原因。同样的api在Postman中给出了200。这里的问题是,当api中有电子邮件时,它会给出302错误。我想知道是否有办法处理这种情况。我发现电子邮件中的“@”被编码为%40,因此api返回302错误。有什么方法可以防止这种情况发生吗?提前谢谢你的帮助!!