|
| 1 | +package io.vertx.ext.web.client.tests; |
| 2 | + |
| 3 | +import io.vertx.core.*; |
| 4 | +import io.vertx.core.buffer.Buffer; |
| 5 | +import io.vertx.core.http.*; |
| 6 | +import io.vertx.core.json.JsonObject; |
| 7 | +import io.vertx.ext.web.client.*; |
| 8 | +import io.vertx.test.core.TestUtils; |
| 9 | +import org.assertj.core.api.Assertions; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | + |
| 12 | +import java.util.*; |
| 13 | +import java.util.function.Consumer; |
| 14 | + |
| 15 | +import static org.junit.jupiter.api.Assertions.*; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author <a href="mailto:julien@julienviet.com">Julien Viet</a> |
| 19 | + */ |
| 20 | +public class HttpResponseExpectationsTest extends WebClientTestBase { |
| 21 | + |
| 22 | + @Test |
| 23 | + public void testExpectFail_2() throws Exception { |
| 24 | + testExpectation(true, |
| 25 | + value -> false, |
| 26 | + HttpServerResponse::end); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void testExpectPass_2() throws Exception { |
| 31 | + testExpectation(false, |
| 32 | + value -> true, |
| 33 | + HttpServerResponse::end); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testExpectStatusFail_2() throws Exception { |
| 38 | + testExpectation(true, |
| 39 | + HttpResponseExpectation.status(200), |
| 40 | + resp -> resp.setStatusCode(201).end()); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testExpectStatusPass_2() throws Exception { |
| 45 | + testExpectation(false, |
| 46 | + HttpResponseExpectation.status(200), |
| 47 | + resp -> resp.setStatusCode(200).end()); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testExpectStatusRangeFail_2() throws Exception { |
| 52 | + testExpectation(true, |
| 53 | + HttpResponseExpectation.SC_SUCCESS, |
| 54 | + resp -> resp.setStatusCode(500).end()); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testExpectStatusRangePass1_2() throws Exception { |
| 59 | + testExpectation(false, |
| 60 | + HttpResponseExpectation.SC_SUCCESS, |
| 61 | + resp -> resp.setStatusCode(200).end()); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testExpectStatusRangePass2_2() throws Exception { |
| 66 | + testExpectation(false, |
| 67 | + HttpResponseExpectation.SC_SUCCESS, |
| 68 | + resp -> resp.setStatusCode(299).end()); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testExpectContentTypeFail_2() throws Exception { |
| 73 | + testExpectation(true, |
| 74 | + HttpResponseExpectation.JSON, |
| 75 | + HttpServerResponse::end); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testExpectOneOfContentTypesFail_2() throws Exception { |
| 80 | + testExpectation(true, |
| 81 | + HttpResponseExpectation.contentType(Arrays.asList("text/plain", "text/csv")), |
| 82 | + httpServerResponse -> httpServerResponse.putHeader(HttpHeaders.CONTENT_TYPE, HttpHeaders.TEXT_HTML).end()); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testExpectContentTypePass_2() throws Exception { |
| 87 | + testExpectation(false, |
| 88 | + HttpResponseExpectation.JSON, |
| 89 | + resp -> resp.putHeader("content-type", "application/JSON").end()); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testExpectContentTypeWithEncodingPass_2() throws Exception { |
| 94 | + testExpectation(false, |
| 95 | + HttpResponseExpectation.JSON, |
| 96 | + resp -> resp.putHeader("content-type", "application/JSON;charset=UTF-8").end()); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testExpectOneOfContentTypesPass_2() throws Exception { |
| 101 | + testExpectation(false, |
| 102 | + HttpResponseExpectation.contentType(Arrays.asList("text/plain", "text/HTML")), |
| 103 | + httpServerResponse -> httpServerResponse.putHeader(HttpHeaders.CONTENT_TYPE, HttpHeaders.TEXT_HTML).end()); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testExpectCustomException_2() throws Exception { |
| 108 | + Expectation<HttpResponseHead> expectation = ((Expectation<HttpResponseHead>) value -> false) |
| 109 | + .wrappingFailure((head, err) -> new CustomException("boom")); |
| 110 | + testExpectation(true, expectation, HttpServerResponse::end, cause -> { |
| 111 | + Assertions.assertThat(cause).isInstanceOf(CustomException.class); |
| 112 | + CustomException customException = (CustomException) cause; |
| 113 | + assertEquals("boom", customException.getMessage()); |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void testExpectCustomExceptionWithResponseBody_2() throws Exception { |
| 119 | + UUID uuid = UUID.randomUUID(); |
| 120 | + Expectation<HttpResponseHead> expectation = HttpResponseExpectation.SC_SUCCESS.wrappingFailure((head, err) -> { |
| 121 | + JsonObject body = ((HttpResponse<?>) head).bodyAsJsonObject(); |
| 122 | + return new CustomException(UUID.fromString(body.getString("tag")), body.getString("message")); |
| 123 | + }); |
| 124 | + testExpectation(true, expectation, httpServerResponse -> { |
| 125 | + httpServerResponse |
| 126 | + .setStatusCode(400) |
| 127 | + .end(new JsonObject().put("tag", uuid.toString()).put("message", "tilt").toBuffer()); |
| 128 | + }, cause -> { |
| 129 | + Assertions.assertThat(cause).isInstanceOf(CustomException.class); |
| 130 | + CustomException customException = (CustomException) cause; |
| 131 | + assertEquals("tilt", customException.getMessage()); |
| 132 | + assertEquals(uuid, customException.tag); |
| 133 | + }); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + public void testExpectCustomExceptionWithStatusCode_2() throws Exception { |
| 138 | + UUID uuid = UUID.randomUUID(); |
| 139 | + int statusCode = 400; |
| 140 | + |
| 141 | + Expectation<HttpResponseHead> expectation = HttpResponseExpectation.SC_SUCCESS |
| 142 | + .wrappingFailure((head, err) -> new CustomException(uuid, String.valueOf(head.statusCode()))); |
| 143 | + |
| 144 | + testExpectation(true, expectation, httpServerResponse -> { |
| 145 | + httpServerResponse |
| 146 | + .setStatusCode(statusCode) |
| 147 | + .end(TestUtils.randomBuffer(2048)); |
| 148 | + }, cause -> { |
| 149 | + Assertions.assertThat(cause).isInstanceOf(CustomException.class); |
| 150 | + CustomException customException = (CustomException) cause; |
| 151 | + assertEquals(String.valueOf(statusCode), customException.getMessage()); |
| 152 | + assertEquals(uuid, customException.tag); |
| 153 | + }); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void testExpectFunctionThrowsException_2() throws Exception { |
| 158 | + Expectation<HttpResponseHead> expectation = value -> { |
| 159 | + throw new IndexOutOfBoundsException("boom"); |
| 160 | + }; |
| 161 | + |
| 162 | + testExpectation(true, expectation, HttpServerResponse::end, cause -> { |
| 163 | + Assertions.assertThat(cause).isInstanceOf(IndexOutOfBoundsException.class); |
| 164 | + }); |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + public void testErrorConverterThrowsException_2() throws Exception { |
| 169 | + Expectation<HttpResponseHead> expectation = ((Expectation<HttpResponseHead>) value -> false).wrappingFailure((head, err) -> { |
| 170 | + throw new IndexOutOfBoundsException(); |
| 171 | + }); |
| 172 | + |
| 173 | + testExpectation(true, expectation, HttpServerResponse::end, cause -> { |
| 174 | + Assertions.assertThat(cause).isInstanceOf(IndexOutOfBoundsException.class); |
| 175 | + }); |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + public void testErrorConverterReturnsNull_2() throws Exception { |
| 180 | + Expectation<HttpResponseHead> expectation = ((Expectation<HttpResponseHead>) value -> false) |
| 181 | + .wrappingFailure((head, err) -> null); |
| 182 | + |
| 183 | + testExpectation(true, expectation, HttpServerResponse::end, cause -> { |
| 184 | + Assertions.assertThat(cause).isNotInstanceOf(NullPointerException.class); |
| 185 | + }); |
| 186 | + } |
| 187 | + |
| 188 | + private void testExpectation(boolean shouldFail, |
| 189 | + Expectation<HttpResponseHead> expectation, |
| 190 | + Consumer<HttpServerResponse> bilto) throws Exception { |
| 191 | + testExpectation(shouldFail, expectation, bilto, ignore -> {}); |
| 192 | + } |
| 193 | + |
| 194 | + private void testExpectation(boolean shouldFail, |
| 195 | + Expectation<HttpResponseHead> expectation, |
| 196 | + Consumer<HttpServerResponse> bilto, |
| 197 | + Consumer<Throwable> failureTest) throws Exception { |
| 198 | + server.requestHandler(request -> bilto.accept(request.response())); |
| 199 | + startServer(); |
| 200 | + HttpRequest<Buffer> request = webClient |
| 201 | + .get("/test"); |
| 202 | + if (shouldFail) { |
| 203 | + Assertions |
| 204 | + .assertThatThrownBy(() -> request.send().expecting(expectation).await()) |
| 205 | + .satisfies(failureTest); |
| 206 | + } else { |
| 207 | + request.send().expecting(expectation).await(); |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + private static class CustomException extends Exception { |
| 212 | + |
| 213 | + UUID tag; |
| 214 | + |
| 215 | + CustomException(String message) { |
| 216 | + super(message); |
| 217 | + } |
| 218 | + |
| 219 | + CustomException(UUID tag, String message) { |
| 220 | + super(message); |
| 221 | + this.tag = tag; |
| 222 | + } |
| 223 | + } |
| 224 | +} |
0 commit comments