Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. Enabled EthCall integration test.
2. Fixed issue with http connections not persisting. 3. Fixed serialisation issue with DefaultBlockParameterName. 4. Added RequestTest for unit testing requests. 5. Bumped version.
- Loading branch information
Showing
6 changed files
with
82 additions
and
11 deletions.
There are no files selected for viewing
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
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
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
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
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
64 changes: 64 additions & 0 deletions
64
src/test/java/org/web3j/protocol/jsonrpc20/RequestTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package org.web3j.protocol.jsonrpc20; | ||
|
||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
import org.apache.http.client.ResponseHandler; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.ArgumentCaptor; | ||
|
||
import org.web3j.methods.request.EthCall; | ||
import org.web3j.protocol.Web3j; | ||
import org.web3j.protocol.http.HttpService; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.mockito.Matchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
|
||
|
||
public class RequestTest { | ||
|
||
private CloseableHttpClient closeableHttpClient; | ||
private HttpService httpService; | ||
private Web3j web3j; | ||
|
||
@Before | ||
public void setUp() { | ||
closeableHttpClient = mock(CloseableHttpClient.class); | ||
httpService = new HttpService("", closeableHttpClient); | ||
web3j = Web3j.build(httpService); | ||
} | ||
|
||
@Test | ||
public void testEthCall() throws Exception { | ||
web3j.ethCall(new EthCall("0x52b93c80364dc2dd4444c146d73b9836bbbb2b3f", "0x0"), | ||
DefaultBlockParameter.valueOf("latest")).send(); | ||
|
||
verifyResult("{\"jsonRpc\":\"2.0\",\"method\":\"eth_call\",\"params\":[{\"toAddress\":\"0x52b93c80364dc2dd4444c146d73b9836bbbb2b3f\",\"data\":\"0x0\"},\"latest\"],\"id\":1}"); | ||
} | ||
|
||
private void verifyResult(String expected) throws Exception { | ||
ArgumentCaptor<HttpPost> httpPostArgumentCaptor = ArgumentCaptor.forClass(HttpPost.class); | ||
verify(closeableHttpClient).execute(httpPostArgumentCaptor.capture(), any(ResponseHandler.class)); | ||
|
||
String result = readResult(httpPostArgumentCaptor.getValue().getEntity().getContent()); | ||
assertThat(result, is(expected)); | ||
} | ||
|
||
private static String readResult(InputStream inputStream) throws IOException { | ||
ByteArrayOutputStream result = new ByteArrayOutputStream(); | ||
byte[] buffer = new byte[1024]; | ||
int length; | ||
while ((length = inputStream.read(buffer)) != -1) { | ||
result.write(buffer, 0, length); | ||
} | ||
return result.toString("UTF-8"); | ||
} | ||
} |