JAVA-627: Moved 2 articles to libraries-http-2
This commit is contained in:
parent
1ec2d9a4ed
commit
c990a29bf7
10
libraries-http-2/README.md
Normal file
10
libraries-http-2/README.md
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
## HTTP
|
||||||
|
|
||||||
|
This module contains articles about HTTP libraries.
|
||||||
|
|
||||||
|
### Relevant Articles:
|
||||||
|
|
||||||
|
- [Jetty ReactiveStreams HTTP Client](https://www.baeldung.com/jetty-reactivestreams-http-client)
|
||||||
|
- [Decode an OkHttp JSON Response](https://www.baeldung.com/okhttp-json-response)
|
||||||
|
- More articles [[<-- prev]](/libraries-http)
|
||||||
|
|
47
libraries-http-2/pom.xml
Normal file
47
libraries-http-2/pom.xml
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>libraries-http-2</artifactId>
|
||||||
|
<name>libraries-http-2</name>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-modules</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- Dependencies for response decoder with okhttp -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.squareup.okhttp3</groupId>
|
||||||
|
<artifactId>okhttp</artifactId>
|
||||||
|
<version>${okhttp.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
<version>${jackson.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.code.gson</groupId>
|
||||||
|
<artifactId>gson</artifactId>
|
||||||
|
<version>${gson.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.squareup.okhttp3</groupId>
|
||||||
|
<artifactId>mockwebserver</artifactId>
|
||||||
|
<version>${mockwebserver.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<okhttp.version>3.14.2</okhttp.version>
|
||||||
|
<gson.version>2.8.5</gson.version>
|
||||||
|
<mockwebserver.version>3.14.2</mockwebserver.version>
|
||||||
|
<jackson.version>2.9.8</jackson.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,102 @@
|
|||||||
|
package com.baeldung.okhttp;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.ResponseBody;
|
||||||
|
import okhttp3.mockwebserver.MockResponse;
|
||||||
|
import okhttp3.mockwebserver.MockWebServer;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Rule;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.ExpectedException;
|
||||||
|
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
|
||||||
|
public class ResponseDecoderUnitTest {
|
||||||
|
|
||||||
|
@Rule public ExpectedException exceptionRule = ExpectedException.none();
|
||||||
|
|
||||||
|
@Rule public MockWebServer server = new MockWebServer();
|
||||||
|
|
||||||
|
SimpleEntity sampleResponse;
|
||||||
|
|
||||||
|
MockResponse mockResponse;
|
||||||
|
|
||||||
|
OkHttpClient client;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
sampleResponse = new SimpleEntity("Baeldung");
|
||||||
|
client = new OkHttpClient.Builder().build();
|
||||||
|
mockResponse = new MockResponse()
|
||||||
|
.setResponseCode(200)
|
||||||
|
.setHeader("Content-Type", "application/json")
|
||||||
|
.setBody(new Gson().toJson(sampleResponse));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenJacksonDecoder_whenGetStringOfResponse_thenExpectSimpleEntity() throws Exception {
|
||||||
|
server.enqueue(mockResponse);
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(server.url(""))
|
||||||
|
.build();
|
||||||
|
ResponseBody responseBody = client
|
||||||
|
.newCall(request)
|
||||||
|
.execute()
|
||||||
|
.body();
|
||||||
|
|
||||||
|
Assert.assertNotNull(responseBody);
|
||||||
|
Assert.assertNotEquals(0, responseBody.contentLength());
|
||||||
|
|
||||||
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
SimpleEntity entity = objectMapper.readValue(responseBody.string(), SimpleEntity.class);
|
||||||
|
|
||||||
|
Assert.assertNotNull(entity);
|
||||||
|
Assert.assertEquals(sampleResponse.getName(), entity.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenGsonDecoder_whenGetByteStreamOfResponse_thenExpectSimpleEntity() throws Exception {
|
||||||
|
server.enqueue(mockResponse);
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(server.url(""))
|
||||||
|
.build();
|
||||||
|
ResponseBody responseBody = client
|
||||||
|
.newCall(request)
|
||||||
|
.execute()
|
||||||
|
.body();
|
||||||
|
|
||||||
|
Assert.assertNotNull(responseBody);
|
||||||
|
Assert.assertNotEquals(0, responseBody.contentLength());
|
||||||
|
|
||||||
|
Gson gson = new Gson();
|
||||||
|
SimpleEntity entity = gson.fromJson(new InputStreamReader(responseBody.byteStream()), SimpleEntity.class);
|
||||||
|
|
||||||
|
Assert.assertNotNull(entity);
|
||||||
|
Assert.assertEquals(sampleResponse.getName(), entity.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenGsonDecoder_whenGetStringOfResponse_thenExpectSimpleEntity() throws Exception {
|
||||||
|
server.enqueue(mockResponse);
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(server.url(""))
|
||||||
|
.build();
|
||||||
|
ResponseBody responseBody = client
|
||||||
|
.newCall(request)
|
||||||
|
.execute()
|
||||||
|
.body();
|
||||||
|
|
||||||
|
Assert.assertNotNull(responseBody);
|
||||||
|
|
||||||
|
Gson gson = new Gson();
|
||||||
|
SimpleEntity entity = gson.fromJson(responseBody.string(), SimpleEntity.class);
|
||||||
|
|
||||||
|
Assert.assertNotNull(entity);
|
||||||
|
Assert.assertEquals(sampleResponse.getName(), entity.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.baeldung.okhttp;
|
||||||
|
|
||||||
|
public class SimpleEntity {
|
||||||
|
protected String name;
|
||||||
|
|
||||||
|
public SimpleEntity(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
//no-arg constructor, getters and setters here
|
||||||
|
|
||||||
|
public SimpleEntity() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user