Created
May 10, 2012 23:00
-
-
Save electrum/2656457 to your computer and use it in GitHub Desktop.
Platform TestingHttpClient
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.proofpoint.dphillips.bootcamp; | |
import com.google.common.collect.ImmutableListMultimap; | |
import com.google.common.collect.ListMultimap; | |
import com.proofpoint.http.client.HttpClient; | |
import com.proofpoint.http.client.Request; | |
import com.proofpoint.http.client.RequestStats; | |
import com.proofpoint.http.client.Response; | |
import com.proofpoint.http.client.ResponseHandler; | |
import java.io.ByteArrayInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import static javax.ws.rs.core.Response.Status.fromStatusCode; | |
public class TestingHttpClient | |
implements HttpClient | |
{ | |
private Request lastRequest; | |
private int statusCode; | |
private String contentType; | |
private byte[] responseBody; | |
@Override | |
public RequestStats getStats() | |
{ | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public <T, E extends Exception> T execute(Request request, ResponseHandler<T, E> responseHandler) | |
throws E | |
{ | |
lastRequest = request; | |
TestingResponse response = new TestingResponse(statusCode, contentType, responseBody); | |
return responseHandler.handle(request, response); | |
} | |
public void setResponseStatusCode(int statusCode) | |
{ | |
this.statusCode = statusCode; | |
} | |
public void setResponseContentType(String contentType) | |
{ | |
this.contentType = contentType; | |
} | |
public void setResponseBody(byte[] responseBody) | |
{ | |
this.responseBody = responseBody; | |
} | |
public Request getLastRequest() | |
{ | |
return lastRequest; | |
} | |
private static class TestingResponse | |
implements Response | |
{ | |
private final int statusCode; | |
private final String contentType; | |
private final byte[] body; | |
private TestingResponse(int statusCode, String contentType, byte[] body) | |
{ | |
this.statusCode = statusCode; | |
this.contentType = contentType; | |
this.body = body; | |
} | |
@Override | |
public int getStatusCode() | |
{ | |
return statusCode; | |
} | |
@Override | |
public String getStatusMessage() | |
{ | |
return fromStatusCode(statusCode).getReasonPhrase(); | |
} | |
@Override | |
public String getHeader(String name) | |
{ | |
if (name.equalsIgnoreCase("content-type")) { | |
return contentType; | |
} | |
return null; | |
} | |
@Override | |
public ListMultimap<String, String> getHeaders() | |
{ | |
return ImmutableListMultimap.of("Content-Type", contentType); | |
} | |
@Override | |
public long getBytesRead() | |
{ | |
return body.length; | |
} | |
@Override | |
public InputStream getInputStream() | |
throws IOException | |
{ | |
return new ByteArrayInputStream(body); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment