|
| 1 | +package com.baeldung.unirest; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNotNull; |
| 5 | + |
| 6 | +import java.io.File; |
| 7 | +import java.io.FileInputStream; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.InputStream; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.concurrent.ExecutionException; |
| 13 | +import java.util.concurrent.Future; |
| 14 | + |
| 15 | +import org.apache.http.entity.ContentType; |
| 16 | +import org.junit.AfterClass; |
| 17 | +import org.junit.BeforeClass; |
| 18 | +import org.junit.Test; |
| 19 | + |
| 20 | +import com.baeldung.unirest.Article; |
| 21 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 22 | +import com.mashape.unirest.http.HttpResponse; |
| 23 | +import com.mashape.unirest.http.JsonNode; |
| 24 | +import com.mashape.unirest.http.ObjectMapper; |
| 25 | +import com.mashape.unirest.http.Unirest; |
| 26 | +import com.mashape.unirest.http.async.Callback; |
| 27 | +import com.mashape.unirest.http.exceptions.UnirestException; |
| 28 | + |
| 29 | +public class HttpClientTest { |
| 30 | + |
| 31 | + @BeforeClass |
| 32 | + public static void setup() { |
| 33 | + // Unirest.setProxy(new HttpHost("localhost", 8080)); |
| 34 | + Unirest.setTimeouts(20000, 15000); |
| 35 | + Unirest.setDefaultHeader("X-app-name", "baeldung-unirest"); |
| 36 | + Unirest.setDefaultHeader("X-request-id", "100004f00ab5"); |
| 37 | + Unirest.setConcurrency(20, 5); |
| 38 | + Unirest.setObjectMapper(new ObjectMapper() { |
| 39 | + com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper(); |
| 40 | + |
| 41 | + public String writeValue(Object value) { |
| 42 | + try { |
| 43 | + return mapper.writeValueAsString(value); |
| 44 | + } catch (JsonProcessingException e) { |
| 45 | + throw new RuntimeException(e); |
| 46 | + } |
| 47 | + |
| 48 | + } |
| 49 | + |
| 50 | + public <T> T readValue(String value, Class<T> valueType) { |
| 51 | + |
| 52 | + try { |
| 53 | + return mapper.readValue(value, valueType); |
| 54 | + } catch (Exception e) { |
| 55 | + throw new RuntimeException(e); |
| 56 | + } |
| 57 | + |
| 58 | + } |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + @AfterClass |
| 63 | + public static void tearDown() throws IOException { |
| 64 | + Unirest.clearDefaultHeaders(); |
| 65 | + Unirest.shutdown(); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void shouldReturnStatusOkay() throws UnirestException { |
| 70 | + HttpResponse<JsonNode> jsonResponse = Unirest.get("http://www.mocky.io/v2/5a9ce37b3100004f00ab5154") |
| 71 | + .header("accept", "application/json") |
| 72 | + .queryString("apiKey", "123") |
| 73 | + .asJson(); |
| 74 | + assertNotNull(jsonResponse.getBody()); |
| 75 | + assertEquals(200, jsonResponse.getStatus()); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void shouldReturnStatusAccepted() throws UnirestException { |
| 80 | + |
| 81 | + Map<String, String> headers = new HashMap<String, String>(); |
| 82 | + headers.put("accept", "application/json"); |
| 83 | + headers.put("Authorization", "Bearer 5a9ce37b3100004f00ab5154"); |
| 84 | + |
| 85 | + Map<String, Object> fields = new HashMap<String, Object>(); |
| 86 | + fields.put("name", "Sam Baeldung"); |
| 87 | + fields.put("id", "PSP123"); |
| 88 | + |
| 89 | + HttpResponse<JsonNode> jsonResponse = Unirest.put("http://www.mocky.io/v2/5a9ce7853100002a00ab515e") |
| 90 | + .headers(headers) |
| 91 | + .fields(fields) |
| 92 | + .asJson(); |
| 93 | + assertNotNull(jsonResponse.getBody()); |
| 94 | + assertEquals(202, jsonResponse.getStatus()); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void givenRequestBodyWhenCreatedThenCorrect() throws UnirestException { |
| 99 | + |
| 100 | + HttpResponse<JsonNode> jsonResponse = Unirest.post("http://www.mocky.io/v2/5a9ce7663100006800ab515d") |
| 101 | + .body("{\"name\":\"Sam Baeldung\", \"city\":\"viena\"}") |
| 102 | + .asJson(); |
| 103 | + assertEquals(201, jsonResponse.getStatus()); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void whenAysncRequestShouldReturnOk() throws InterruptedException, ExecutionException { |
| 108 | + Future<HttpResponse<JsonNode>> future = Unirest.post("http://www.mocky.io/v2/5a9ce37b3100004f00ab5154?mocky-delay=10000ms") |
| 109 | + .header("accept", "application/json") |
| 110 | + .asJsonAsync(new Callback<JsonNode>() { |
| 111 | + |
| 112 | + public void failed(UnirestException e) { |
| 113 | + // Do something if the request failed |
| 114 | + } |
| 115 | + |
| 116 | + public void completed(HttpResponse<JsonNode> response) { |
| 117 | + // Do something if the request is successful |
| 118 | + } |
| 119 | + |
| 120 | + public void cancelled() { |
| 121 | + // Do something if the request is cancelled |
| 122 | + } |
| 123 | + |
| 124 | + }); |
| 125 | + assertEquals(200, future.get() |
| 126 | + .getStatus()); |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void givenArticleWhenCreatedThenCorrect() throws UnirestException { |
| 132 | + Article article = new Article("ID1213", "Guide to Rest", "baeldung"); |
| 133 | + HttpResponse<JsonNode> jsonResponse = Unirest.post("http://www.mocky.io/v2/5a9ce7663100006800ab515d") |
| 134 | + .body(article) |
| 135 | + .asJson(); |
| 136 | + assertEquals(201, jsonResponse.getStatus()); |
| 137 | + } |
| 138 | + |
| 139 | + // @Test |
| 140 | + public void givenFileWhenUploadedThenCorrect() throws UnirestException { |
| 141 | + |
| 142 | + HttpResponse<JsonNode> jsonResponse = Unirest.post("http://www.mocky.io/v2/5a9ce7663100006800ab515d") |
| 143 | + .field("file", new File("/path/to/file")) |
| 144 | + .asJson(); |
| 145 | + assertEquals(201, jsonResponse.getStatus()); |
| 146 | + } |
| 147 | + |
| 148 | + // @Test |
| 149 | + public void givenByteStreamWhenUploadedThenCorrect() throws IOException, UnirestException { |
| 150 | + try (InputStream inputStream = new FileInputStream(new File("/path/to/file/artcile.txt"))) { |
| 151 | + byte[] bytes = new byte[inputStream.available()]; |
| 152 | + inputStream.read(bytes); |
| 153 | + HttpResponse<JsonNode> jsonResponse = Unirest.post("http://www.mocky.io/v2/5a9ce7663100006800ab515d") |
| 154 | + .field("file", bytes, "article.txt") |
| 155 | + .asJson(); |
| 156 | + assertEquals(201, jsonResponse.getStatus()); |
| 157 | + } |
| 158 | + |
| 159 | + } |
| 160 | + |
| 161 | + // @Test |
| 162 | + public void givenInputStreamWhenUploadedThenCorrect() throws UnirestException, IOException { |
| 163 | + try (InputStream inputStream = new FileInputStream(new File("/path/to/file/artcile.txt"))) { |
| 164 | + |
| 165 | + HttpResponse<JsonNode> jsonResponse = Unirest.post("http://www.mocky.io/v2/5a9ce7663100006800ab515d") |
| 166 | + .field("file", inputStream, ContentType.APPLICATION_OCTET_STREAM, "article.txt") |
| 167 | + .asJson(); |
| 168 | + assertEquals(201, jsonResponse.getStatus()); |
| 169 | + |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments