1
2
3
4
5
6
7 package org.mule.transport.vm.functional;
8
9 import org.mule.api.MessagingException;
10 import org.mule.api.MuleEventContext;
11 import org.mule.api.MuleMessage;
12 import org.mule.api.lifecycle.Callable;
13 import org.mule.api.transport.PropertyScope;
14 import org.mule.module.client.MuleClient;
15 import org.mule.tck.junit4.FunctionalTestCase;
16
17 import java.util.HashMap;
18 import java.util.Map;
19
20 import org.junit.Test;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25
26 public class EndpointContentTypeTestCase extends FunctionalTestCase
27 {
28
29 @Override
30 protected String getConfigResources()
31 {
32 return "org/mule/test/config/content-type-setting-endpoint-configs.xml";
33 }
34
35 @Test
36 public void testContentTypes() throws Exception
37 {
38 MuleMessage response;
39 Map<String, Object> messageProperties = new HashMap<String, Object>();
40 messageProperties.put("content-type", "text/xml");
41 MuleClient client = new MuleClient(muleContext);
42 response = client.send("vm://in1?connector=vm-in1", "<OK/>", messageProperties);
43 assertNotNull(response);
44 assertNotNull("Invalid mime type was not rejected", response.getExceptionPayload());
45 assertTrue(response.getExceptionPayload().getException() instanceof MessagingException);
46 messageProperties.put("content-type", "text/plain");
47 EchoComponent.setExpectedContentType("text/plain");
48 response = client.send("vm://in1?connector=vm-in1", "OK", messageProperties);
49 assertNotNull(response);
50 assertEquals("OK", response.getPayload());
51
52 messageProperties.remove("content-type");
53 EchoComponent.setExpectedContentType("text/plain");
54 response = client.send("vm://in1?connector=vm-in1", "OK", messageProperties);
55 assertNotNull(response);
56 assertEquals("OK", response.getPayload());
57
58 messageProperties.put("content-type", "text/plain");
59 EchoComponent.setExpectedContentType("text/xml");
60 response = client.send("vm://in2?connector=vm-in2", "OK", messageProperties);
61 assertNotNull(response);
62 assertEquals("OK", response.getPayload());
63 }
64
65 public static class EchoComponent implements Callable
66 {
67 static String expectedContentType;
68
69 public Object onCall(MuleEventContext eventContext) throws Exception
70 {
71 MuleMessage message = eventContext.getMessage();
72 assertEquals(expectedContentType, message.getProperty("content-type", PropertyScope.INBOUND));
73 return message;
74 }
75
76 public static void setExpectedContentType(String expectedContentType)
77 {
78 EchoComponent.expectedContentType = expectedContentType;
79 }
80 }
81 }