### Question:
DiagramHandler { public Handler<RoutingContext> createPost(String serviceName) { return routingContext -> { try { FileFormat fileFormat; String diagramSource; MIMEHeader contentType = routingContext.parsedHeaders().contentType(); if (contentType != null && contentType.value() != null && contentType.value().equals("application/json")) { String bodyAsString = routingContext.getBodyAsString(); if (bodyAsString == null || bodyAsString.trim().isEmpty()) { routingContext.fail(new BadRequestException("Request body must not be empty.")); return; } JsonObject jsonBody = new JsonObject(bodyAsString); diagramSource = jsonBody.getString("diagram_source"); if (diagramSource == null || diagramSource.trim().isEmpty()) { routingContext.fail(new BadRequestException("Field diagram_source must not be empty.")); return; } fileFormat = getOutputFileFormatFromJsonRequest(serviceName, routingContext, jsonBody); } else { diagramSource = routingContext.getBodyAsString(); if (diagramSource == null || diagramSource.trim().isEmpty()) { routingContext.fail(new BadRequestException("Request body must not be empty.")); return; } fileFormat = getOutputFileFormatFromTextRequest(serviceName, routingContext); } convert(routingContext, diagramSource, serviceName, fileFormat); } catch (UnsupportedFormatException | UnsupportedMimeTypeException | UndefinedOutputFormatException e) { routingContext.fail(e); } }; } DiagramHandler(DiagramService service); Handler<RoutingContext> createRequestReceived(String serviceName); Handler<RoutingContext> createGet(String serviceName); Handler<RoutingContext> createPost(String serviceName); FileFormat validate(String serviceName, String outputFormat); void convert(RoutingContext routingContext, String sourceDecoded, String serviceName, FileFormat fileFormat);  }### Answer:
@Test void should_reject_request_with_unsupported_format_infer_from_accept() { DiagramHandler diagramHandler = new DiagramHandler(mockDiagramService(Lists.newArrayList(FileFormat.SVG), null)); MockDiagramRequest mockDiagramRequest = new MockDiagramRequest(); mockDiagramRequest.setMethod(HttpMethod.POST); mockDiagramRequest.addAccept("image/png"); mockDiagramRequest.setContentType("text/plain"); mockDiagramRequest.setBodyAsString("Bob -> Alice : hello"); RoutingContext routingContext = mockDiagramRequest.getRoutingContext(); diagramHandler.createPost("plantuml").handle(routingContext); verify(routingContext).fail(any(UnsupportedMimeTypeException.class)); }
@Test void should_reject_request_with_invalid_accept() { DiagramHandler diagramHandler = new DiagramHandler(mockDiagramService(Lists.newArrayList(FileFormat.SVG), null)); MockDiagramRequest mockDiagramRequest = new MockDiagramRequest(); mockDiagramRequest.setMethod(HttpMethod.POST); mockDiagramRequest.addAccept("text/html"); mockDiagramRequest.setContentType("text/plain"); mockDiagramRequest.setBodyAsString("Bob -> Alice : hello"); RoutingContext routingContext = mockDiagramRequest.getRoutingContext(); diagramHandler.createPost("plantuml").handle(routingContext); verify(routingContext).fail(any(UnsupportedMimeTypeException.class)); }
@Test void should_reject_request_with_unsupported_format() { DiagramHandler diagramHandler = new DiagramHandler(mockDiagramService(Lists.newArrayList(FileFormat.SVG), null)); MockDiagramRequest mockDiagramRequest = new MockDiagramRequest(); mockDiagramRequest.setMethod(HttpMethod.POST); mockDiagramRequest.addParam("output_format", "png"); mockDiagramRequest.setContentType("text/plain"); mockDiagramRequest.setBodyAsString("Bob -> Alice : hello"); mockDiagramRequest.setPath("/plantuml/png"); RoutingContext routingContext = mockDiagramRequest.getRoutingContext(); diagramHandler.createPost("plantuml").handle(routingContext); verify(routingContext).fail(any(UnsupportedFormatException.class)); }