我正在尝试进行以下测试:
@Test(priority =1)
public void PostNetwork() throws ClientProtocolException, IOException {
String postBody = new JSONObject(){{
put("vlan", TestHelper.randomVlan());
put("computeNodeGroups", new int[]{});
put("name", TestHelper.randomString(5));
put("virtualNetworkGroups",new int[]{});
put("colocationPorts",new int[]{});
}}.toString();
System.out.println("POST BODY: " + postBody);
Object response = super.post(postnetworks, postBody);
//Assert.assertNotNull(response.createdDate);
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>"+postnetworks);
System.out.println(
"-----------------------------------------");
}
这是主要测试:
public PostResponseType post(String url, String postBody, boolean isNegative, int expectedStatusCode) throws ClientProtocolException, IOException {
HashMap<String, String> headerMap = new HashMap<String, String>();
headerMap.put("Content-Type", "application/json");
headerMap.put("Authorization", "Bearer " + TestBase.getAccessToken(isNegative));
System.out.printf("Sending request to %s\n", url);
// Call the API
CloseableHttpResponse closeableHttpResponse = restClient.post(url, postBody, headerMap);
// Assert that the request was successful
int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
//Assert.assertEquals(statusCode, expectedStatusCode);
// If the request fails, don't check the object structure
if (expectedStatusCode / 100 != 2) {
return null;
}
Header[] headersArray = closeableHttpResponse.getAllHeaders();
// Print headers and response for debugging
HashMap<String, String> allHeaders = new HashMap<String, String>();
for (Header header : headersArray) {
allHeaders.put(header.getName(), header.getValue());
}
System.out.println("Header Array-->" + allHeaders);
System.out.println("statusCode: " + statusCode);
String responseString = EntityUtils.toString(closeableHttpResponse.getEntity(), "UTF-8");
System.out.println("The response from post API: " + responseString);
try {
PostResponseType returnvalue = MAPPER.readValue(responseString, this.postResponseClass);
return returnvalue;
} catch (Exception e) {
return null;
}
}
在控制台上获取它:
POST BODY: {“ vlan”:2400,“ computeNodeGroups”:[],“ name”:“ onkhw”,“ virtualNetworkGroups”:[],“ colocationPorts”:[]} statusCode:400 来自API的响应:错误 在BadRequestError.HttpError [作为构造函数](/node_modules/routing-controllers/http-error/HttpError.js:27:23) 在新的BadRequestError(/node_modules/routing-controllers/http-error/BadRequestError.js:20:28) 在VirtualNetworkController。 (/home/site/wwwroot/app/controllers/virtual-network-controller.js:103:23) 在Generator.next() 在实现(/home/site/wwwroot/app/controllers/virtual-network-controller.js:16:58) 在 在process._tickCallback(internal / process / next_tick.js:188:7)
但是帖子实际上是在UI上发生的,所以既然帖子确实在帖子中,我如何获得200状态。