| 1 |
package oauth.signpost.commonshttp; |
| 2 |
|
| 3 |
import static org.mockito.Mockito.mock; |
| 4 |
import static org.mockito.Mockito.when; |
| 5 |
|
| 6 |
import java.io.ByteArrayInputStream; |
| 7 |
import java.io.InputStream; |
| 8 |
|
| 9 |
import oauth.signpost.http.HttpRequest; |
| 10 |
import oauth.signpost.mocks.OAuthProviderMock; |
| 11 |
|
| 12 |
import org.apache.http.HttpResponse; |
| 13 |
import org.apache.http.ProtocolVersion; |
| 14 |
import org.apache.http.StatusLine; |
| 15 |
import org.apache.http.client.HttpClient; |
| 16 |
import org.apache.http.client.methods.HttpUriRequest; |
| 17 |
import org.apache.http.entity.InputStreamEntity; |
| 18 |
import org.apache.http.message.BasicStatusLine; |
| 19 |
import org.mockito.Mockito; |
| 20 |
|
| 21 |
@SuppressWarnings("serial") |
| 22 |
public class CommonHttpOAuthProviderMock extends CommonsHttpOAuthProvider implements |
| 23 |
OAuthProviderMock { |
| 24 |
|
| 25 |
private HttpClient httpClientMock; |
| 26 |
|
| 27 |
public CommonHttpOAuthProviderMock(String requestTokenUrl, String accessTokenUrl, |
| 28 |
String websiteUrl) { |
| 29 |
super(requestTokenUrl, accessTokenUrl, websiteUrl); |
| 30 |
} |
| 31 |
|
| 32 |
@Override |
| 33 |
protected oauth.signpost.http.HttpResponse sendRequest(HttpRequest request) throws Exception { |
| 34 |
HttpResponse resp = httpClientMock.execute((HttpUriRequest) request.unwrap()); |
| 35 |
return new HttpResponseAdapter(resp); |
| 36 |
} |
| 37 |
|
| 38 |
public void mockConnection(String responseBody) throws Exception { |
| 39 |
HttpResponse response = mock(HttpResponse.class); |
| 40 |
this.httpClientMock = mock(HttpClient.class); |
| 41 |
InputStream is = new ByteArrayInputStream(responseBody.getBytes()); |
| 42 |
InputStreamEntity entity = new InputStreamEntity(is, responseBody.length()); |
| 43 |
StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "OK"); |
| 44 |
|
| 45 |
when(response.getStatusLine()).thenReturn(statusLine); |
| 46 |
when(response.getEntity()).thenReturn(entity); |
| 47 |
when(httpClientMock.execute(Mockito.any(HttpUriRequest.class))).thenReturn(response); |
| 48 |
} |
| 49 |
} |