A fake WebConnection designed to mock out the actual HTTP connections. @author Mike Bowler @author Noboru Sinohara @author Marc Guillemot @author Brad Clarke @author Ahmed Ashour @author Ronald Brill
| 42 | * @author Ronald Brill |
| 43 | */ |
| 44 | public class MockWebConnection implements WebConnection { |
| 45 | |
| 46 | private static final Log LOG = LogFactory.getLog(MockWebConnection.class); |
| 47 | |
| 48 | private final Map<String, IOException> throwableMap_ = new HashMap<>(); |
| 49 | private final Map<String, RawResponseData> responseMap_ = new HashMap<>(); |
| 50 | private RawResponseData defaultResponse_; |
| 51 | private WebRequest lastRequest_; |
| 52 | private int requestCount_; |
| 53 | private final List<URL> requestedUrls_ = Collections.synchronizedList(new ArrayList<>()); |
| 54 | |
| 55 | /** |
| 56 | * Contains the raw data configured for a response. |
| 57 | */ |
| 58 | public static class RawResponseData { |
| 59 | private final List<NameValuePair> headers_; |
| 60 | private final byte[] byteContent_; |
| 61 | private final String stringContent_; |
| 62 | private final int statusCode_; |
| 63 | private final String statusMessage_; |
| 64 | private Charset charset_; |
| 65 | |
| 66 | RawResponseData(final byte[] byteContent, final int statusCode, final String statusMessage, |
| 67 | final String contentType, final List<NameValuePair> headers) { |
| 68 | byteContent_ = byteContent; |
| 69 | stringContent_ = null; |
| 70 | statusCode_ = statusCode; |
| 71 | statusMessage_ = statusMessage; |
| 72 | headers_ = compileHeaders(headers, contentType); |
| 73 | } |
| 74 | |
| 75 | RawResponseData(final String stringContent, final Charset charset, final int statusCode, |
| 76 | final String statusMessage, final String contentType, final List<NameValuePair> headers) { |
| 77 | byteContent_ = null; |
| 78 | charset_ = charset; |
| 79 | stringContent_ = stringContent; |
| 80 | statusCode_ = statusCode; |
| 81 | statusMessage_ = statusMessage; |
| 82 | headers_ = compileHeaders(headers, contentType); |
| 83 | } |
| 84 | |
| 85 | private static List<NameValuePair> compileHeaders(final List<NameValuePair> headers, final String contentType) { |
| 86 | final List<NameValuePair> compiledHeaders = new ArrayList<>(); |
| 87 | if (headers != null) { |
| 88 | compiledHeaders.addAll(headers); |
| 89 | } |
| 90 | if (contentType != null) { |
| 91 | compiledHeaders.add(new NameValuePair(HttpHeader.CONTENT_TYPE, contentType)); |
| 92 | } |
| 93 | return compiledHeaders; |
| 94 | } |
| 95 | |
| 96 | WebResponseData asWebResponseData() { |
| 97 | final byte[] content; |
| 98 | if (byteContent_ != null) { |
| 99 | content = byteContent_; |
| 100 | } |
| 101 | else if (stringContent_ == null) { |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…