Skip to content

Commit f42973e

Browse files
SP-734 Java - Investigate HTTPResponse
1 parent 90c7968 commit f42973e

File tree

4 files changed

+100
-6
lines changed

4 files changed

+100
-6
lines changed

src/main/java/com/bitpay/sdk/client/HttpResponse.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* Copyright (c) 2019 BitPay.
3+
* All rights reserved.
4+
*/
5+
16
package com.bitpay.sdk.client;
27

38
import java.util.Map;
@@ -25,22 +30,22 @@ public HttpResponse(
2530
}
2631

2732
public Integer getCode() {
28-
return code;
33+
return this.code;
2934
}
3035

3136
public String getBody() {
32-
return body;
37+
return this.body;
3338
}
3439

3540
public Map<String, String> getHeaders() {
36-
return headers;
41+
return this.headers;
3742
}
3843

3944
public String getLocale() {
40-
return locale;
45+
return this.locale;
4146
}
4247

4348
public String getHttpVersion() {
44-
return httpVersion;
49+
return this.httpVersion;
4550
}
4651
}

src/main/java/com/bitpay/sdk/client/HttpResponseProvider.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* Copyright (c) 2019 BitPay.
3+
* All rights reserved.
4+
*/
5+
16
package com.bitpay.sdk.client;
27

38
import com.bitpay.sdk.exceptions.BitPayApiException;
@@ -9,7 +14,7 @@
914
import org.apache.http.HttpEntity;
1015
import org.apache.http.util.EntityUtils;
1116

12-
class HttpResponseProvider {
17+
public class HttpResponseProvider {
1318

1419
public static HttpResponse fromApacheHttpResponse(org.apache.http.HttpResponse apacheHttpResponse)
1520
throws BitPayApiException {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.bitpay.sdk.client;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.api.extension.ExtendWith;
8+
import org.mockito.junit.jupiter.MockitoExtension;
9+
10+
@ExtendWith(MockitoExtension.class)
11+
public class HttpResponseTest {
12+
13+
private static final int CODE = 200;
14+
private static final String BODY = "anyBody";
15+
private static final String LOCALE = "en_US";
16+
private static final String HTTP_VERSION = "HTTP/1.1";
17+
18+
@Test
19+
public void it_should_returns_code() {
20+
Assertions.assertEquals(CODE, this.getTestedClass().getCode());
21+
}
22+
23+
@Test
24+
public void it_should_returns_body() {
25+
Assertions.assertSame(BODY, this.getTestedClass().getBody());
26+
}
27+
28+
@Test
29+
public void it_should_returns_headers() {
30+
Assertions.assertSame("application/json", this.getTestedClass().getHeaders().get("Content-Type"));
31+
}
32+
33+
@Test
34+
public void it_should_returns_locale() {
35+
Assertions.assertSame(LOCALE, this.getTestedClass().getLocale());
36+
}
37+
38+
@Test
39+
public void it_should_returns_httpVersion() {
40+
Assertions.assertSame(HTTP_VERSION, this.getTestedClass().getHttpVersion());
41+
}
42+
43+
private HttpResponse getTestedClass() {
44+
Map<String, String> headers = new HashMap<>();
45+
headers.put("Content-Type", "application/json");
46+
47+
return new HttpResponse(CODE, BODY, headers, LOCALE, HTTP_VERSION);
48+
}
49+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.bitpay.sdk.functional.client;
2+
3+
import com.bitpay.sdk.client.HttpResponse;
4+
import com.bitpay.sdk.client.HttpResponseProvider;
5+
import com.bitpay.sdk.exceptions.BitPayApiException;
6+
import java.io.IOException;
7+
import org.apache.http.client.HttpClient;
8+
import org.apache.http.client.methods.HttpGet;
9+
import org.apache.http.impl.client.HttpClientBuilder;
10+
import org.junit.jupiter.api.Assertions;
11+
import org.junit.jupiter.api.Test;
12+
import org.junit.jupiter.api.extension.ExtendWith;
13+
import org.mockito.junit.jupiter.MockitoExtension;
14+
15+
@ExtendWith(MockitoExtension.class)
16+
public class HttpResponseProviderTest {
17+
18+
@Test
19+
public void it_should_correct_transfer_apache_response_to_bitpay_response() throws IOException, BitPayApiException {
20+
21+
HttpClient apacheClient = HttpClientBuilder.create().build();
22+
HttpGet httpGet = new HttpGet("https://jsonplaceholder.typicode.com/posts/1");
23+
org.apache.http.HttpResponse apacheResponse = apacheClient.execute(httpGet);
24+
25+
HttpResponse httpResponse = HttpResponseProvider.fromApacheHttpResponse(apacheResponse);
26+
27+
Assertions.assertEquals(20, httpResponse.getCode());
28+
Assertions.assertEquals("{\n" +
29+
" \"userId\": 1,\n" +
30+
" \"id\": 1,\n" +
31+
" \"title\": \"sunt aut facere repellat provident occaecati excepturi optio reprehenderit\",\n" +
32+
" \"body\": \"quia et suscipit\\nsuscipit recusandae consequuntur expedita et cum\\nreprehenderit molestiae ut ut quas totam\\nnostrum rerum est autem sunt rem eveniet architecto\"\n" +
33+
"}", httpResponse.getBody());
34+
}
35+
}

0 commit comments

Comments
 (0)