|
15 | 15 | */ |
16 | 16 | package com.ocpsoft.rewrite.test; |
17 | 17 |
|
| 18 | +import java.io.IOException; |
| 19 | +import java.io.InputStream; |
| 20 | +import java.io.InputStreamReader; |
| 21 | +import java.io.Reader; |
| 22 | +import java.io.UnsupportedEncodingException; |
18 | 23 | import java.util.ArrayList; |
19 | 24 | import java.util.List; |
20 | 25 |
|
|
27 | 32 | import org.apache.http.protocol.ExecutionContext; |
28 | 33 | import org.apache.http.protocol.HttpContext; |
29 | 34 |
|
| 35 | +import com.ocpsoft.common.util.Streams; |
| 36 | +import com.ocpsoft.rewrite.exception.RewriteException; |
| 37 | + |
30 | 38 | /** |
31 | 39 | * @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a> |
32 | 40 | * |
@@ -133,4 +141,41 @@ public int getStatusCode() |
133 | 141 | { |
134 | 142 | return response.getStatusLine().getStatusCode(); |
135 | 143 | } |
| 144 | + |
| 145 | + public String getResponseContent() |
| 146 | + { |
| 147 | + try { |
| 148 | + return toString(getResponse().getEntity().getContent()); |
| 149 | + } |
| 150 | + catch (Exception e) { |
| 151 | + throw new RewriteException("Could not stringify response InputStream", e); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Return a {@link String} containing the contents of the given {@link InputStream} |
| 157 | + */ |
| 158 | + public static String toString(final InputStream stream) |
| 159 | + { |
| 160 | + StringBuilder out = new StringBuilder(); |
| 161 | + try { |
| 162 | + final char[] buffer = new char[0x10000]; |
| 163 | + Reader in = new InputStreamReader(stream, "UTF-8"); |
| 164 | + int read; |
| 165 | + do { |
| 166 | + read = in.read(buffer, 0, buffer.length); |
| 167 | + if (read > 0) { |
| 168 | + out.append(buffer, 0, read); |
| 169 | + } |
| 170 | + } |
| 171 | + while (read >= 0); |
| 172 | + } |
| 173 | + catch (UnsupportedEncodingException e) { |
| 174 | + throw new RuntimeException(e); |
| 175 | + } |
| 176 | + catch (IOException e) { |
| 177 | + throw new RuntimeException(e); |
| 178 | + } |
| 179 | + return out.toString(); |
| 180 | + } |
136 | 181 | } |
0 commit comments