1 19 20 package com.sslexplorer.httpunit; 21 22 import static org.junit.Assert.assertEquals; 23 import static org.junit.Assert.fail; 24 25 import java.util.Collection ; 26 import java.util.Collections ; 27 import java.util.HashSet ; 28 import java.util.Map ; 29 import java.util.StringTokenizer ; 30 31 import com.maverick.http.GetMethod; 32 import com.maverick.http.HttpClient; 33 import com.maverick.http.HttpMethod; 34 import com.maverick.http.HttpResponse; 35 import com.maverick.http.PostMethod; 36 37 39 final class HttpTestRunner { 40 41 private final HttpClient client; 42 private final HttpTestContainer container; 43 private final HttpTestEntry entry; 44 45 HttpTestRunner(HttpTestContainer container, HttpTestEntry entry) { 46 client = new HttpClient(container.getRootUrl(), container.getPort(), false); 47 this.container = container; 48 this.entry = entry; 49 } 50 51 void setUp() throws Exception { 52 if (entry.isAuthenticated()) { 53 authenticate(client, container, entry); 54 } 55 } 56 57 void tearDown() throws Exception { 58 logOff(client); 59 } 60 61 void runTest() throws Exception { 62 for (HttpTestEntryStep step : entry.getSteps()) { 63 runStep(client, step); 64 } 65 } 66 67 private static void authenticate(HttpClient client, HttpTestContainer container, HttpTestEntry entry) throws Exception { 68 String username = isEmpty(entry.getUsername()) ? container.getDefaultUsername() : entry.getUsername(); 69 String password = isEmpty(entry.getPassword()) ? container.getDefaultPassword() : entry.getPassword(); 70 71 GetMethod usernameLogon = new GetMethod("/usernameLogon.do"); 72 usernameLogon.setParameter("username", username); 73 usernameLogon.setParameter("password", password); 74 HttpResponse usernameLogonResponse = client.execute(usernameLogon); 75 assertEquals("Authenticated Failed", 200, usernameLogonResponse.getStatus()); 76 77 GetMethod logon = new GetMethod("/logon.do"); 78 logon.setParameter("username", username); 79 logon.setParameter("password", password); 80 HttpResponse logonResponse = client.execute(logon); 81 assertEquals("Authenticated Failed", 200, logonResponse.getStatus()); 82 } 83 84 private static void logOff(HttpClient client) throws Exception { 85 GetMethod getMethod = new GetMethod("/logoff.do"); 86 HttpResponse httpResponse = client.execute(getMethod); 87 assertEquals("Log Off Failed", 302, httpResponse.getStatus()); 88 assertRedirect(httpResponse, "showHome.do"); 89 } 90 91 private static boolean isEmpty(String value) { 92 return value == null || value.length() == 0; 93 } 94 95 private static void runStep(HttpClient client, HttpTestEntryStep step) throws Exception { 96 String url = "/" + step.getUrl(); 97 HttpMethod get = step.isPost() ? new PostMethod(url) : new GetMethod(url); 98 for (Map.Entry <String , String > entry : step.getParameters().entrySet()) { 99 get.setParameter(entry.getKey(), entry.getValue()); 100 } 101 102 HttpResponse response = client.execute(get); 103 int responseCode = response.getStatus(); 104 assertEquals("Unexpected Status", step.getExpectedCode(), responseCode); 105 assertRedirect(response, step.getRedirectUrl()); 106 107 Collection <String > messages = getActionMessages(response, "unitTestMessages"); 108 assertEquals("The messages differ", step.getMessages(), messages); 109 Collection <String > errors = getActionMessages(response, "unitTestErrors"); 110 assertEquals("The errors differ", step.getErrors(), errors); 111 } 112 113 private static void assertRedirect(HttpResponse response, String redirectUrl) { 114 String [] headerFields = response.getHeaderFields("location"); 115 if (redirectUrl != null) { 116 if (headerFields == null || headerFields.length == 0) { 117 fail("Redirect expected but not found"); 118 } else { 119 String strippedLocation = getStrippedLocation(headerFields[0]); 120 if (!redirectUrl.equals(strippedLocation)) { 121 fail("Redirect was incorrect : expected = '" + redirectUrl + "' actual = '" + strippedLocation + "'"); 122 } 123 } 124 } 125 } 126 127 private static String getStrippedLocation(String value) { 128 String strippedPrefix = value.substring(value.lastIndexOf("/") + 1); 129 String strippedPostfix = strippedPrefix.substring(0, strippedPrefix.indexOf(".") + 3); 130 return strippedPostfix; 131 } 132 133 private static Collection <String > getActionMessages(HttpResponse response, String headerName) { 134 String headerField = response.getHeaderField(headerName); 135 if (headerField == null) { 136 return Collections.<String > emptySet(); 137 } 138 139 Collection <String > messages = new HashSet <String >(); 140 for (StringTokenizer tokenizer = new StringTokenizer (headerField, ","); tokenizer.hasMoreTokens();) { 141 messages.add(tokenizer.nextToken()); 142 } 143 return messages; 144 } 145 } | Popular Tags |