mirror of
https://github.com/square/okhttp.git
synced 2025-07-31 05:04:26 +03:00
Use AssertJ in tests (#4713)
This commit is contained in:
committed by
Jesse Wilson
parent
a217a390ee
commit
6c4855a7c7
@ -46,6 +46,11 @@
|
||||
<artifactId>conscrypt-openjdk-uber</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -39,8 +39,7 @@ import org.junit.Test;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public class DnsOverHttpsTest {
|
||||
@ -62,12 +61,12 @@ public class DnsOverHttpsTest {
|
||||
|
||||
List<InetAddress> result = dns.lookup("google.com");
|
||||
|
||||
assertEquals(singletonList(address("157.240.1.18")), result);
|
||||
assertThat(result).isEqualTo(singletonList(address("157.240.1.18")));
|
||||
|
||||
RecordedRequest recordedRequest = server.takeRequest();
|
||||
assertEquals("GET", recordedRequest.getMethod());
|
||||
assertEquals("/lookup?ct&dns=AAABAAABAAAAAAAABmdvb2dsZQNjb20AAAEAAQ",
|
||||
recordedRequest.getPath());
|
||||
assertThat(recordedRequest.getMethod()).isEqualTo("GET");
|
||||
assertThat(recordedRequest.getPath()).isEqualTo(
|
||||
"/lookup?ct&dns=AAABAAABAAAAAAAABmdvb2dsZQNjb20AAAEAAQ");
|
||||
}
|
||||
|
||||
@Test public void getIpv6() throws Exception {
|
||||
@ -84,20 +83,20 @@ public class DnsOverHttpsTest {
|
||||
|
||||
List<InetAddress> result = dns.lookup("google.com");
|
||||
|
||||
assertEquals(2, result.size());
|
||||
assertTrue(result.contains(address("157.240.1.18")));
|
||||
assertTrue(result.contains(address("2a03:2880:f029:11:face:b00c:0:2")));
|
||||
assertThat(result.size()).isEqualTo(2);
|
||||
assertThat(result.contains(address("157.240.1.18"))).isTrue();
|
||||
assertThat(result.contains(address("2a03:2880:f029:11:face:b00c:0:2"))).isTrue();
|
||||
|
||||
RecordedRequest request1 = server.takeRequest();
|
||||
assertEquals("GET", request1.getMethod());
|
||||
assertThat(request1.getMethod()).isEqualTo("GET");
|
||||
|
||||
RecordedRequest request2 = server.takeRequest();
|
||||
assertEquals("GET", request2.getMethod());
|
||||
assertThat(request2.getMethod()).isEqualTo("GET");
|
||||
|
||||
assertEquals(new HashSet<>(
|
||||
assertThat(new LinkedHashSet<>(Arrays.asList(request1.getPath(), request2.getPath()))).isEqualTo(
|
||||
new HashSet<>(
|
||||
Arrays.asList("/lookup?ct&dns=AAABAAABAAAAAAAABmdvb2dsZQNjb20AAAEAAQ",
|
||||
"/lookup?ct&dns=AAABAAABAAAAAAAABmdvb2dsZQNjb20AABwAAQ")),
|
||||
new LinkedHashSet<>(Arrays.asList(request1.getPath(), request2.getPath())));
|
||||
"/lookup?ct&dns=AAABAAABAAAAAAAABmdvb2dsZQNjb20AABwAAQ")));
|
||||
}
|
||||
|
||||
@Test public void failure() throws Exception {
|
||||
@ -111,13 +110,13 @@ public class DnsOverHttpsTest {
|
||||
fail();
|
||||
} catch (UnknownHostException uhe) {
|
||||
uhe.printStackTrace();
|
||||
assertEquals("google.com: NXDOMAIN", uhe.getMessage());
|
||||
assertThat(uhe.getMessage()).isEqualTo("google.com: NXDOMAIN");
|
||||
}
|
||||
|
||||
RecordedRequest recordedRequest = server.takeRequest();
|
||||
assertEquals("GET", recordedRequest.getMethod());
|
||||
assertEquals("/lookup?ct&dns=AAABAAABAAAAAAAABmdvb2dsZQNjb20AAAEAAQ",
|
||||
recordedRequest.getPath());
|
||||
assertThat(recordedRequest.getMethod()).isEqualTo("GET");
|
||||
assertThat(recordedRequest.getPath()).isEqualTo(
|
||||
"/lookup?ct&dns=AAABAAABAAAAAAAABmdvb2dsZQNjb20AAAEAAQ");
|
||||
}
|
||||
|
||||
@Test public void failOnExcessiveResponse() {
|
||||
@ -129,10 +128,11 @@ public class DnsOverHttpsTest {
|
||||
dns.lookup("google.com");
|
||||
fail();
|
||||
} catch (IOException ioe) {
|
||||
assertEquals("google.com", ioe.getMessage());
|
||||
assertThat(ioe.getMessage()).isEqualTo("google.com");
|
||||
Throwable cause = ioe.getCause();
|
||||
assertTrue(cause instanceof IOException);
|
||||
assertEquals("response size exceeds limit (65536 bytes): 65537 bytes", cause.getMessage());
|
||||
assertThat(cause instanceof IOException).isTrue();
|
||||
assertThat(cause.getMessage()).isEqualTo(
|
||||
"response size exceeds limit (65536 bytes): 65537 bytes");
|
||||
}
|
||||
}
|
||||
|
||||
@ -143,9 +143,10 @@ public class DnsOverHttpsTest {
|
||||
dns.lookup("google.com");
|
||||
fail();
|
||||
} catch (IOException ioe) {
|
||||
assertEquals("google.com", ioe.getMessage());
|
||||
assertThat(ioe.getMessage()).isEqualTo("google.com");
|
||||
Throwable cause = ioe.getCause();
|
||||
assertTrue(cause instanceof RuntimeException);
|
||||
boolean condition = cause instanceof RuntimeException;
|
||||
assertThat(condition).isTrue();
|
||||
}
|
||||
}
|
||||
|
||||
@ -170,15 +171,15 @@ public class DnsOverHttpsTest {
|
||||
|
||||
List<InetAddress> result = cachedDns.lookup("google.com");
|
||||
|
||||
assertEquals(singletonList(address("157.240.1.18")), result);
|
||||
assertThat(result).containsExactly(address("157.240.1.18"));
|
||||
|
||||
RecordedRequest recordedRequest = server.takeRequest();
|
||||
assertEquals("GET", recordedRequest.getMethod());
|
||||
assertEquals("/lookup?ct&dns=AAABAAABAAAAAAAABmdvb2dsZQNjb20AAAEAAQ",
|
||||
recordedRequest.getPath());
|
||||
assertThat(recordedRequest.getMethod()).isEqualTo("GET");
|
||||
assertThat(recordedRequest.getPath()).isEqualTo(
|
||||
"/lookup?ct&dns=AAABAAABAAAAAAAABmdvb2dsZQNjb20AAAEAAQ");
|
||||
|
||||
result = cachedDns.lookup("google.com");
|
||||
assertEquals(singletonList(address("157.240.1.18")), result);
|
||||
assertThat(result).isEqualTo(singletonList(address("157.240.1.18")));
|
||||
}
|
||||
|
||||
private MockResponse dnsResponse(String s) {
|
||||
|
@ -17,21 +17,20 @@ package okhttp3.dnsoverhttps;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import okio.ByteString;
|
||||
import org.junit.Test;
|
||||
|
||||
import static okhttp3.dnsoverhttps.DnsRecordCodec.TYPE_A;
|
||||
import static okhttp3.dnsoverhttps.DnsRecordCodec.TYPE_AAAA;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public class DnsRecordCodecTest {
|
||||
@Test public void testGoogleDotComEncoding() {
|
||||
String encoded = encodeQuery("google.com", TYPE_A);
|
||||
|
||||
assertEquals("AAABAAABAAAAAAAABmdvb2dsZQNjb20AAAEAAQ", encoded);
|
||||
assertThat(encoded).isEqualTo("AAABAAABAAAAAAAABmdvb2dsZQNjb20AAAEAAQ");
|
||||
}
|
||||
|
||||
private String encodeQuery(String host, int type) {
|
||||
@ -41,30 +40,28 @@ public class DnsRecordCodecTest {
|
||||
@Test public void testGoogleDotComEncodingWithIPv6() {
|
||||
String encoded = encodeQuery("google.com", TYPE_AAAA);
|
||||
|
||||
assertEquals("AAABAAABAAAAAAAABmdvb2dsZQNjb20AABwAAQ", encoded);
|
||||
assertThat(encoded).isEqualTo("AAABAAABAAAAAAAABmdvb2dsZQNjb20AABwAAQ");
|
||||
}
|
||||
|
||||
@Test public void testGoogleDotComDecodingFromCloudflare() throws Exception {
|
||||
List<InetAddress> encoded = DnsRecordCodec.decodeAnswers("test.com", ByteString.decodeHex(
|
||||
"00008180000100010000000006676f6f676c6503636f6d0000010001c00c00010001000000430004d83ad54e"));
|
||||
|
||||
assertEquals(Collections.singletonList(InetAddress.getByName("216.58.213.78")), encoded);
|
||||
assertThat(encoded).containsExactly(InetAddress.getByName("216.58.213.78"));
|
||||
}
|
||||
|
||||
@Test public void testGoogleDotComDecodingFromGoogle() throws Exception {
|
||||
List<InetAddress> decoded = DnsRecordCodec.decodeAnswers("test.com", ByteString.decodeHex(
|
||||
"0000818000010003000000000567726170680866616365626f6f6b03636f6d0000010001c00c0005000100000a6d000603617069c012c0300005000100000cde000c04737461720463313072c012c042000100010000003b00049df00112"));
|
||||
|
||||
assertEquals(Collections.singletonList(InetAddress.getByName("157.240.1.18")), decoded);
|
||||
assertThat(decoded).containsExactly(InetAddress.getByName("157.240.1.18"));
|
||||
}
|
||||
|
||||
@Test public void testGoogleDotComDecodingFromGoogleIPv6() throws Exception {
|
||||
List<InetAddress> decoded = DnsRecordCodec.decodeAnswers("test.com", ByteString.decodeHex(
|
||||
"0000818000010003000000000567726170680866616365626f6f6b03636f6d00001c0001c00c0005000100000a1b000603617069c012c0300005000100000b1f000c04737461720463313072c012c042001c00010000003b00102a032880f0290011faceb00c00000002"));
|
||||
|
||||
assertEquals(
|
||||
Collections.singletonList(InetAddress.getByName("2a03:2880:f029:11:face:b00c:0:2")),
|
||||
decoded);
|
||||
assertThat(decoded).containsExactly(InetAddress.getByName("2a03:2880:f029:11:face:b00c:0:2"));
|
||||
}
|
||||
|
||||
@Test public void testGoogleDotComDecodingNxdomainFailure() throws Exception {
|
||||
@ -73,7 +70,7 @@ public class DnsRecordCodecTest {
|
||||
"0000818300010000000100000e7364666c6b686673646c6b6a64660265650000010001c01b00060001000007070038026e7303746c64c01b0a686f73746d61737465720d6565737469696e7465726e6574c01b5adb12c100000e10000003840012750000000e10"));
|
||||
fail();
|
||||
} catch (UnknownHostException uhe) {
|
||||
assertEquals("sdflkhfsdlkjdf.ee: NXDOMAIN", uhe.getMessage());
|
||||
assertThat(uhe.getMessage()).isEqualTo("sdflkhfsdlkjdf.ee: NXDOMAIN");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user