1. http: PlainSocketFactory
2. https: SSLSocketFactory( need TrustManager(usally X509TrustManager), SSLContext)
package com.wagerworks.framework.test.util; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import org.apache.http.HttpHost; import org.apache.http.conn.routing.HttpRoute; import org.apache.http.conn.scheme.PlainSocketFactory; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; public class SSLUtil { private static SSLSocketFactory getSSLSocketFactory() { TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted( java.security.cert.X509Certificate[] certs, String authType) { // I'm easy, all accepted } public void checkServerTrusted( java.security.cert.X509Certificate[] certs, String authType) { // I'm easy, all trusted } } }; try { SSLContext sslcontext = SSLContext.getInstance("TLS"); sslcontext.init(null, trustAllCerts, null); SSLSocketFactory ssf = new SSLSocketFactory(sslcontext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); return ssf; } catch (Exception e) { throw new RuntimeException(e); } } public static ThreadSafeClientConnManager getConnManager(String hostName, int sslPort) { SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("https", 443, getSSLSocketFactory())); schemeRegistry.register(new Scheme("http", 9000, PlainSocketFactory .getSocketFactory())); schemeRegistry.register(new Scheme("https", sslPort, getSSLSocketFactory())); // schemeRegistry.register(new Scheme("https", 8789, SSLSocketFactory.getSocketFactory())); ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager( schemeRegistry); connManager.setDefaultMaxPerRoute(20); // for TreadSafeClientConnManager, set a reasonable maxperroute HttpHost localHost = new HttpHost(hostName,sslPort); connManager.setMaxForRoute(new HttpRoute(localHost), 20); return connManager; } }
client: 一个使用HttpClient自动play game的UT
DefaultHttpClient httpClient =new DefaultHttpClient(SSLUtil.getConnManager(host, Integer.valueOf(port)));
package com.igt.rgs.test.verification; import static com.igt.rgs.test.verification.URLParamConstants.CHANNEL; import static com.igt.rgs.test.verification.URLParamConstants.COUNTRY_CODE; import static com.igt.rgs.test.verification.URLParamConstants.CURRENCY_CODE; import static com.igt.rgs.test.verification.URLParamConstants.DENOM_AMOUNT; import static com.igt.rgs.test.verification.URLParamConstants.LANGUAGE; import static com.igt.rgs.test.verification.URLParamConstants.MIN_BET; import static com.igt.rgs.test.verification.URLParamConstants.NS_CODE; import static com.igt.rgs.test.verification.URLParamConstants.PRESENT_TYPE; import static com.igt.rgs.test.verification.URLParamConstants.SECURE_TOKEN; import static com.igt.rgs.test.verification.URLParamConstants.SKIN_CODE; import static com.igt.rgs.test.verification.URLParamConstants.SOFTWARE_ID; import static com.igt.rgs.test.verification.URLParamConstants.UNIQUE_ID; import static com.igt.rgs.test.verification.URLParamConstants.*; import java.io.IOException; import java.io.InputStream; import java.net.URI; import java.util.ArrayList; import java.util.List; import java.util.Properties; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.URIUtils; import org.apache.http.client.utils.URLEncodedUtils; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.log4j.Logger; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import org.w3c.dom.Document; import org.w3c.dom.Node; import com.igt.util.xml.XMLUtil; import com.wagerworks.framework.test.util.SSLUtil; public class OneXGamePlayTest { private static String host = null; private static String port = null; Logger log = Logger.getLogger(OneXGamePlayTest.class); @BeforeClass public static void setup() throws IOException{ InputStream in = ClassLoader.getSystemResourceAsStream("serverConfig.properties"); Properties p = new Properties(); p.load(in); host = p.getProperty("host"); port = p.getProperty("port"); } @Test public void testXT_INT_FLSH_1x(){ final String gameVersion = "1"; final String caseName = "test1"; TestCaseUtil testCase = TestCaseUtil.getInstance(gameVersion, caseName); String skinCode = testCase.getElmValue(SKIN_CODE); String softwareID = testCase.getElmValue(SOFTWARE_ID); String currencyCode = testCase.getElmValue(CURRENCY_CODE); String minBet = testCase.getElmValue(MIN_BET); String presentType = testCase.getElmValue(PRESENT_TYPE); List<NameValuePair> qparams = new ArrayList<NameValuePair>(); qparams.add(new BasicNameValuePair(COUNTRY_CODE, testCase.getElmValue(COUNTRY_CODE))); qparams.add(new BasicNameValuePair(CURRENCY_CODE, currencyCode)); qparams.add(new BasicNameValuePair(LANGUAGE, testCase.getElmValue(LANGUAGE))); qparams.add(new BasicNameValuePair(DENOM_AMOUNT, testCase.getElmValue(DENOM_AMOUNT))); qparams.add(new BasicNameValuePair(MIN_BET, minBet)); qparams.add(new BasicNameValuePair(NS_CODE, testCase.getElmValue(NS_CODE))); qparams.add(new BasicNameValuePair(SECURE_TOKEN, testCase.getElmValue(SECURE_TOKEN))); qparams.add(new BasicNameValuePair(SKIN_CODE, skinCode)); qparams.add(new BasicNameValuePair(SOFTWARE_ID, softwareID)); qparams.add(new BasicNameValuePair(UNIQUE_ID, testCase.getElmValue(UNIQUE_ID))); qparams.add(new BasicNameValuePair(CHANNEL, testCase.getElmValue(CHANNEL))); qparams.add(new BasicNameValuePair(PRESENT_TYPE, presentType)); DefaultHttpClient httpClient =new DefaultHttpClient(SSLUtil.getConnManager(host, Integer.valueOf(port))); HttpResponse httpResponse = null; Document respDoc = null; URI gameURI; URI contentURI; URI initURI; URI playURI; URI closeURI; BasicNameValuePair initAction = new BasicNameValuePair("action","game"); BasicNameValuePair closeAction = new BasicNameValuePair("action","close"); try { gameURI = URIUtils.createURI( "https", host, Integer.valueOf(port), ONE_X_GAME_PATH, URLEncodedUtils.format(qparams, "UTF-8"), null); contentURI = URIUtils.createURI( "https", host, Integer.valueOf(port), ONE_X_CONTENT_PATH, URLEncodedUtils.format(qparams, "UTF-8"), null); qparams.add(initAction); initURI = URIUtils.createURI( "https", host, Integer.valueOf(port), ONE_X_INIT_PATH, URLEncodedUtils.format(qparams, "UTF-8"), null); playURI = URIUtils.createURI( "https", host, Integer.valueOf(port), ONE_X_PLAY_PATH+"/"+skinCode+"/"+softwareID+"/"+currencyCode+minBet+"/"+presentType, URLEncodedUtils.format(qparams, "UTF-8"), null); qparams.remove(initAction); qparams.add(closeAction); closeURI = URIUtils.createURI( "https", host, Integer.valueOf(port), ONE_X_CLOSE_PATH, URLEncodedUtils.format(qparams, "UTF-8"), null); HttpGet httpGet_game = new HttpGet(gameURI); HttpGet httpGet_content = new HttpGet(contentURI); HttpGet httpGet_init = new HttpGet(initURI); HttpPost httpPost_play = new HttpPost(playURI); HttpPost httpPost_close = new HttpPost(closeURI); httpResponse = httpClient.execute(httpGet_game); Assert.assertEquals(200, httpResponse.getStatusLine().getStatusCode()); httpResponse = httpClient.execute(httpGet_content); Assert.assertEquals(200, httpResponse.getStatusLine().getStatusCode()); httpResponse = httpClient.execute(httpGet_init); Assert.assertEquals(200, httpResponse.getStatusLine().getStatusCode()); respDoc = GameResponseUtil.getRespDoc(httpResponse); if(GameResponseUtil.isGIP(respDoc)){ qparams = GameResponseUtil.buildQparamsfromGIP(respDoc); contentURI = URIUtils.createURI( "https", host, Integer.valueOf(port), ONE_X_CONTENT_PATH, URLEncodedUtils.format(qparams, "UTF-8"), null); qparams.add(initAction); initURI = URIUtils.createURI( "https", host, Integer.valueOf(port), ONE_X_INIT_PATH, URLEncodedUtils.format(qparams, "UTF-8"), null); playURI = URIUtils.createURI( "https", host, Integer.valueOf(port), ONE_X_PLAY_PATH+"/"+skinCode+"/"+softwareID+"/"+currencyCode+minBet+"/"+presentType, URLEncodedUtils.format(qparams, "UTF-8"), null); qparams.remove(initAction); qparams.add(closeAction); closeURI = URIUtils.createURI( "https", host, Integer.valueOf(port), ONE_X_CLOSE_PATH, URLEncodedUtils.format(qparams, "UTF-8"), null); httpGet_content.setURI(contentURI); httpResponse = httpClient.execute(httpGet_content); Assert.assertEquals(200, httpResponse.getStatusLine().getStatusCode()); qparams.add(initAction); initURI = URIUtils.createURI( "https", host, Integer.valueOf(port), ONE_X_INIT_PATH, URLEncodedUtils.format(qparams, "UTF-8"), null); httpGet_init.setURI(initURI); httpResponse = httpClient.execute(httpGet_init); Assert.assertEquals(200, httpResponse.getStatusLine().getStatusCode()); } //System.out.println(XMLUtil.toString(respDoc)); Node interfaceStateNode = GameResponseUtil.getInitInterfaceStateNode(respDoc); //play String postData = XMLUtil.toString(interfaceStateNode); System.out.println("post data:\n" + postData); httpPost_play.setEntity(new StringEntity(postData,"UTF-8")); httpResponse = httpClient.execute(httpPost_play); Assert.assertEquals(200, httpResponse.getStatusLine().getStatusCode()); respDoc = GameResponseUtil.getRespDoc(httpResponse); log.debug(XMLUtil.toString(respDoc)); Assert.assertFalse(GameResponseUtil.isResponseFail(respDoc)); //close httpResponse = httpClient.execute(httpPost_close); Assert.assertEquals(200, httpResponse.getStatusLine().getStatusCode()); httpClient.getConnectionManager().shutdown(); } catch (Exception e) { Assert.fail(e.getMessage()); } } }