如何在mule FunctionalTestCase中设置http标头的值

时间:2013-07-04 15:58:27

标签: mule

我有一个流程需要从HTTP请求访问特定标题“REMOTE_USER”:

 <flow name="http" >
      <http:inbound-endpoint exchange-pattern="request-response" host="127.0.0.1" port="8501"/>
      <logger message="#[message.inboundProperties.get('http.headers').get('REMOTE_USER')]" level="ERROR" />
...

我想编写一个单元测试,设置此标题并测试流程。

@Test
public void testSend() throws Exception{
  MuleClient client = muleContext.getClient();                                
  HashMap<String, Object> headers = new HashMap<String, Object>();
  headers.put("REMOTE_USER", "test");
  Map<String, Object> properties = new HashMap<String, Object>();           
  properties.put("http.headers", headers);          
  MuleMessage result = client.send("http://localhost:8501", "test", properties);         
  assertNotNull(result.getPayloadAsString());
}    

有没有办法测试这个?

1 个答案:

答案 0 :(得分:0)

要添加HTTP标头,请使用范围为outbound的消息属性:

 <message-properties-transformer  doc:name="REMOTE_USER">
            <add-message-property key="REMOTE_USER" value="abcdxyz"/>
 </message-properties-transformer>

在功能测试中测试:

public class XYZTest extends FunctionalTestCase {

 @Test
 public void testXyz(){
        HttpClient hc = new DefaultHttpClient();
        HttpPost post = new HttpPost();
        post.set.....
         .......

      org.apache.http.HttpResponse response = hc.execute(post);
      assertNotNull(response.getFirstHeader("REMOTE_USER"));
  }  
}