org.apache.http.impl.client.DefaultHttpClient#clearRequestInterceptors()源码实例Demo

下面列出了org.apache.http.impl.client.DefaultHttpClient#clearRequestInterceptors() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@Test(groups = "wso2.esb", description = "Test CorrelateOn in Aggregate mediator ")
public void testAggregateWithCorrelateExpression() throws IOException {
    String expectedOutput1 = "<result><value>value1</value><value>value2</value></result>";
    String expectedOutput2 = "<result><value>value2</value><value>value1</value></result>";

    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(getApiInvocationURL("testAggregate"));

    try {
        HttpResponse httpResponse = httpclient.execute(httpget);
        HttpEntity entity = httpResponse.getEntity();
        BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent()));
        String result = "";
        String line;
        while ((line = rd.readLine()) != null) {
            result += line;
        }
        Assert.assertTrue(expectedOutput1.equals(result) || expectedOutput2.equals(result),
                "Aggregated response is not correct.");
    } finally {
        httpclient.clearRequestInterceptors();
    }
}
 
@Test(groups = "wso2.esb", description = "Test CorrelateOn in Aggregate mediator ")
public void testAggregateWithCorrelateExpression() throws IOException{
    String expectedOutput1 = "<result><value>value1</value><value>value2</value></result>";
    String expectedOutput2 = "<result><value>value2</value><value>value1</value></result>";

    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(getApiInvocationURL("testAggregate"));

    try {
        HttpResponse httpResponse = httpclient.execute(httpget);
        HttpEntity entity = httpResponse.getEntity();
        BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent()));
        String result = "";
        String line;
        while ((line = rd.readLine()) != null) {
            result += line;
        }
        Assert.assertTrue(expectedOutput1.equals(result) || expectedOutput2.equals(result), "Aggregated response is not correct.");
    }
    finally {
        httpclient.clearRequestInterceptors();
    }
}
 
@Test(groups = "wso2.esb", description = "Preserve Content-Type header test", enabled = true)
public void testPreserveContentTypeHeader() throws Exception {
    wireServer.start();

    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(getApiInvocationURL("ContentTypePreserveAPI"));

    StringEntity postingString = new StringEntity("{\"sampleJson\" : \"sampleValue\"}");
    httppost.setEntity(postingString);
    httppost.setHeader("Content-type", "application/json");

    try {
        httpclient.execute(httppost);
    } finally {
        httpclient.clearRequestInterceptors();
    }

    String wireResponse = wireServer.getCapturedMessage();
    String[] wireResponseLines = wireResponse.split(System.lineSeparator());
    boolean isContentTypePresent = false;
    for (String line : wireResponseLines) {
        if (line.contains("Content-Type")) {
            isContentTypePresent = true;
            //charset encoding is appended to content-type header even preserve the content-type header as it is
            //This checks charset encoding is appended or not
            Assert.assertFalse(line.contains(";"), "Content-Type header was modified - " + line);
        }
    }
    //coming to this line means content type header is in expected state, hence passing the test
    Assert.assertTrue(isContentTypePresent, "Content-Type header is not present in the ESB request");
}
 
@Test(groups = "wso2.esb", description = "Preserve Content-Type header test", enabled = true)
public void testPreserveContentTypeHeader() throws Exception {
    wireServer.start();

    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(getApiInvocationURL("ContentTypePreserveAPI"));

    StringEntity postingString = new StringEntity("{\"sampleJson\" : \"sampleValue\"}");
    httppost.setEntity(postingString);
    httppost.setHeader("Content-type", "application/json");

    try {
        httpclient.execute(httppost);
    } finally {
        httpclient.clearRequestInterceptors();
    }


    String wireResponse = wireServer.getCapturedMessage();
    String[] wireResponseLines = wireResponse.split(System.lineSeparator());
    boolean isContentTypePresent = false;
    for (String line : wireResponseLines) {
        if (line.contains("Content-Type")) {
            isContentTypePresent = true;
            //charset encoding is appended to content-type header even preserve the content-type header as it is
            //This checks charset encoding is appended or not
            Assert.assertFalse(line.contains(";"), "Content-Type header was modified - " + line);
        }
    }
    //coming to this line means content type header is in expected state, hence passing the test
    Assert.assertTrue(isContentTypePresent, "Content-Type header is not present in the ESB request");
}