diff --git a/README.md b/README.md index fd15cea..1c5f49e 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ This means that all the power and flexibility of the underlying HTTP engine is s ### Modularity Since version 1.1, Signpost comes in modules. Apart from the core module, which you always need, you can download additional modules to support other HTTP messaging libraries than the one coming with the standard Java platform (which would be [java.net.HttpURLConnection](https://docs.oracle.com/javase/8/docs/api/java/net/HttpURLConnection.html)). -Apart from HttpURLConnection, Signpost currently has modules for [Apache Commons HTTP](https://hc.apache.org/) version 4, and [Jetty HTTP Client](https://www.eclipse.org/jetty/documentation/current/http-client-api.html) version 6. +Apart from HttpURLConnection, Signpost currently has modules for [Apache Commons HTTP](https://hc.apache.org/) version 4 and version 5 as well as [Jetty HTTP Client](https://www.eclipse.org/jetty/documentation/current/http-client-api.html) version 6. Limitations ------------ diff --git a/pom.xml b/pom.xml index db0dd09..588e5a5 100644 --- a/pom.xml +++ b/pom.xml @@ -32,6 +32,7 @@ signpost-core signpost-commonshttp3 signpost-commonshttp4 + signpost-commonshttp5 signpost-jetty6 @@ -99,13 +100,13 @@ junit junit - 4.5 + 4.13.2 test org.mockito - mockito-core - 1.9.0 + mockito-inline + 4.3.1 test @@ -115,10 +116,6 @@ ossrh https://oss.sonatype.org/service/local/staging/deploy/maven2 - - ossrh - https://oss.sonatype.org/content/repositories/snapshots - diff --git a/signpost-commonshttp3/src/test/java/oauth/signpost/commonshttp3/CommonHttpOAuthProviderMock.java b/signpost-commonshttp3/src/test/java/oauth/signpost/commonshttp3/CommonHttpOAuthProviderMock.java index ba26212..0bd6c6a 100644 --- a/signpost-commonshttp3/src/test/java/oauth/signpost/commonshttp3/CommonHttpOAuthProviderMock.java +++ b/signpost-commonshttp3/src/test/java/oauth/signpost/commonshttp3/CommonHttpOAuthProviderMock.java @@ -2,8 +2,6 @@ import oauth.signpost.commonshttp3.Http3ResponseAdapter; import oauth.signpost.commonshttp3.CommonsHttp3OAuthProvider; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; import java.io.ByteArrayInputStream; import java.io.InputStream; @@ -17,6 +15,8 @@ import org.mockito.Mockito; +import static org.mockito.Mockito.*; + @SuppressWarnings("serial") public class CommonHttpOAuthProviderMock extends CommonsHttp3OAuthProvider implements OAuthProviderMock { @@ -42,8 +42,8 @@ public void mockConnection(String responseBody) throws Exception { this.httpMethodMock = mock(HttpMethod.class); - when(httpMethodMock.getStatusLine()).thenReturn(statusLine); - when(httpMethodMock.getStatusCode()).thenReturn(200); - when(httpMethodMock.getResponseBodyAsStream()).thenReturn(is); + lenient().when(httpMethodMock.getStatusLine()).thenReturn(statusLine); + lenient().when(httpMethodMock.getStatusCode()).thenReturn(200); + lenient().when(httpMethodMock.getResponseBodyAsStream()).thenReturn(is); } } diff --git a/signpost-commonshttp3/src/test/java/oauth/signpost/commonshttp3/CommonsHttpOAuthProviderTest.java b/signpost-commonshttp3/src/test/java/oauth/signpost/commonshttp3/CommonsHttpOAuthProviderTest.java index 61b2de4..43f15f2 100644 --- a/signpost-commonshttp3/src/test/java/oauth/signpost/commonshttp3/CommonsHttpOAuthProviderTest.java +++ b/signpost-commonshttp3/src/test/java/oauth/signpost/commonshttp3/CommonsHttpOAuthProviderTest.java @@ -1,19 +1,17 @@ package oauth.signpost.commonshttp3; -import oauth.signpost.commonshttp3.CommonsHttp3OAuthProvider; import oauth.signpost.OAuth; import oauth.signpost.OAuthProvider; import oauth.signpost.OAuthProviderTest; - import org.junit.runner.RunWith; -import org.mockito.runners.MockitoJUnit44Runner; +import org.mockito.junit.MockitoJUnitRunner; -@RunWith(MockitoJUnit44Runner.class) +@RunWith(MockitoJUnitRunner.class) public class CommonsHttpOAuthProviderTest extends OAuthProviderTest { @Override protected OAuthProvider buildProvider(String requestTokenUrl, String accessTokenUrl, - String websiteUrl, boolean mockConnection) throws Exception { + String websiteUrl, boolean mockConnection) throws Exception { if (mockConnection) { CommonHttpOAuthProviderMock provider = new CommonHttpOAuthProviderMock(requestTokenUrl, accessTokenUrl, websiteUrl); diff --git a/signpost-commonshttp3/src/test/java/oauth/signpost/commonshttp3/HttpRequestAdapterTest.java b/signpost-commonshttp3/src/test/java/oauth/signpost/commonshttp3/HttpRequestAdapterTest.java index 2f32ceb..8c0979b 100644 --- a/signpost-commonshttp3/src/test/java/oauth/signpost/commonshttp3/HttpRequestAdapterTest.java +++ b/signpost-commonshttp3/src/test/java/oauth/signpost/commonshttp3/HttpRequestAdapterTest.java @@ -1,22 +1,20 @@ package oauth.signpost.commonshttp3; -import oauth.signpost.commonshttp3.Http3RequestAdapter; import oauth.signpost.basic.HttpRequestAdapterTestBase; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.RequestEntity; import org.apache.commons.httpclient.methods.StringRequestEntity; - import org.junit.runner.RunWith; -import org.mockito.runners.MockitoJUnit44Runner; +import org.mockito.junit.MockitoJUnitRunner; -@RunWith(MockitoJUnit44Runner.class) +@RunWith(MockitoJUnitRunner.class) public class HttpRequestAdapterTest extends HttpRequestAdapterTestBase { @Override public void prepareRequest() throws Exception { PostMethod method = new PostMethod(URL); method.setRequestHeader(HEADER_NAME, HEADER_VALUE); - RequestEntity body = new StringRequestEntity(PAYLOAD,CONTENT_TYPE,null); + RequestEntity body = new StringRequestEntity(PAYLOAD, CONTENT_TYPE, null); method.setRequestEntity(body); request = new Http3RequestAdapter(method); } diff --git a/signpost-commonshttp4/src/test/java/oauth/signpost/commonshttp/CommonsHttpOAuthProviderTest.java b/signpost-commonshttp4/src/test/java/oauth/signpost/commonshttp/CommonsHttpOAuthProviderTest.java index af9e5c8..52d645a 100644 --- a/signpost-commonshttp4/src/test/java/oauth/signpost/commonshttp/CommonsHttpOAuthProviderTest.java +++ b/signpost-commonshttp4/src/test/java/oauth/signpost/commonshttp/CommonsHttpOAuthProviderTest.java @@ -3,16 +3,15 @@ import oauth.signpost.OAuth; import oauth.signpost.OAuthProvider; import oauth.signpost.OAuthProviderTest; - import org.junit.runner.RunWith; -import org.mockito.runners.MockitoJUnit44Runner; +import org.mockito.junit.MockitoJUnitRunner; -@RunWith(MockitoJUnit44Runner.class) +@RunWith(MockitoJUnitRunner.class) public class CommonsHttpOAuthProviderTest extends OAuthProviderTest { @Override protected OAuthProvider buildProvider(String requestTokenUrl, String accessTokenUrl, - String websiteUrl, boolean mockConnection) throws Exception { + String websiteUrl, boolean mockConnection) throws Exception { if (mockConnection) { CommonHttpOAuthProviderMock provider = new CommonHttpOAuthProviderMock(requestTokenUrl, accessTokenUrl, websiteUrl); diff --git a/signpost-commonshttp4/src/test/java/oauth/signpost/commonshttp/HttpRequestAdapterTest.java b/signpost-commonshttp4/src/test/java/oauth/signpost/commonshttp/HttpRequestAdapterTest.java index 2148628..0103229 100644 --- a/signpost-commonshttp4/src/test/java/oauth/signpost/commonshttp/HttpRequestAdapterTest.java +++ b/signpost-commonshttp4/src/test/java/oauth/signpost/commonshttp/HttpRequestAdapterTest.java @@ -1,13 +1,12 @@ package oauth.signpost.commonshttp; import oauth.signpost.basic.HttpRequestAdapterTestBase; - import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.junit.runner.RunWith; -import org.mockito.runners.MockitoJUnit44Runner; +import org.mockito.junit.MockitoJUnitRunner; -@RunWith(MockitoJUnit44Runner.class) +@RunWith(MockitoJUnitRunner.class) public class HttpRequestAdapterTest extends HttpRequestAdapterTestBase { @Override diff --git a/signpost-commonshttp5/pom.xml b/signpost-commonshttp5/pom.xml new file mode 100644 index 0000000..84443f8 --- /dev/null +++ b/signpost-commonshttp5/pom.xml @@ -0,0 +1,33 @@ + + + oauth-signpost + oauth.signpost + 2.1.2-SNAPSHOT + + 4.0.0 + signpost-commonshttp5 + signpost-commonshttp5 + + + + oauth.signpost + signpost-core + ${project.version} + compile + + + org.apache.httpcomponents.client5 + httpclient5 + 5.1.2 + compile + + + oauth.signpost + signpost-core + ${project.version} + test-jar + test + + + + diff --git a/signpost-commonshttp5/src/main/java/oauth/signpost/commonshttp5/async/CommonsHttpAsyncOAuthConsumer.java b/signpost-commonshttp5/src/main/java/oauth/signpost/commonshttp5/async/CommonsHttpAsyncOAuthConsumer.java new file mode 100644 index 0000000..9656f9c --- /dev/null +++ b/signpost-commonshttp5/src/main/java/oauth/signpost/commonshttp5/async/CommonsHttpAsyncOAuthConsumer.java @@ -0,0 +1,43 @@ +/* Copyright (c) 2009 Matthias Kaeppler + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package oauth.signpost.commonshttp5.async; + +import oauth.signpost.AbstractOAuthConsumer; +import oauth.signpost.http.HttpRequest; +import org.apache.hc.client5.http.async.methods.SimpleHttpRequest; + +/** + * Supports signing HTTP requests of type {@link SimpleHttpRequest}. + * + * @author Kristof Jozsa + */ +public class CommonsHttpAsyncOAuthConsumer extends AbstractOAuthConsumer { + + private static final long serialVersionUID = 1L; + + public CommonsHttpAsyncOAuthConsumer(String consumerKey, String consumerSecret) { + super(consumerKey, consumerSecret); + } + + @Override + protected HttpRequest wrap(Object request) { + if (!(request instanceof SimpleHttpRequest)) { + throw new IllegalArgumentException("This consumer expects requests of type " + SimpleHttpRequest.class.getCanonicalName()); + } + + return new HttpAsyncRequestAdapter((SimpleHttpRequest) request); + } + +} diff --git a/signpost-commonshttp5/src/main/java/oauth/signpost/commonshttp5/async/CommonsHttpAsyncOAuthProvider.java b/signpost-commonshttp5/src/main/java/oauth/signpost/commonshttp5/async/CommonsHttpAsyncOAuthProvider.java new file mode 100644 index 0000000..380f927 --- /dev/null +++ b/signpost-commonshttp5/src/main/java/oauth/signpost/commonshttp5/async/CommonsHttpAsyncOAuthProvider.java @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2009 Matthias Kaeppler Licensed under the Apache License, + * Version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law + * or agreed to in writing, software distributed under the License is + * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ +package oauth.signpost.commonshttp5.async; + +import oauth.signpost.AbstractOAuthProvider; +import oauth.signpost.http.HttpRequest; +import oauth.signpost.http.HttpResponse; +import org.apache.hc.client5.http.async.methods.SimpleHttpRequest; +import org.apache.hc.client5.http.async.methods.SimpleHttpResponse; +import org.apache.hc.client5.http.async.methods.SimpleRequestProducer; +import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer; +import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient; +import org.apache.hc.client5.http.impl.async.HttpAsyncClients; +import org.apache.hc.client5.http.protocol.HttpClientContext; +import org.apache.hc.core5.concurrent.FutureCallback; +import org.apache.hc.core5.http.ClassicHttpResponse; +import org.apache.hc.core5.http.HttpEntity; +import org.apache.hc.core5.http.Method; +import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.apache.hc.core5.http.message.BasicHttpResponse; + +import java.io.IOException; +import java.net.URI; +import java.util.concurrent.Future; + +/** + * This implementation uses the Apache Commons {@link org.apache.hc.client5.http.async.HttpAsyncClient} 5.x HTTP + * implementation to fetch OAuth tokens from a service provider. + * + * @author Kristof Jozsa + */ +public class CommonsHttpAsyncOAuthProvider extends AbstractOAuthProvider { + + private static final long serialVersionUID = 1L; + + private transient CloseableHttpAsyncClient httpClient; + + public CommonsHttpAsyncOAuthProvider(String requestTokenEndpointUrl, String accessTokenEndpointUrl, + String authorizationWebsiteUrl) { + super(requestTokenEndpointUrl, accessTokenEndpointUrl, authorizationWebsiteUrl); + this.httpClient = HttpAsyncClients.createDefault(); + } + + public CommonsHttpAsyncOAuthProvider(String requestTokenEndpointUrl, String accessTokenEndpointUrl, + String authorizationWebsiteUrl, CloseableHttpAsyncClient httpClient) { + super(requestTokenEndpointUrl, accessTokenEndpointUrl, authorizationWebsiteUrl); + this.httpClient = httpClient; + } + + public void setHttpClient(CloseableHttpAsyncClient httpClient) { + this.httpClient = httpClient; + } + + @Override + protected HttpRequest createRequest(String endpointUrl) throws Exception { + return new HttpAsyncRequestAdapter(new SimpleHttpRequest(Method.POST, new URI(endpointUrl))); + } + + @Override + protected HttpResponse sendRequest(HttpRequest request) throws Exception { + SimpleRequestProducer requestProducer = SimpleRequestProducer.create((SimpleHttpRequest) request.unwrap()); + Future response = httpClient.execute(requestProducer, SimpleResponseConsumer.create(), HttpClientContext.create(), new FutureCallback() { + @Override + public void completed(SimpleHttpResponse simpleHttpResponse) { + } + + @Override + public void failed(Exception e) { + throw new RuntimeException(e); + } + + @Override + public void cancelled() { + } + }); + return new HttpAsyncResponseAdapter(response.get()); + } + + @Override + protected void closeConnection(HttpRequest request, oauth.signpost.http.HttpResponse response) throws Exception { + httpClient.close(); + } +} diff --git a/signpost-commonshttp5/src/main/java/oauth/signpost/commonshttp5/async/HttpAsyncRequestAdapter.java b/signpost-commonshttp5/src/main/java/oauth/signpost/commonshttp5/async/HttpAsyncRequestAdapter.java new file mode 100644 index 0000000..47b449a --- /dev/null +++ b/signpost-commonshttp5/src/main/java/oauth/signpost/commonshttp5/async/HttpAsyncRequestAdapter.java @@ -0,0 +1,67 @@ +package oauth.signpost.commonshttp5.async; + +import org.apache.hc.client5.http.async.methods.SimpleHttpRequest; +import org.apache.hc.core5.http.Header; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +public class HttpAsyncRequestAdapter implements oauth.signpost.http.HttpRequest { + + private SimpleHttpRequest request; + + + public HttpAsyncRequestAdapter(SimpleHttpRequest request) { + this.request = request; + this.request.setAbsoluteRequestUri(true); + } + + public String getMethod() { + return request.getMethod(); + } + + public String getRequestUrl() { + return request.getRequestUri(); + } + + public void setRequestUrl(String url) { + throw new RuntimeException(new UnsupportedOperationException()); + } + + public String getHeader(String name) { + Header header = request.getFirstHeader(name); + if (header == null) { + return null; + } + return header.getValue(); + } + + public void setHeader(String name, String value) { + request.setHeader(name, value); + } + + public Map getAllHeaders() { + Header[] origHeaders = request.getHeaders(); + HashMap headers = new HashMap(); + for (Header h : origHeaders) { + headers.put(h.getName(), h.getValue()); + } + return headers; + } + + public String getContentType() { + return request.getContentType().toString(); + } + + public InputStream getMessagePayload() throws IOException { + return new ByteArrayInputStream(request.getBody().getBodyBytes()); + } + + public Object unwrap() { + return request; + } +} diff --git a/signpost-commonshttp5/src/main/java/oauth/signpost/commonshttp5/async/HttpAsyncResponseAdapter.java b/signpost-commonshttp5/src/main/java/oauth/signpost/commonshttp5/async/HttpAsyncResponseAdapter.java new file mode 100644 index 0000000..2aa1e45 --- /dev/null +++ b/signpost-commonshttp5/src/main/java/oauth/signpost/commonshttp5/async/HttpAsyncResponseAdapter.java @@ -0,0 +1,35 @@ +package oauth.signpost.commonshttp5.async; + +import org.apache.hc.client5.http.async.methods.SimpleHttpResponse; +import org.apache.hc.core5.http.ClassicHttpResponse; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + + +public class HttpAsyncResponseAdapter implements oauth.signpost.http.HttpResponse { + + private SimpleHttpResponse response; + + public HttpAsyncResponseAdapter(SimpleHttpResponse response) { + this.response = response; + } + + public InputStream getContent() throws IOException { + byte[] bytes = response.getBodyBytes(); + return bytes == null ? new ByteArrayInputStream(new byte[0]) : new ByteArrayInputStream(bytes); + } + + public int getStatusCode() throws IOException { + return response.getCode(); + } + + public String getReasonPhrase() throws Exception { + return response.getReasonPhrase(); + } + + public Object unwrap() { + return response; + } +} diff --git a/signpost-commonshttp5/src/main/java/oauth/signpost/commonshttp5/sync/CommonsHttpSyncOAuthConsumer.java b/signpost-commonshttp5/src/main/java/oauth/signpost/commonshttp5/sync/CommonsHttpSyncOAuthConsumer.java new file mode 100644 index 0000000..c0a76ee --- /dev/null +++ b/signpost-commonshttp5/src/main/java/oauth/signpost/commonshttp5/sync/CommonsHttpSyncOAuthConsumer.java @@ -0,0 +1,43 @@ +/* Copyright (c) 2009 Matthias Kaeppler + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package oauth.signpost.commonshttp5.sync; + +import oauth.signpost.AbstractOAuthConsumer; +import oauth.signpost.http.HttpRequest; +import org.apache.hc.core5.http.message.BasicClassicHttpRequest; + +/** + * Supports signing HTTP requests of type {@link BasicClassicHttpRequest}. + * + * @author Kristof Jozsa + */ +public class CommonsHttpSyncOAuthConsumer extends AbstractOAuthConsumer { + + private static final long serialVersionUID = 1L; + + public CommonsHttpSyncOAuthConsumer(String consumerKey, String consumerSecret) { + super(consumerKey, consumerSecret); + } + + @Override + protected HttpRequest wrap(Object request) { + if (!(request instanceof BasicClassicHttpRequest)) { + throw new IllegalArgumentException("This consumer expects requests of type " + BasicClassicHttpRequest.class.getCanonicalName()); + } + + return new HttpSyncRequestAdapter((BasicClassicHttpRequest) request); + } + +} diff --git a/signpost-commonshttp5/src/main/java/oauth/signpost/commonshttp5/sync/CommonsHttpSyncOAuthProvider.java b/signpost-commonshttp5/src/main/java/oauth/signpost/commonshttp5/sync/CommonsHttpSyncOAuthProvider.java new file mode 100644 index 0000000..abbfcfa --- /dev/null +++ b/signpost-commonshttp5/src/main/java/oauth/signpost/commonshttp5/sync/CommonsHttpSyncOAuthProvider.java @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2009 Matthias Kaeppler Licensed under the Apache License, + * Version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law + * or agreed to in writing, software distributed under the License is + * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ +package oauth.signpost.commonshttp5.sync; + +import oauth.signpost.AbstractOAuthProvider; +import oauth.signpost.http.HttpRequest; +import org.apache.hc.client5.http.classic.methods.HttpPost; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.HttpClients; +import org.apache.hc.core5.http.ClassicHttpRequest; +import org.apache.hc.core5.http.ClassicHttpResponse; +import org.apache.hc.core5.http.HttpEntity; +import org.apache.hc.core5.http.io.entity.EntityUtils; + +import java.io.IOException; + +/** + * This implementation uses the Apache Commons {@link org.apache.hc.client5.http.classic.HttpClient} 5.x HTTP + * implementation to fetch OAuth tokens from a service provider. + * + * @author Kristof Jozsa + */ +public class CommonsHttpSyncOAuthProvider extends AbstractOAuthProvider { + + private static final long serialVersionUID = 1L; + + private transient CloseableHttpClient httpClient; + + public CommonsHttpSyncOAuthProvider(String requestTokenEndpointUrl, String accessTokenEndpointUrl, + String authorizationWebsiteUrl) { + super(requestTokenEndpointUrl, accessTokenEndpointUrl, authorizationWebsiteUrl); + this.httpClient = HttpClients.createDefault(); + } + + public CommonsHttpSyncOAuthProvider(String requestTokenEndpointUrl, String accessTokenEndpointUrl, + String authorizationWebsiteUrl, CloseableHttpClient httpClient) { + super(requestTokenEndpointUrl, accessTokenEndpointUrl, authorizationWebsiteUrl); + this.httpClient = httpClient; + } + + public void setHttpClient(CloseableHttpClient httpClient) { + this.httpClient = httpClient; + } + + @Override + protected HttpRequest createRequest(String endpointUrl) throws Exception { + HttpPost request = new HttpPost(endpointUrl); + return new HttpSyncRequestAdapter(request); + } + + @Override + protected oauth.signpost.http.HttpResponse sendRequest(HttpRequest request) throws Exception { + ClassicHttpResponse response = httpClient.execute((ClassicHttpRequest) request.unwrap()); + return new HttpSyncResponseAdapter(response); + } + + @Override + protected void closeConnection(HttpRequest request, oauth.signpost.http.HttpResponse response) + throws Exception { + if (response != null) { + HttpEntity entity = ((ClassicHttpResponse) response.unwrap()).getEntity(); + if (entity != null) { + try { + // free the connection + EntityUtils.consume(entity); + } catch (IOException e) { + // this means HTTP keep-alive is not possible + e.printStackTrace(); + } + } + } + } +} diff --git a/signpost-commonshttp5/src/main/java/oauth/signpost/commonshttp5/sync/HttpSyncRequestAdapter.java b/signpost-commonshttp5/src/main/java/oauth/signpost/commonshttp5/sync/HttpSyncRequestAdapter.java new file mode 100644 index 0000000..4155c45 --- /dev/null +++ b/signpost-commonshttp5/src/main/java/oauth/signpost/commonshttp5/sync/HttpSyncRequestAdapter.java @@ -0,0 +1,74 @@ +package oauth.signpost.commonshttp5.sync; + +import org.apache.hc.core5.http.Header; +import org.apache.hc.core5.http.HttpEntity; +import org.apache.hc.core5.http.message.BasicClassicHttpRequest; + +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + +public class HttpSyncRequestAdapter implements oauth.signpost.http.HttpRequest { + + private BasicClassicHttpRequest request; + + private HttpEntity entity; + + public HttpSyncRequestAdapter(BasicClassicHttpRequest request) { + this.request = request; + this.entity = request.getEntity(); + this.request.setAbsoluteRequestUri(true); + } + + public String getMethod() { + return request.getMethod(); + } + + public String getRequestUrl() { + return request.getRequestUri(); + } + + public void setRequestUrl(String url) { + throw new RuntimeException(new UnsupportedOperationException()); + } + + public String getHeader(String name) { + Header header = request.getFirstHeader(name); + if (header == null) { + return null; + } + return header.getValue(); + } + + public void setHeader(String name, String value) { + request.setHeader(name, value); + } + + public Map getAllHeaders() { + Header[] origHeaders = request.getHeaders(); + HashMap headers = new HashMap(); + for (Header h : origHeaders) { + headers.put(h.getName(), h.getValue()); + } + return headers; + } + + public String getContentType() { + if (entity == null) { + return null; + } + return entity.getContentType(); + } + + public InputStream getMessagePayload() throws IOException { + if (entity == null) { + return null; + } + return entity.getContent(); + } + + public Object unwrap() { + return request; + } +} diff --git a/signpost-commonshttp5/src/main/java/oauth/signpost/commonshttp5/sync/HttpSyncResponseAdapter.java b/signpost-commonshttp5/src/main/java/oauth/signpost/commonshttp5/sync/HttpSyncResponseAdapter.java new file mode 100644 index 0000000..3dd41ca --- /dev/null +++ b/signpost-commonshttp5/src/main/java/oauth/signpost/commonshttp5/sync/HttpSyncResponseAdapter.java @@ -0,0 +1,32 @@ +package oauth.signpost.commonshttp5.sync; + +import org.apache.hc.core5.http.ClassicHttpResponse; + +import java.io.IOException; +import java.io.InputStream; + + +public class HttpSyncResponseAdapter implements oauth.signpost.http.HttpResponse { + + private ClassicHttpResponse response; + + public HttpSyncResponseAdapter(ClassicHttpResponse response) { + this.response = response; + } + + public InputStream getContent() throws IOException { + return response.getEntity().getContent(); + } + + public int getStatusCode() throws IOException { + return response.getCode(); + } + + public String getReasonPhrase() throws Exception { + return response.getReasonPhrase(); + } + + public Object unwrap() { + return response; + } +} diff --git a/signpost-commonshttp5/src/test/java/oauth/signpost/commonshttp5/async/CommonHttpAsyncOAuthProviderMock.java b/signpost-commonshttp5/src/test/java/oauth/signpost/commonshttp5/async/CommonHttpAsyncOAuthProviderMock.java new file mode 100644 index 0000000..2562cf1 --- /dev/null +++ b/signpost-commonshttp5/src/test/java/oauth/signpost/commonshttp5/async/CommonHttpAsyncOAuthProviderMock.java @@ -0,0 +1,59 @@ +package oauth.signpost.commonshttp5.async; + +import oauth.signpost.http.HttpRequest; +import oauth.signpost.mocks.OAuthProviderMock; +import org.apache.hc.client5.http.async.methods.SimpleBody; +import org.apache.hc.client5.http.async.methods.SimpleHttpRequest; +import org.apache.hc.client5.http.async.methods.SimpleHttpResponse; +import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient; +import org.apache.hc.core5.concurrent.FutureCallback; +import org.apache.hc.core5.http.ContentType; +import org.apache.hc.core5.http.ProtocolVersion; +import org.apache.hc.core5.http.io.entity.InputStreamEntity; +import org.apache.hc.core5.http.message.StatusLine; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.util.concurrent.CompletableFuture; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +@SuppressWarnings("serial") +public class CommonHttpAsyncOAuthProviderMock extends CommonsHttpAsyncOAuthProvider implements OAuthProviderMock { + + private CloseableHttpAsyncClient httpClientMock; + + public CommonHttpAsyncOAuthProviderMock(String requestTokenUrl, String accessTokenUrl, String websiteUrl) { + super(requestTokenUrl, accessTokenUrl, websiteUrl); + } + + @Override + protected oauth.signpost.http.HttpResponse sendRequest(HttpRequest request) throws Exception { + SimpleHttpResponse response = httpClientMock.execute((SimpleHttpRequest) request.unwrap(), new FutureCallback() { + @Override + public void completed(SimpleHttpResponse simpleHttpResponse) { + } + + @Override + public void failed(Exception e) { + } + + @Override + public void cancelled() { + } + }).get(); + return new HttpAsyncResponseAdapter(response); + } + + public void mockConnection(String responseBody) throws Exception { + SimpleHttpResponse response = mock(SimpleHttpResponse.class); + this.httpClientMock = mock(CloseableHttpAsyncClient.class); + InputStream is = new ByteArrayInputStream(responseBody.getBytes()); + InputStreamEntity entity = new InputStreamEntity(is, responseBody.length(), ContentType.APPLICATION_JSON); + StatusLine statusLine = new StatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "OK"); + + when(response.getBodyBytes()).thenReturn(responseBody.getBytes()); + when(httpClientMock.execute(any(SimpleHttpRequest.class), any())).thenReturn(CompletableFuture.completedFuture(response)); + } +} diff --git a/signpost-commonshttp5/src/test/java/oauth/signpost/commonshttp5/async/CommonsHttpAsyncOAuthProviderTest.java b/signpost-commonshttp5/src/test/java/oauth/signpost/commonshttp5/async/CommonsHttpAsyncOAuthProviderTest.java new file mode 100644 index 0000000..52fa64e --- /dev/null +++ b/signpost-commonshttp5/src/test/java/oauth/signpost/commonshttp5/async/CommonsHttpAsyncOAuthProviderTest.java @@ -0,0 +1,23 @@ +package oauth.signpost.commonshttp5.async; + +import oauth.signpost.OAuth; +import oauth.signpost.OAuthProvider; +import oauth.signpost.OAuthProviderTest; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class CommonsHttpAsyncOAuthProviderTest extends OAuthProviderTest { + + @Override + protected OAuthProvider buildProvider(String requestTokenUrl, String accessTokenUrl, + String websiteUrl, boolean mockConnection) throws Exception { + if (mockConnection) { + CommonHttpAsyncOAuthProviderMock provider = new CommonHttpAsyncOAuthProviderMock(requestTokenUrl, accessTokenUrl, websiteUrl); + provider.mockConnection(OAuth.OAUTH_TOKEN + "=" + TOKEN + "&" + + OAuth.OAUTH_TOKEN_SECRET + "=" + TOKEN_SECRET); + return provider; + } + return new CommonsHttpAsyncOAuthProvider(requestTokenUrl, accessTokenUrl, websiteUrl); + } +} diff --git a/signpost-commonshttp5/src/test/java/oauth/signpost/commonshttp5/async/HttpAsyncRequestAdapterTest.java b/signpost-commonshttp5/src/test/java/oauth/signpost/commonshttp5/async/HttpAsyncRequestAdapterTest.java new file mode 100644 index 0000000..49623c6 --- /dev/null +++ b/signpost-commonshttp5/src/test/java/oauth/signpost/commonshttp5/async/HttpAsyncRequestAdapterTest.java @@ -0,0 +1,20 @@ +package oauth.signpost.commonshttp5.async; + +import oauth.signpost.basic.HttpRequestAdapterTestBase; +import org.apache.hc.client5.http.async.methods.SimpleHttpRequest; +import org.apache.hc.core5.http.ContentType; +import org.apache.hc.core5.http.Method; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class HttpAsyncRequestAdapterTest extends HttpRequestAdapterTestBase { + + @Override + public void prepareRequest() throws Exception { + SimpleHttpRequest request = SimpleHttpRequest.create(Method.POST.toString(), URL); + request.addHeader(HEADER_NAME, HEADER_VALUE); + request.setBody(PAYLOAD, ContentType.create(CONTENT_TYPE)); + this.request = new HttpAsyncRequestAdapter(request); + } +} diff --git a/signpost-commonshttp5/src/test/java/oauth/signpost/commonshttp5/sync/CommonHttpSyncOAuthProviderMock.java b/signpost-commonshttp5/src/test/java/oauth/signpost/commonshttp5/sync/CommonHttpSyncOAuthProviderMock.java new file mode 100644 index 0000000..562f050 --- /dev/null +++ b/signpost-commonshttp5/src/test/java/oauth/signpost/commonshttp5/sync/CommonHttpSyncOAuthProviderMock.java @@ -0,0 +1,47 @@ +package oauth.signpost.commonshttp5.sync; + +import oauth.signpost.http.HttpRequest; +import oauth.signpost.mocks.OAuthProviderMock; +import org.apache.hc.client5.http.classic.HttpClient; +import org.apache.hc.client5.http.classic.methods.HttpUriRequest; +import org.apache.hc.core5.http.ClassicHttpRequest; +import org.apache.hc.core5.http.ClassicHttpResponse; +import org.apache.hc.core5.http.ContentType; +import org.apache.hc.core5.http.ProtocolVersion; +import org.apache.hc.core5.http.io.entity.InputStreamEntity; +import org.apache.hc.core5.http.message.StatusLine; +import org.mockito.Mockito; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +@SuppressWarnings("serial") +public class CommonHttpSyncOAuthProviderMock extends CommonsHttpSyncOAuthProvider implements + OAuthProviderMock { + + private HttpClient httpClientMock; + + public CommonHttpSyncOAuthProviderMock(String requestTokenUrl, String accessTokenUrl, String websiteUrl) { + super(requestTokenUrl, accessTokenUrl, websiteUrl); + } + + @Override + protected oauth.signpost.http.HttpResponse sendRequest(HttpRequest request) throws Exception { + ClassicHttpResponse response = (ClassicHttpResponse) httpClientMock.execute((ClassicHttpRequest) request.unwrap()); + return new HttpSyncResponseAdapter(response); + } + + public void mockConnection(String responseBody) throws Exception { + ClassicHttpResponse response = mock(ClassicHttpResponse.class); + this.httpClientMock = mock(HttpClient.class); + InputStream is = new ByteArrayInputStream(responseBody.getBytes()); + InputStreamEntity entity = new InputStreamEntity(is, responseBody.length(), ContentType.APPLICATION_JSON); + StatusLine statusLine = new StatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "OK"); + + when(response.getEntity()).thenReturn(entity); + when(httpClientMock.execute(Mockito.any(HttpUriRequest.class))).thenReturn(response); + } +} diff --git a/signpost-commonshttp5/src/test/java/oauth/signpost/commonshttp5/sync/CommonsHttpSyncOAuthProviderTest.java b/signpost-commonshttp5/src/test/java/oauth/signpost/commonshttp5/sync/CommonsHttpSyncOAuthProviderTest.java new file mode 100644 index 0000000..570a0cc --- /dev/null +++ b/signpost-commonshttp5/src/test/java/oauth/signpost/commonshttp5/sync/CommonsHttpSyncOAuthProviderTest.java @@ -0,0 +1,24 @@ +package oauth.signpost.commonshttp5.sync; + +import oauth.signpost.OAuth; +import oauth.signpost.OAuthProvider; +import oauth.signpost.OAuthProviderTest; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class CommonsHttpSyncOAuthProviderTest extends OAuthProviderTest { + + @Override + protected OAuthProvider buildProvider(String requestTokenUrl, String accessTokenUrl, + String websiteUrl, boolean mockConnection) throws Exception { + if (mockConnection) { + CommonHttpSyncOAuthProviderMock provider = new CommonHttpSyncOAuthProviderMock(requestTokenUrl, + accessTokenUrl, websiteUrl); + provider.mockConnection(OAuth.OAUTH_TOKEN + "=" + TOKEN + "&" + + OAuth.OAUTH_TOKEN_SECRET + "=" + TOKEN_SECRET); + return provider; + } + return new CommonsHttpSyncOAuthProvider(requestTokenUrl, accessTokenUrl, websiteUrl); + } +} diff --git a/signpost-commonshttp5/src/test/java/oauth/signpost/commonshttp5/sync/HttpSyncRequestAdapterTest.java b/signpost-commonshttp5/src/test/java/oauth/signpost/commonshttp5/sync/HttpSyncRequestAdapterTest.java new file mode 100644 index 0000000..1da530d --- /dev/null +++ b/signpost-commonshttp5/src/test/java/oauth/signpost/commonshttp5/sync/HttpSyncRequestAdapterTest.java @@ -0,0 +1,21 @@ +package oauth.signpost.commonshttp5.sync; + +import oauth.signpost.basic.HttpRequestAdapterTestBase; +import org.apache.hc.client5.http.classic.methods.HttpPost; +import org.apache.hc.core5.http.ContentType; +import org.apache.hc.core5.http.io.entity.StringEntity; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class HttpSyncRequestAdapterTest extends HttpRequestAdapterTestBase { + + @Override + public void prepareRequest() throws Exception { + HttpPost r = new HttpPost(URL); + r.setHeader(HEADER_NAME, HEADER_VALUE); + StringEntity body = new StringEntity(PAYLOAD, ContentType.create(CONTENT_TYPE)); + r.setEntity(body); + request = new HttpSyncRequestAdapter(r); + } +} diff --git a/signpost-core/src/test/java/oauth/signpost/OAuthConsumerTest.java b/signpost-core/src/test/java/oauth/signpost/OAuthConsumerTest.java index c6fa383..688f444 100644 --- a/signpost-core/src/test/java/oauth/signpost/OAuthConsumerTest.java +++ b/signpost-core/src/test/java/oauth/signpost/OAuthConsumerTest.java @@ -1,38 +1,33 @@ package oauth.signpost; -import static org.junit.Assert.*; -import static org.hamcrest.CoreMatchers.*; -import static org.mockito.Matchers.argThat; -import static org.mockito.Matchers.eq; -import static org.mockito.Matchers.same; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; - import oauth.signpost.exception.OAuthExpectationFailedException; import oauth.signpost.http.HttpParameters; import oauth.signpost.http.HttpRequest; import oauth.signpost.signature.HmacSha1MessageSigner; import oauth.signpost.signature.OAuthMessageSigner; - import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.ArgumentMatcher; import org.mockito.ArgumentCaptor; -import org.mockito.runners.MockitoJUnit44Runner; +import org.mockito.ArgumentMatcher; +import org.mockito.junit.MockitoJUnitRunner; -@RunWith(MockitoJUnit44Runner.class) +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.not; +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +@RunWith(MockitoJUnitRunner.class) public abstract class OAuthConsumerTest extends SignpostTestBase { protected OAuthConsumer consumer; protected abstract OAuthConsumer buildConsumer(String consumerKey, String consumerSecret, - OAuthMessageSigner messageSigner); + OAuthMessageSigner messageSigner); @Test(expected = OAuthExpectationFailedException.class) public void shouldThrowIfConsumerKeyNotSet() throws Exception { @@ -101,17 +96,14 @@ public void shouldIncludeOAuthAndQueryAndBodyParams() throws Exception { // verify that all custom params are properly read and passed to the // message signer - ArgumentMatcher hasAllParameters = new ArgumentMatcher() { - public boolean matches(Object argument) { - HttpParameters params = (HttpParameters) argument; - assertEquals("1 1", params.getFirst("a", true)); - assertEquals("2 2", params.getFirst("b", true)); - assertEquals("http://example.com", params.getFirst("realm", true)); - assertEquals("12%345", params.getFirst("oauth_token", true)); - // signature should be dropped, not valid to pre-set - assertNull(params.getFirst("oauth_signature")); - return true; - } + ArgumentMatcher hasAllParameters = params -> { + assertEquals("1 1", params.getFirst("a", true)); + assertEquals("2 2", params.getFirst("b", true)); + assertEquals("http://example.com", params.getFirst("realm", true)); + assertEquals("12%345", params.getFirst("oauth_token", true)); + // signature should be dropped, not valid to pre-set + assertNull(params.getFirst("oauth_signature")); + return true; }; verify(signer).sign(same(request), argThat(hasAllParameters)); @@ -135,13 +127,10 @@ public void shouldHonorManuallySetSigningParameters() throws Exception { // verify that all custom params are properly read and passed to the // message signer - ArgumentMatcher hasParameters = new ArgumentMatcher() { - public boolean matches(Object argument) { - HttpParameters params = (HttpParameters) argument; - assertEquals("http://mycallback", params.getFirst("oauth_callback")); - assertEquals("1", params.getFirst("a")); - return true; - } + ArgumentMatcher hasParameters = params1 -> { + assertEquals("http://mycallback", params1.getFirst("oauth_callback")); + assertEquals("1", params1.getFirst("a")); + return true; }; verify(signer).sign(same(request), argThat(hasParameters)); @@ -237,11 +226,10 @@ public void run() { // argThat(new IsCompleteListOfOAuthParameters())); // } - private class IsCompleteListOfOAuthParameters extends ArgumentMatcher { + private class IsCompleteListOfOAuthParameters implements ArgumentMatcher { @Override - public boolean matches(Object argument) { - String oauthHeader = (String) argument; + public boolean matches(String oauthHeader) { assertTrue(oauthHeader.startsWith("OAuth ")); assertAllOAuthParametersExist(OAuth.oauthHeaderToParamsMap(oauthHeader)); return true; @@ -258,10 +246,10 @@ private void assertAllOAuthParametersExist(HttpParameters params) { assertNotNull(params.getFirst("oauth_version")); } - private class HasValuesPercentEncoded extends ArgumentMatcher { + private class HasValuesPercentEncoded implements ArgumentMatcher { @Override - public boolean matches(Object argument) { + public boolean matches(String argument) { String oauthHeader = (String) argument; HttpParameters params = OAuth.oauthHeaderToParamsMap(oauthHeader); assertEquals("1%252", params.getFirst("oauth_consumer_key")); diff --git a/signpost-core/src/test/java/oauth/signpost/OAuthProviderTest.java b/signpost-core/src/test/java/oauth/signpost/OAuthProviderTest.java index 88d3cf5..13341d1 100644 --- a/signpost-core/src/test/java/oauth/signpost/OAuthProviderTest.java +++ b/signpost-core/src/test/java/oauth/signpost/OAuthProviderTest.java @@ -1,32 +1,29 @@ package oauth.signpost; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.anyObject; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.net.URL; - import oauth.signpost.basic.DefaultOAuthConsumer; import oauth.signpost.exception.OAuthExpectationFailedException; import oauth.signpost.http.HttpParameters; import oauth.signpost.http.HttpRequest; import oauth.signpost.mocks.OAuthProviderMock; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import org.mockito.runners.MockitoJUnit44Runner; +import org.mockito.junit.MockitoJUnitRunner; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.net.URL; + +import static org.junit.Assert.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; -@RunWith(MockitoJUnit44Runner.class) +@RunWith(MockitoJUnitRunner.class) public abstract class OAuthProviderTest extends SignpostTestBase { protected OAuthProvider provider; @@ -46,26 +43,26 @@ public void prepare() throws Exception { when(consumerMock.getTokenSecret()).thenReturn(TOKEN_SECRET); provider = buildProvider(REQUEST_TOKEN_ENDPOINT_URL, ACCESS_TOKEN_ENDPOINT_URL, - AUTHORIZE_WEBSITE_URL, true); + AUTHORIZE_WEBSITE_URL, true); } protected abstract OAuthProvider buildProvider(String requestTokenUrl, String accessTokenUrl, - String websiteUrl, boolean mockConnection) throws Exception; + String websiteUrl, boolean mockConnection) throws Exception; @Test(expected = OAuthExpectationFailedException.class) public void shouldThrowExpectationFailedIfConsumerKeyNotSet() throws Exception { provider = buildProvider(REQUEST_TOKEN_ENDPOINT_URL, ACCESS_TOKEN_ENDPOINT_URL, - AUTHORIZE_WEBSITE_URL, true); + AUTHORIZE_WEBSITE_URL, true); provider.retrieveRequestToken(new DefaultOAuthConsumer(null, CONSUMER_SECRET), - REQUEST_TOKEN_ENDPOINT_URL); + REQUEST_TOKEN_ENDPOINT_URL); } @Test(expected = OAuthExpectationFailedException.class) public void shouldThrowExpectationFailedIfConsumerSecretNotSet() throws Exception { provider = buildProvider(REQUEST_TOKEN_ENDPOINT_URL, ACCESS_TOKEN_ENDPOINT_URL, - AUTHORIZE_WEBSITE_URL, true); + AUTHORIZE_WEBSITE_URL, true); provider.retrieveRequestToken(new DefaultOAuthConsumer(CONSUMER_KEY, null), - REQUEST_TOKEN_ENDPOINT_URL); + REQUEST_TOKEN_ENDPOINT_URL); } @Test @@ -74,7 +71,7 @@ public void shouldRetrieveRequestTokenAndUpdateConsumer() throws Exception { String callbackUrl = "http://www.example.com"; String result = provider.retrieveRequestToken(consumerMock, callbackUrl); - verify(consumerMock).sign((HttpRequest) anyObject()); + verify(consumerMock).sign((HttpRequest) any()); verify(consumerMock).setTokenWithSecret(TOKEN, TOKEN_SECRET); assertEquals(AUTHORIZE_WEBSITE_URL + "?" + OAuth.OAUTH_TOKEN + "=" + TOKEN + "&" @@ -84,7 +81,7 @@ public void shouldRetrieveRequestTokenAndUpdateConsumer() throws Exception { @Test public void shouldRespectCustomQueryParametersInAuthWebsiteUrl() throws Exception { provider = buildProvider(REQUEST_TOKEN_ENDPOINT_URL, ACCESS_TOKEN_ENDPOINT_URL, - "http://provider.com/authorize?q=1", true); + "http://provider.com/authorize?q=1", true); String callbackUrl = "http://www.example.com"; // the URL ctor checks for URL validity @@ -109,7 +106,7 @@ public void shouldRetrieveAccessTokenAndUpdateConsumer() throws Exception { provider.retrieveAccessToken(consumerMock, null); - verify(consumerMock).sign((HttpRequest) anyObject()); + verify(consumerMock).sign((HttpRequest) any()); verify(consumerMock).setTokenWithSecret(TOKEN, TOKEN_SECRET); } @@ -144,7 +141,7 @@ public void shouldBeSerializable() throws Exception { consumer.setTokenWithSecret(TOKEN, TOKEN_SECRET); provider = buildProvider(REQUEST_TOKEN_ENDPOINT_URL, ACCESS_TOKEN_ENDPOINT_URL, - AUTHORIZE_WEBSITE_URL, false); + AUTHORIZE_WEBSITE_URL, false); provider.setOAuth10a(true); // prepare a provider that has response params set diff --git a/signpost-core/src/test/java/oauth/signpost/SignpostTestBase.java b/signpost-core/src/test/java/oauth/signpost/SignpostTestBase.java index 137b4e1..2434909 100644 --- a/signpost-core/src/test/java/oauth/signpost/SignpostTestBase.java +++ b/signpost-core/src/test/java/oauth/signpost/SignpostTestBase.java @@ -1,5 +1,6 @@ package oauth.signpost; +import static org.mockito.Mockito.lenient; import static org.mockito.Mockito.when; import oauth.signpost.http.HttpRequest; import oauth.signpost.http.HttpParameters; @@ -59,13 +60,13 @@ public static void initOAuthParams() { public void initRequestMocks() { MockitoAnnotations.initMocks(this); - when(httpGetMock.getMethod()).thenReturn("GET"); - when(httpGetMock.getRequestUrl()).thenReturn("http://www.example.com"); + lenient().when(httpGetMock.getMethod()).thenReturn("GET"); + lenient().when(httpGetMock.getRequestUrl()).thenReturn("http://www.example.com"); - when(httpGetMockWithQueryString.getMethod()).thenReturn("GET"); - when(httpGetMockWithQueryString.getRequestUrl()).thenReturn("http://www.example.com?foo=bar"); + lenient().when(httpGetMockWithQueryString.getMethod()).thenReturn("GET"); + lenient().when(httpGetMockWithQueryString.getRequestUrl()).thenReturn("http://www.example.com?foo=bar"); - when(httpPostMock.getMethod()).thenReturn("POST"); - when(httpPostMock.getRequestUrl()).thenReturn("http://www.example.com"); + lenient().when(httpPostMock.getMethod()).thenReturn("POST"); + lenient().when(httpPostMock.getRequestUrl()).thenReturn("http://www.example.com"); } } diff --git a/signpost-core/src/test/java/oauth/signpost/basic/DefaultOAuthConsumerTest.java b/signpost-core/src/test/java/oauth/signpost/basic/DefaultOAuthConsumerTest.java index 73f448b..3a57e17 100644 --- a/signpost-core/src/test/java/oauth/signpost/basic/DefaultOAuthConsumerTest.java +++ b/signpost-core/src/test/java/oauth/signpost/basic/DefaultOAuthConsumerTest.java @@ -6,9 +6,9 @@ import org.junit.Before; import org.junit.runner.RunWith; -import org.mockito.runners.MockitoJUnit44Runner; +import org.mockito.junit.MockitoJUnitRunner; -@RunWith(MockitoJUnit44Runner.class) +@RunWith(MockitoJUnitRunner.class) public class DefaultOAuthConsumerTest extends OAuthConsumerTest { @Before diff --git a/signpost-core/src/test/java/oauth/signpost/basic/DefaultOAuthProviderTest.java b/signpost-core/src/test/java/oauth/signpost/basic/DefaultOAuthProviderTest.java index 0c0cb61..0c2eba4 100644 --- a/signpost-core/src/test/java/oauth/signpost/basic/DefaultOAuthProviderTest.java +++ b/signpost-core/src/test/java/oauth/signpost/basic/DefaultOAuthProviderTest.java @@ -4,15 +4,14 @@ import oauth.signpost.OAuthProvider; import oauth.signpost.OAuthProviderTest; import oauth.signpost.mocks.DefaultOAuthProviderMock; - import org.junit.runner.RunWith; -import org.mockito.runners.MockitoJUnit44Runner; +import org.mockito.junit.MockitoJUnitRunner; -@RunWith(MockitoJUnit44Runner.class) +@RunWith(MockitoJUnitRunner.class) public class DefaultOAuthProviderTest extends OAuthProviderTest { protected OAuthProvider buildProvider(String requestTokenUrl, String accessTokenUrl, - String websiteUrl, boolean mockConnection) throws Exception { + String websiteUrl, boolean mockConnection) throws Exception { if (mockConnection) { DefaultOAuthProviderMock provider = new DefaultOAuthProviderMock(requestTokenUrl, accessTokenUrl, websiteUrl); diff --git a/signpost-core/src/test/java/oauth/signpost/basic/HttpRequestAdapterTest.java b/signpost-core/src/test/java/oauth/signpost/basic/HttpRequestAdapterTest.java index 531ac29..1bd98ee 100644 --- a/signpost-core/src/test/java/oauth/signpost/basic/HttpRequestAdapterTest.java +++ b/signpost-core/src/test/java/oauth/signpost/basic/HttpRequestAdapterTest.java @@ -1,14 +1,14 @@ package oauth.signpost.basic; -import static org.junit.Assert.assertTrue; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; import java.net.HttpURLConnection; import java.net.URL; -import org.junit.runner.RunWith; -import org.mockito.runners.MockitoJUnit44Runner; +import static org.junit.Assert.assertTrue; -@RunWith(MockitoJUnit44Runner.class) +@RunWith(MockitoJUnitRunner.class) public class HttpRequestAdapterTest extends HttpRequestAdapterTestBase { @Override diff --git a/signpost-core/src/test/java/oauth/signpost/http/HttpParametersTest.java b/signpost-core/src/test/java/oauth/signpost/http/HttpParametersTest.java index 6ccd718..3fdabfc 100644 --- a/signpost-core/src/test/java/oauth/signpost/http/HttpParametersTest.java +++ b/signpost-core/src/test/java/oauth/signpost/http/HttpParametersTest.java @@ -1,18 +1,16 @@ package oauth.signpost.http; -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertFalse; -import static junit.framework.Assert.assertTrue; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; import java.util.HashMap; import java.util.LinkedList; import java.util.List; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.runners.MockitoJUnit44Runner; +import static junit.framework.Assert.*; -@RunWith(MockitoJUnit44Runner.class) +@RunWith(MockitoJUnitRunner.class) public class HttpParametersTest { @Test @@ -51,7 +49,7 @@ public void testBasicBehavior() { assertEquals(0, params.size()); assertEquals(null, params.get("a")); - String[] kvPairs = new String[] { "a", "1", "b", "2" }; + String[] kvPairs = new String[]{"a", "1", "b", "2"}; params.putAll(kvPairs, false); assertEquals("1", params.getFirst("a")); assertEquals("2", params.getFirst("b")); diff --git a/signpost-core/src/test/java/oauth/signpost/signature/OAuthMessageSignerTest.java b/signpost-core/src/test/java/oauth/signpost/signature/OAuthMessageSignerTest.java index 4d6f584..099ab4c 100644 --- a/signpost-core/src/test/java/oauth/signpost/signature/OAuthMessageSignerTest.java +++ b/signpost-core/src/test/java/oauth/signpost/signature/OAuthMessageSignerTest.java @@ -1,17 +1,17 @@ package oauth.signpost.signature; -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; import oauth.signpost.SignpostTestBase; -import oauth.signpost.http.HttpRequest; import oauth.signpost.http.HttpParameters; - +import oauth.signpost.http.HttpRequest; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.runners.MockitoJUnit44Runner; +import org.mockito.junit.MockitoJUnitRunner; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; -@RunWith(MockitoJUnit44Runner.class) +@RunWith(MockitoJUnitRunner.class) public class OAuthMessageSignerTest extends SignpostTestBase { @Test diff --git a/signpost-core/src/test/java/oauth/signpost/signature/SignatureBaseStringTest.java b/signpost-core/src/test/java/oauth/signpost/signature/SignatureBaseStringTest.java index 06b7b5b..76a3036 100644 --- a/signpost-core/src/test/java/oauth/signpost/signature/SignatureBaseStringTest.java +++ b/signpost-core/src/test/java/oauth/signpost/signature/SignatureBaseStringTest.java @@ -1,19 +1,17 @@ package oauth.signpost.signature; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; import oauth.signpost.SignpostTestBase; -import oauth.signpost.http.HttpRequest; import oauth.signpost.http.HttpParameters; - +import oauth.signpost.http.HttpRequest; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.runners.MockitoJUnit44Runner; +import org.mockito.junit.MockitoJUnitRunner; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; -@RunWith(MockitoJUnit44Runner.class) +@RunWith(MockitoJUnitRunner.class) public class SignatureBaseStringTest extends SignpostTestBase { @Test diff --git a/signpost-core/src/test/java/oauth/signpost/signature/SigningStrategyTest.java b/signpost-core/src/test/java/oauth/signpost/signature/SigningStrategyTest.java index dd33108..574de8d 100644 --- a/signpost-core/src/test/java/oauth/signpost/signature/SigningStrategyTest.java +++ b/signpost-core/src/test/java/oauth/signpost/signature/SigningStrategyTest.java @@ -1,14 +1,14 @@ package oauth.signpost.signature; -import static org.junit.Assert.assertEquals; import oauth.signpost.SignpostTestBase; import oauth.signpost.http.HttpParameters; - import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.runners.MockitoJUnit44Runner; +import org.mockito.junit.MockitoJUnitRunner; + +import static org.junit.Assert.assertEquals; -@RunWith(MockitoJUnit44Runner.class) +@RunWith(MockitoJUnitRunner.class) public class SigningStrategyTest extends SignpostTestBase { @Test diff --git a/signpost-jetty6/src/test/java/oauth/signpost/jetty/HttpRequestAdapterTest.java b/signpost-jetty6/src/test/java/oauth/signpost/jetty/HttpRequestAdapterTest.java index 7b9a4f0..9ff0611 100644 --- a/signpost-jetty6/src/test/java/oauth/signpost/jetty/HttpRequestAdapterTest.java +++ b/signpost-jetty6/src/test/java/oauth/signpost/jetty/HttpRequestAdapterTest.java @@ -1,13 +1,12 @@ package oauth.signpost.jetty; import oauth.signpost.basic.HttpRequestAdapterTestBase; - import org.junit.runner.RunWith; -import org.mockito.runners.MockitoJUnit44Runner; +import org.mockito.junit.MockitoJUnitRunner; import org.mortbay.io.ByteArrayBuffer; import org.mortbay.jetty.client.HttpExchange; -@RunWith(MockitoJUnit44Runner.class) +@RunWith(MockitoJUnitRunner.class) public class HttpRequestAdapterTest extends HttpRequestAdapterTestBase { @Override