Java tutorial
/* * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com * The software in this package is published under the terms of the CPAL v1.0 * license, a copy of which has been included with this distribution in the * LICENSE.txt file. */ package org.mule.listener; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertThat; import org.mule.tck.junit4.FunctionalTestCase; import org.mule.tck.junit4.rule.DynamicPort; import org.mule.tck.junit4.rule.SystemProperty; import org.mule.util.IOUtils; import org.apache.http.HttpResponse; import org.apache.http.client.fluent.Request; import org.apache.http.client.fluent.Response; import org.junit.Rule; import org.junit.Test; public class HttpListenerMethodRoutingTestCase extends FunctionalTestCase { @Rule public DynamicPort listenPort = new DynamicPort("port"); @Rule public DynamicPort listenPort2 = new DynamicPort("port2"); @Rule public SystemProperty path = new SystemProperty("path", "path"); @Override protected String getConfigFile() { return "http-listener-method-routing-config.xml"; } @Test public void notAllowedMethodReturns404() throws Exception { final String url = String.format("http://localhost:%s/%s", listenPort.getNumber(), path.getValue()); final Response response = Request.Put(url).connectTimeout(1000).execute(); final HttpResponse httpResponse = response.returnResponse(); assertThat(httpResponse.getStatusLine().getStatusCode(), is(404)); } @Test public void allowedCustomMethodReturns200() throws Exception { final String url = String.format("http://localhost:%s/%s", listenPort2.getNumber(), path.getValue()); final Response response = Request.Delete(url).connectTimeout(1000).execute(); final HttpResponse httpResponse = response.returnResponse(); assertThat(httpResponse.getStatusLine().getStatusCode(), is(200)); assertThat(IOUtils.toString(httpResponse.getEntity().getContent()), is("CUSTOM-METHOD")); } @Test public void notAllowedCustomMethodReturns404() throws Exception { final String url = String.format("http://localhost:%s/%s", listenPort2.getNumber(), path.getValue()); final Response response = Request.Put(url).connectTimeout(1000).execute(); final HttpResponse httpResponse = response.returnResponse(); assertThat(httpResponse.getStatusLine().getStatusCode(), is(404)); } }