我有一项服务,我可以使用以下 jQuery 代码访问它(来自带有 --disable-web-security 的 google chrome)
$.ajax({
type: 'POST',
url: "http://10.30.1.2:9234/myapp/v6/token/generate",
headers: {
"Content-Type":"application/json",
"Accept":"application/json"
},
data: JSON.stringify({
"staffId" : "13254",
"password" : "JustADummyPassword"
})
}).done(function(data) {
console.log(data);
});
$.ajax({
type: 'GET',
url: "http://10.30.1.2:9234/myapp/v6/user/appl/Firstname/Lastname/email@address.com/1998-01-01",
headers: {
"Content-Type":"application/json",
"Accept":"application/json"
}
}).done(function(data) {
console.log(data);
});
第一次调用设置了一个 cookie,这是第二次调用进行身份验证所必需的。这工作正常,两个请求都返回预期的结果。
我正在尝试为服务设置自动化测试,并使用 RestAssured 用 JAVA 编写。
public class UserApplication {
public static Map<String, String> authCookies = null;
public static String JSESSIONID = null;
public static void main(String[] args) {
Response resp = hello();
resp = apiUserApplication();
}
public static Response apiUserApplication() {
String userAppl = "http://10.30.1.2:9234/myapp/v6/user/appl/Firstname/Lastname/email@address.com/1998-01-01";
Response response = RestAssured.given()
.cookie("JSESSIONID", JSESSIONID).and()
.header("Accept", "application/json").and()
.header("Content-Type", "application/json").and()
.when().get(userAppl);
return response;
}
public static Response hello() {
String helloUrl = "http://10.30.1.2:9234/myapp/v6/hello";
Response response = RestAssured.given().cookies(authCookies)
.contentType("application/json").when().get(helloUrl);
return response;
}
}
第一次调用 (hello) 工作正常,并返回 200 代码,并获得一个有效令牌以用于第二次调用。我从带有 400 状态代码的第二次呼叫中得到的错误是......
{"errors":["Content type 'null' not supported"]}