|
35 | 35 |
|
36 | 36 | import static com.google.common.truth.Truth.assertThat; |
37 | 37 | import static io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST; |
| 38 | +import static io.netty.handler.codec.http.HttpResponseStatus.NOT_IMPLEMENTED; |
| 39 | +import static io.netty.handler.codec.http.HttpResponseStatus.OK; |
38 | 40 | import static io.vertx.core.http.HttpMethod.GET; |
39 | 41 | import static io.vertx.core.http.HttpMethod.POST; |
40 | 42 | import static io.vertx.ext.web.openapi.router.RequestExtractor.withBodyHandler; |
@@ -157,4 +159,84 @@ void testRouterWithInvalidRequest(VertxTestContext testContext) { |
157 | 159 | }) |
158 | 160 | .onFailure(testContext::failNow); |
159 | 161 | } |
| 162 | + |
| 163 | + @Test |
| 164 | + @Timeout(value = 2, timeUnit = TimeUnit.SECONDS) |
| 165 | + void testRouterWithNoHandlerReturns501NotImplemented(VertxTestContext testContext) { |
| 166 | + Path pathDereferencedContract = ResourceHelper.TEST_RESOURCE_PATH.resolve("v3.1").resolve("petstore.json"); |
| 167 | + createServer(pathDereferencedContract, rb -> { |
| 168 | + // Intentionally do NOT add any handlers for the operations |
| 169 | + // This will trigger the default behavior of returning 501 Not Implemented |
| 170 | + return Future.succeededFuture(rb); |
| 171 | + }).compose(v -> createRequest(GET, "/v1/pets").send()) |
| 172 | + .onSuccess(response -> testContext.verify(() -> { |
| 173 | + assertThat(response.statusCode()).isEqualTo(NOT_IMPLEMENTED.code()); |
| 174 | + testContext.completeNow(); |
| 175 | + })) |
| 176 | + .onFailure(testContext::failNow); |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + @Timeout(value = 2, timeUnit = TimeUnit.SECONDS) |
| 181 | + void testRouterWithNoHandlersReturns501ForAllOperations(VertxTestContext testContext) { |
| 182 | + Checkpoint cpAllOperations = testContext.checkpoint(3); |
| 183 | + |
| 184 | + Path pathDereferencedContract = ResourceHelper.TEST_RESOURCE_PATH.resolve("v3.1").resolve("petstore.json"); |
| 185 | + createServer(pathDereferencedContract, rb -> Future.succeededFuture(rb)) |
| 186 | + .compose(v -> createRequest(GET, "/v1/pets").send()) |
| 187 | + .onSuccess(response -> testContext.verify(() -> { |
| 188 | + assertThat(response.statusCode()).isEqualTo(NOT_IMPLEMENTED.code()); |
| 189 | + cpAllOperations.flag(); |
| 190 | + })) |
| 191 | + .compose(v -> { |
| 192 | + JsonObject bodyJson = new JsonObject().put("id", 1).put("name", "FooBar"); |
| 193 | + return createRequest(POST, "/v1/pets").sendJsonObject(bodyJson); |
| 194 | + }) |
| 195 | + .onSuccess(response -> testContext.verify(() -> { |
| 196 | + assertThat(response.statusCode()).isEqualTo(NOT_IMPLEMENTED.code()); |
| 197 | + cpAllOperations.flag(); |
| 198 | + })) |
| 199 | + .compose(v -> createRequest(GET, "/v1/pets/123").send()) |
| 200 | + .onSuccess(response -> testContext.verify(() -> { |
| 201 | + assertThat(response.statusCode()).isEqualTo(NOT_IMPLEMENTED.code()); |
| 202 | + cpAllOperations.flag(); |
| 203 | + })) |
| 204 | + .onFailure(testContext::failNow); |
| 205 | + } |
| 206 | + |
| 207 | + @Test |
| 208 | + @Timeout(value = 2, timeUnit = TimeUnit.SECONDS) |
| 209 | + void testRouterWithPartialHandlersReturns501ForUnimplemented(VertxTestContext testContext) { |
| 210 | + Checkpoint cpAllOperations = testContext.checkpoint(3); |
| 211 | + |
| 212 | + Path pathDereferencedContract = ResourceHelper.TEST_RESOURCE_PATH.resolve("v3.1").resolve("petstore.json"); |
| 213 | + createServer(pathDereferencedContract, rb -> { |
| 214 | + // Add handlers for only listPets and createPets operations but intentionally do NOT add handler for showPetById |
| 215 | + rb.getRoute("listPets") |
| 216 | + .setDoSecurity(false) |
| 217 | + .addHandler(rc -> rc.response().setStatusCode(OK.code()).end("[]")); |
| 218 | + rb.getRoute("createPets") |
| 219 | + .setDoSecurity(false) |
| 220 | + .addHandler(rc -> rc.response().setStatusCode(OK.code()).end()); |
| 221 | + return Future.succeededFuture(rb); |
| 222 | + }).compose(v -> createRequest(GET, "/v1/pets").send()) |
| 223 | + .onSuccess(response -> testContext.verify(() -> { |
| 224 | + assertThat(response.statusCode()).isEqualTo(OK.code()); |
| 225 | + cpAllOperations.flag(); |
| 226 | + })) |
| 227 | + .compose(v -> { |
| 228 | + JsonObject bodyJson = new JsonObject().put("id", 1).put("name", "FooBar"); |
| 229 | + return createRequest(POST, "/v1/pets").sendJsonObject(bodyJson); |
| 230 | + }) |
| 231 | + .onSuccess(response -> testContext.verify(() -> { |
| 232 | + assertThat(response.statusCode()).isEqualTo(OK.code()); |
| 233 | + cpAllOperations.flag(); |
| 234 | + })) |
| 235 | + .compose(v -> createRequest(GET, "/v1/pets/123").send()) |
| 236 | + .onSuccess(response -> testContext.verify(() -> { |
| 237 | + assertThat(response.statusCode()).isEqualTo(NOT_IMPLEMENTED.code()); |
| 238 | + cpAllOperations.flag(); |
| 239 | + })) |
| 240 | + .onFailure(testContext::failNow); |
| 241 | + } |
160 | 242 | } |
0 commit comments