1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.testframework.htmlunit;
35
36 import static org.junit.Assert.*;
37
38 import info.magnolia.testframework.AbstractMagnoliaIntegrationTest;
39
40 import java.io.FileOutputStream;
41 import java.io.IOException;
42 import java.io.InputStream;
43 import java.net.HttpURLConnection;
44 import java.net.URL;
45 import java.util.Collections;
46 import java.util.HashMap;
47 import java.util.Map;
48
49 import org.apache.commons.codec.binary.Base64;
50 import org.apache.commons.io.IOUtils;
51
52 import com.gargoylesoftware.htmlunit.BrowserVersion;
53 import com.gargoylesoftware.htmlunit.Page;
54 import com.gargoylesoftware.htmlunit.WebClient;
55 import com.gargoylesoftware.htmlunit.WebConnection;
56 import com.gargoylesoftware.htmlunit.WebResponse;
57 import com.gargoylesoftware.htmlunit.html.HtmlPage;
58 import com.gargoylesoftware.htmlunit.util.DebuggingWebConnection;
59
60
61
62
63
64 public abstract class AbstractMagnoliaHtmlUnitTest extends AbstractMagnoliaIntegrationTest{
65
66
67
68
69 protected static final String SESSION_ID_REGEXP = ";jsessionid=[a-zA-Z0-9]+";
70
71 protected static final Map<String, String> DEFAULT_HEADERS = new HashMap<String, String>() {{
72 put("Referer", Instance.AUTHOR.getURL(""));
73 }};
74
75
76
77
78 protected HttpURLConnection openConnection(Instance instance, String path, User user) throws IOException {
79 return openConnection(instance, path, user, Collections.<String, String>emptyMap());
80 }
81
82
83
84
85 protected HttpURLConnection openConnection(Instance instance, String path, User user, Map<String, String> headers) throws IOException {
86 final URL url = new URL(instance.getURL(path));
87 final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
88 if (user != null) {
89 final String authValue = getAuthValue(user.name());
90 connection.setRequestProperty("Authorization", authValue);
91 }
92
93 for (String header : headers.keySet()) {
94 connection.setRequestProperty(header, headers.get(header));
95 }
96
97 connection.connect();
98 return connection;
99 }
100
101
102
103
104
105
106 @Deprecated
107 protected HtmlPage openHtmlPage(Instance instance, String path, User user) throws IOException {
108 return openHtmlPage(instance, path, user, false);
109 }
110
111
112
113
114 @Deprecated
115 protected HtmlPage openHtmlPage(Instance instance, String path, User user, boolean followRedirects) throws IOException {
116 return (HtmlPage) openPage(instance, path, user, followRedirects);
117 }
118
119
120
121
122
123 @Deprecated
124 protected Page openPage(Instance instance, String path, User user) throws IOException {
125 return openPage(instance, path, user, false);
126 }
127
128
129
130
131
132 @Deprecated
133 protected <P extends Page> P openPage(Instance instance, String path, User user, boolean followRedirects) throws IOException {
134 return (P) openPage(instance.getURL(path), user, followRedirects, true, DEFAULT_HEADERS);
135 }
136
137 protected <P extends Page> P openPage(String url, User user) throws IOException {
138 return (P) openPage(url, user, false, true, DEFAULT_HEADERS);
139 }
140
141 protected <P extends Page> P openPage(String url, User user, boolean followRedirects) throws IOException {
142 return (P) openPage(url, user, followRedirects, true, DEFAULT_HEADERS);
143 }
144
145 protected <P extends Page> P openPage(String url, User user, boolean followRedirects, boolean enableJavascript) throws IOException {
146 return (P) openPage(url, user, followRedirects, enableJavascript, DEFAULT_HEADERS);
147 }
148
149
150
151
152 protected <P extends Page> P openPage(String url, User user, boolean followRedirects, boolean enableJavascript, Map<String, String> headers) throws IOException {
153 final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_24);
154
155 final WebConnection connection = new DebuggingWebConnection(webClient.getWebConnection(), "magnolia-test_");
156 webClient.setWebConnection(connection);
157
158 webClient.getOptions().setRedirectEnabled(followRedirects);
159
160
161 webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
162
163 webClient.getOptions().setCssEnabled(true);
164 webClient.getOptions().setJavaScriptEnabled(enableJavascript);
165
166
167 for (String header : headers.keySet()) {
168 webClient.addRequestHeader(header, headers.get(header));
169 }
170
171 if (user != null) {
172 final String authValue = getAuthValue(user.name());
173 webClient.addRequestHeader("Authorization", authValue);
174 }
175
176 return (P) webClient.getPage(new URL(url));
177 }
178
179 protected <P extends Page> P assertRedirected(String reason, String expectedTargetURLPattern, Page page, User user) throws IOException {
180 assertEquals(302, page.getWebResponse().getStatusCode());
181 final String location = page.getWebResponse().getResponseHeaderValue("Location");
182
183 assertTrue("Redirect location " + location + " does not match the expected pattern: " + expectedTargetURLPattern + " ("+reason+")", location.matches(expectedTargetURLPattern));
184
185
186
187 return (P) openPage(location, user, false, false, DEFAULT_HEADERS);
188 }
189
190
191
192
193
194 protected void saveToFile(Page page, Throwable fakeException) throws IOException {
195 saveToFile(page, fakeException.getStackTrace()[0]);
196 }
197
198
199
200
201
202
203
204
205 protected void saveToFile(Page page, StackTraceElement stackTraceElement) throws IOException {
206 final WebResponse res = page.getWebResponse();
207 InputStream input = res.getContentAsStream();
208 final byte[] body = IOUtils.toByteArray(input);
209
210 final String path = "target/" + stackTraceElement.getClassName() + "-" + stackTraceElement.getMethodName() + "-" + stackTraceElement.getLineNumber() + ".out";
211 IOUtils.write(body, new FileOutputStream(path));
212 }
213
214
215
216
217 protected String setSystemProperty(Instance instance, String name, String value) throws IOException {
218 HtmlPage page = openPage(instance.getURL("/.magnolia/sysprop/?name=" + name + "&value=" + value), User.superuser);
219 assertEquals(200, page.getWebResponse().getStatusCode());
220 return page.asText();
221 }
222
223
224
225
226 private String getAuthValue(String usernameAndPassword) {
227 final String authString = usernameAndPassword + ":" + usernameAndPassword;
228 final String encodedAuthStr = new String(Base64.encodeBase64(authString.getBytes()));
229 return "Basic " + encodedAuthStr;
230 }
231 }