| 1 |
package oauth.signpost.mocks; |
| 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.IOException; |
| 8 |
import java.io.InputStream; |
| 9 |
import java.net.HttpURLConnection; |
| 10 |
import java.net.MalformedURLException; |
| 11 |
|
| 12 |
import oauth.signpost.basic.DefaultOAuthProvider; |
| 13 |
import oauth.signpost.basic.HttpURLConnectionRequestAdapter; |
| 14 |
import oauth.signpost.http.HttpRequest; |
| 15 |
|
| 16 |
@SuppressWarnings("serial") |
| 17 |
public class DefaultOAuthProviderMock extends DefaultOAuthProvider implements OAuthProviderMock { |
| 18 |
|
| 19 |
private HttpURLConnection connectionMock; |
| 20 |
|
| 21 |
public DefaultOAuthProviderMock(String requestTokenUrl, String accessTokenUrl, String websiteUrl) { |
| 22 |
super(requestTokenUrl, accessTokenUrl, websiteUrl); |
| 23 |
} |
| 24 |
|
| 25 |
public void mockConnection(String responseBody) throws Exception { |
| 26 |
this.connectionMock = mock(HttpURLConnection.class); |
| 27 |
InputStream is = new ByteArrayInputStream(responseBody.getBytes()); |
| 28 |
when(connectionMock.getResponseCode()).thenReturn(200); |
| 29 |
when(connectionMock.getInputStream()).thenReturn(is); |
| 30 |
} |
| 31 |
|
| 32 |
@Override |
| 33 |
protected HttpRequest createRequest(String endpointUrl) throws MalformedURLException, |
| 34 |
IOException { |
| 35 |
return new HttpURLConnectionRequestAdapter(connectionMock); |
| 36 |
} |
| 37 |
} |