1 package com.foxinmy.weixin4j.base.test.http;
2
3 import java.net.InetSocketAddress;
4 import java.net.Proxy;
5 import java.net.Proxy.Type;
6
7 import org.junit.Assert;
8 import org.junit.Test;
9
10 import com.foxinmy.weixin4j.http.HttpClient;
11 import com.foxinmy.weixin4j.http.HttpClientException;
12 import com.foxinmy.weixin4j.http.HttpParams;
13 import com.foxinmy.weixin4j.http.HttpResponse;
14 import com.foxinmy.weixin4j.http.URLParameter;
15 import com.foxinmy.weixin4j.http.factory.HttpClientFactory;
16 import com.foxinmy.weixin4j.http.support.netty.Netty4HttpClientFactory;
17
18
19
20
21
22
23
24
25
26 public abstract class HttpClientTest {
27
28 private static final String GET_URL = "http://www.iteye.com/";
29 private static final String POST_URL = "http://223.72.192.176:8080/YuJia/LoginServlet";
30 private static final String HTTPS_URL = "https://www.baidu.com/";
31 protected static final Proxy PROXY = new Proxy(Type.HTTP,
32 new InetSocketAddress("219.141.225.108", 80));
33
34 protected abstract HttpClientFactory createHttpFactory();
35
36 protected HttpClient createHttpClient() {
37 return createHttpFactory().newInstance(null);
38 }
39
40 protected HttpClient createProxyHttpClient() {
41 HttpParams params = new HttpParams();
42 params.setProxy(PROXY);
43 return createHttpClient(params);
44 }
45
46 protected HttpClient createSSLHttpClient() {
47 HttpParams params = new HttpParams();
48 params.setHostnameVerifier(HttpClientFactory.AllowHostnameVerifier.GLOBAL);
49 params.setSSLContext(HttpClientFactory.allowSSLContext());
50 return createHttpClient(params);
51 }
52
53 protected HttpClient createProxyAndSSLHttpClient() {
54 HttpParams params = new HttpParams();
55 params.setHostnameVerifier(HttpClientFactory.AllowHostnameVerifier.GLOBAL);
56 params.setSSLContext(HttpClientFactory.allowSSLContext());
57 params.setProxy(PROXY);
58 return createHttpClient(params);
59 }
60
61 protected HttpClient createHttpClient(HttpParams params) {
62 HttpClientFactory httpClientFactory = createHttpFactory();
63 HttpClientFactory.setDefaultParams(params);
64 return httpClientFactory.newInstance(null);
65 }
66
67 @Test
68 public void getDefaultHttpClientFactoryTest() {
69 HttpClientFactory httpClientFactory = HttpClientFactory
70 .getDefaultFactory();
71 Assert.assertTrue(httpClientFactory.getClass().isAssignableFrom(
72 Netty4HttpClientFactory.class));
73 }
74
75 @Test
76 public void getRequestTest() throws HttpClientException {
77 HttpClient httpClient = createHttpClient();
78 HttpResponse response = httpClient.get(GET_URL);
79 Assert.assertEquals(200, response.getStatus().getStatusCode());
80 }
81
82 @Test
83 public void postRequestTest() throws HttpClientException {
84 HttpClient httpClient = createHttpClient();
85 URLParameter parameter = new URLParameter("query", "java");
86 HttpResponse response = httpClient.post(POST_URL, parameter);
87 Assert.assertEquals(200, response.getStatus().getStatusCode());
88 }
89
90 @Test
91 public void httpsRequestTest() throws HttpClientException {
92 HttpClient httpClient = createHttpClient();
93 HttpResponse response = httpClient.get(HTTPS_URL);
94 Assert.assertEquals(200, response.getStatus().getStatusCode());
95 }
96
97 @Test
98 public void proxyRequestTest() throws HttpClientException {
99 HttpClient httpClient = createProxyHttpClient();
100 HttpResponse response = httpClient.get(GET_URL);
101 Assert.assertEquals(200, response.getStatus().getStatusCode());
102 response = httpClient.get(HTTPS_URL);
103 Assert.assertEquals(200, response.getStatus().getStatusCode());
104 }
105
106 @Test
107 public void sslRequestTest() throws HttpClientException {
108 HttpClient httpClient = createSSLHttpClient();
109 HttpResponse response = httpClient.get(GET_URL);
110 Assert.assertEquals(200, response.getStatus().getStatusCode());
111 response = httpClient.get(HTTPS_URL);
112 Assert.assertEquals(200, response.getStatus().getStatusCode());
113 }
114
115 @Test
116 public void proxyAndSSLRequestTest() throws HttpClientException {
117 HttpClient httpClient = createProxyAndSSLHttpClient();
118 HttpResponse response = httpClient.get(GET_URL);
119 Assert.assertEquals(200, response.getStatus().getStatusCode());
120 response = httpClient.get(HTTPS_URL);
121 Assert.assertEquals(200, response.getStatus().getStatusCode());
122 }
123 }