@Test public void newExecutionContextForNonInterceptedProcessor() throws MuleException { final AtomicReference<PrecalculatedExecutionContextAdapter> context = new AtomicReference<>(); Map<String, String> newContextParameters = ImmutableMap.of(MIME_TYPE_PARAMETER_NAME, MediaType.ANY.toRfcString(), ENCODING_PARAMETER_NAME, Charset.defaultCharset().name()); doReturn(newContextParameters).when(parameters).asMap(); doReturn(parameters).when(resolverSet).resolve(any(ValueResolvingContext.class)); messageProcessor.resolveParameters(CoreEvent.builder(event), (params, ctx) -> { assertThat(ctx, instanceOf(PrecalculatedExecutionContextAdapter.class)); context.set(spy((PrecalculatedExecutionContextAdapter) ctx)); }); messageProcessor.process(quickCopy(event, singletonMap(INTERCEPTION_RESOLVED_CONTEXT, context.get()))); verify(operationExecutor, never()).execute(context.get()); verify(operationExecutor).execute(any(ExecutionContext.class)); messageProcessor.disposeResolvedParameters(context.get()); }
@Test public void operationTargetMessageWithDefaultMimeType() { when(componentModel.getModelProperty(MediaTypeModelProperty.class)).thenReturn(of( new MediaTypeModelProperty(APPLICATION_JSON .toRfcString(), true))); delegate = createDelegate("#[message]"); CoreEvent result = delegate.asReturnValue(payload, operationContext); assertMessage(result.getMessage()); assertThat(result.getVariables().get(TARGET).getValue(), is(instanceOf(Message.class))); Message message = (Message) result.getVariables().get(TARGET).getValue(); assertThat(message.getPayload().getValue(), is(payload)); assertThat(message.getPayload().getDataType().getMediaType().toRfcString(), containsString(APPLICATION_JSON.toRfcString())); }
@Test public void generatesContentTypeWithCharset() throws Exception { final DataType dataType = DataType.builder().type(Object.class).mediaType(APPLICATION_JSON).charset(UTF_8.name()).build(); String contentType = dataType.getMediaType().toRfcString(); assertThat(contentType, equalTo("application/json; charset=UTF-8")); }
@Test public void operationWithDefaultMimeType() throws Exception { when(componentModel.getModelProperty(MediaTypeModelProperty.class)).thenReturn(Optional.of( new MediaTypeModelProperty(APPLICATION_JSON .toRfcString(), true))); delegate = createReturnDelegate(); Object value = "Hello world!"; CoreEvent result = delegate.asReturnValue(value, operationContext); Message message = getOutputMessage(result); assertThat(message.getPayload().getValue(), is(sameInstance(value))); assertThat(message.getPayload().getDataType().getType().equals(String.class), is(true)); assertThat(message.getPayload().getDataType().getMediaType().toRfcString(), containsString(APPLICATION_JSON.toRfcString())); }
@Test public void generatesContentTypeWithoutCharset() throws Exception { DataType dataType = DataType.builder().type(Object.class).mediaType(MediaType.APPLICATION_JSON).build(); String contentType = dataType.getMediaType().toRfcString(); assertThat(contentType, equalTo("application/json")); } }
/** * @return a textual representation of this object. */ @Override public String toString() { return toRfcString(); }
@Test public void parseTemplateWithOverriddenDataType() throws Exception { String customEncoding = "UTF-16"; MediaType customMediaType = create("application", "lrmextension"); parseTemplateProcessor.setLocation(UNKNOWN_MEDIATYPE_LOCATION); parseTemplateProcessor.setOutputMimeType(customMediaType.toRfcString()); parseTemplateProcessor.setOutputEncoding(customEncoding); parseTemplateProcessor.initialise(); String expectedExpression = IOUtils.getResourceAsString(UNKNOWN_MEDIATYPE_LOCATION, this.getClass()); when(mockExpressionManager.parseLogTemplate(eq(expectedExpression), eq(event), any(), any())).thenReturn("Parsed"); CoreEvent response = parseTemplateProcessor.process(event); assertThat(response.getMessage().getPayload().getDataType().getMediaType().getPrimaryType(), is(equalTo(customMediaType.getPrimaryType()))); assertThat(response.getMessage().getPayload().getDataType().getMediaType().getSubType(), is(equalTo(customMediaType.getSubType()))); assertThat(response.getMessage().getPayload().getDataType().getMediaType().getCharset().get().toString(), is(equalTo(customEncoding))); }
@Test public void verifyDataType() throws Exception { CoreEvent muleEvent = flowRunner("test").withMediaType(MediaType.JSON).withPayload("{ \"name\":\"Emiliano\" }").run(); MatcherAssert.assertThat(muleEvent.getMessage().getPayload().getValue(), is(MediaType.JSON.toRfcString())); }
@Test public void parseTemplateWithOverriddenDataTypeAsExpression() throws Exception { String customEncoding = "UTF-16"; MediaType customMediaType = create("application", "lrmextension"); parseTemplateProcessor.setLocation(UNKNOWN_MEDIATYPE_LOCATION); parseTemplateProcessor.setOutputMimeType(customMediaType.toRfcString()); parseTemplateProcessor.setOutputEncoding(customEncoding); parseTemplateProcessor.initialise(); String expectedExpression = IOUtils.getResourceAsString(UNKNOWN_MEDIATYPE_LOCATION, this.getClass()); when(mockExpressionManager.parseLogTemplate(eq(expectedExpression), eq(event), any(), any())).thenReturn("Parsed"); CoreEvent response = parseTemplateProcessor.process(event); assertThat(response.getMessage().getPayload().getDataType().getMediaType().getPrimaryType(), is(equalTo(customMediaType.getPrimaryType()))); assertThat(response.getMessage().getPayload().getDataType().getMediaType().getSubType(), is(equalTo(customMediaType.getSubType()))); assertThat(response.getMessage().getPayload().getDataType().getMediaType().getCharset().get().toString(), is(equalTo(customEncoding))); }
private String getDescription(MediaType mediaType) { return mediaType.withoutParameters().toRfcString(); }
public static TypedValue fromMuleTypedValue(org.mule.runtime.api.metadata.TypedValue typedValue) { MediaType mediaType = typedValue.getDataType().getMediaType(); return new TypedValue(typedValue.getValue(), mediaType.withoutParameters().toRfcString(), mediaType.getCharset().orElse(null)); }
private void verifyErrorResponse(HttpRequestBase httpRequest, String path, HttpStatus expectedStatus, String expectedBody) throws Exception { try (CloseableHttpClient httpClient = HttpClients.createDefault()) { httpRequest.setURI(getUri(path)); try (CloseableHttpResponse response = httpClient.execute(httpRequest)) { assertThat(response.getStatusLine().getStatusCode(), is(expectedStatus.getStatusCode())); assertThat(response.getStatusLine().getReasonPhrase(), is(expectedStatus.getReasonPhrase())); assertThat(response.getFirstHeader(CONTENT_TYPE).getValue(), is(TEXT.toRfcString())); assertThat(IOUtils.toString(response.getEntity().getContent()), is(expectedBody)); } } }
@Test public void generatesContentTypeWithCharset() throws Exception { final DataType dataType = DataType.builder().type(Object.class).mediaType(APPLICATION_JSON).charset(UTF_8.name()).build(); String contentType = dataType.getMediaType().toRfcString(); assertThat(contentType, equalTo("application/json; charset=UTF-8")); }
@Test public void generatesContentTypeWithoutCharset() throws Exception { DataType dataType = DataType.builder().type(Object.class).mediaType(MediaType.APPLICATION_JSON).build(); String contentType = dataType.getMediaType().toRfcString(); assertThat(contentType, equalTo("application/json")); } }
@Override protected HttpResponse setUpHttpResponse(HttpRequest request) { HttpResponseBuilder response = HttpResponse.builder(); try { Collection<HttpPart> parts = request.getEntity().getParts(); if (parts.size() == 1 && parts.stream().anyMatch(part -> JSON.toRfcString().equals(part.getContentType()))) { return response.statusCode(OK.getStatusCode()).entity(new ByteArrayHttpEntity(OK.getReasonPhrase().getBytes())).build(); } } catch (IOException e) { // Move on } return response.statusCode(INTERNAL_SERVER_ERROR.getStatusCode()).build(); }
@Test public void hasParts() throws IOException { Object[] parts = entity.getParts().toArray(); assertThat(parts, arrayWithSize(2)); HttpPart part1 = (HttpPart) parts[0]; assertThat(part1.getContentType(), is(APPLICATION_JSON.toRfcString())); assertThat(part1.getName(), is("img")); assertThat(part1.getFileName(), is("a.png")); assertThat(IOUtils.toString(part1.getInputStream()), is("{\r\n" + "\t\"key\" : \"value\"\r\n" + "}")); HttpPart part2 = (HttpPart) parts[1]; assertThat(part2.getContentType(), is(TEXT.toRfcString())); assertThat(part2.getName(), is("foo")); assertThat(part2.getFileName(), is(nullValue())); assertThat(IOUtils.toString(part2.getInputStream()), is("bar")); }
private void testWithContentDisposition(Boolean addHeader) throws Exception { MultiMap<String, String> queryParams = new MultiMap<>(); queryParams.put(CONTENT_DISPOSITION_PARAM, addHeader.toString()); HttpResponse response = client.send(HttpRequest.builder().uri(getUri()).queryParams(queryParams).build(), TIMEOUT, true, null); assertThat(response.getEntity().isComposed(), is(true)); Collection<HttpPart> parts = response.getEntity().getParts(); assertThat(parts, hasSize(1)); HttpPart part = parts.iterator().next(); assertThat(part.getContentType(), is(TEXT.toRfcString())); assertThat(IOUtils.toString(part.getInputStream()), is("test")); if (addHeader) { assertThat(part.getName(), is("partName")); } }
@Test @Description("Verify that parts content type is preserved.") public void partContentTypeIsPreserved() throws Exception { HttpPart part = new HttpPart("someJson", dataBytes, JSON.toRfcString(), dataBytes.length); MultipartHttpEntity multipart = new MultipartHttpEntity(singletonList(part)); final HttpResponse response = client.send(HttpRequest.builder() .method(POST) .uri(getUri()) .entity(multipart) .build(), TIMEOUT, true, null); assertThat(IOUtils.toString(response.getEntity().getContent()), is(equalTo("OK"))); }
@Before public void setUp() throws Exception { server = service.getServerFactory().create(new HttpServerConfiguration.Builder() .setHost("localhost") .setPort(port.getNumber()) .setName("parts-test") .build()); server.start(); server.addRequestHandler("/", (requestContext, responseCallback) -> { String data = "My data here"; HttpPart part = new HttpPart("field", data.getBytes(), TEXT.toRfcString(), data.length()); part.addHeader("Custom", "myHeader"); responseCallback.responseReady(HttpResponse.builder().entity(new MultipartHttpEntity(singletonList(part))) .addHeader(CONTENT_TYPE, MULTIPART_MIXED.toRfcString() + "; boundary=\"the-boundary\"") .build(), new IgnoreResponseStatusCallback()); }); }
@Before public void setUp() throws Exception { server = service.getServerFactory().create(new HttpServerConfiguration.Builder() .setHost("localhost") .setPort(port.getNumber()) .setName("errors-test") .build()); server.start(); server.addRequestHandler(singletonList(GET.name()), "/test", (requestContext, responseCallback) -> { responseCallback.responseReady(HttpResponse.builder().entity(new ByteArrayHttpEntity("Success!".getBytes())) .addHeader(CONTENT_TYPE, TEXT.toRfcString()) .build(), new IgnoreResponseStatusCallback()); }); }