Skip to content

Instantly share code, notes, and snippets.

@mcantrell
Last active December 17, 2015 00:59
Show Gist options
  • Save mcantrell/5525253 to your computer and use it in GitHub Desktop.
Save mcantrell/5525253 to your computer and use it in GitHub Desktop.
Blog Post: Mocking HTTP Endpoints in Mule Functional Tests These are simplified examples of code for embedding within the following Confluex blog post: http://www.confluex.com/blog/mocking-http-endpoints-in-mule-functional-tests/
package com.confluex.mule.test.functional
import com.confluex.mule.test.http.MockHttpRequestHandler
import com.confluex.mule.test.http.expectations.HeaderExpectation
import com.confluex.mule.test.http.expectations.MediaTypeExpectation
import com.confluex.mule.test.http.expectations.MethodExpectation
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.mortbay.jetty.Server
import org.mule.tck.junit4.FunctionalTestCase
class HttpMockFunctionalTest extends FunctionalTestCase {
Server server
MockHttpRequestHandler handler
/**
* The Mule Configuration file(s) to load when starting the embedded Mule server
*/
protected String getConfigResources() {
return "test-mock-http-config.xml"
}
/**
* Create a new Jetty server before each test and assign our MockHttpRequest handler to process
* requests.
*/
@Before
void createMockHttpServer() {
server = new Server(9001)
handler = new MockHttpRequestHandler()
server.handler = handler
server.start()
}
/**
* Stop the server between each test to ensure all of the connections are cleaned up.
*/
@After
void stopMockHttpServer() {
server.stop()
}
/**
* Retrieve the catalog XML and assert interactions
*/
@Test
void shouldListCatalog() {
handler.when("/catalog")
.thenReturnResource("/payloads/catalog.xml")
.withStatus(200)
def message = muleContext.client.send("requestCatalog", "", [:])
assert message.payloadAsString == this.class.getResourceAsStream("/payloads/catalog.xml").text
handler.verify("/catalog", MethodExpectation.GET)
}
/**
* Update the catalog with XML and assert interactions
*/
@Test
void shouldUpdateCatalog() {
handler.when("/catalog")
.thenReturnText("Updated")
.withStatus(302)
.withHeader("Location", "http://localhost:9001/catalog")
def payload = [
[id: 1, name: "Super Widget"],
[id: 2, name: "Super Gadget"]
]
muleContext.client.dispatch("updateCatalog", payload, [:])
// vm endpoint is async, we need to wait until the handler processes
// the request or times out (error condition)
assert handler.waitForEvents(1, 10000)
// now we can do verifications
handler.verify("/catalog",
MethodExpectation.PUT,
MediaTypeExpectation.XML,
new HeaderExpectation("updatedBy", "Bill Murray")
)
// you can also get access to the raw client request data if desired
def requests = handler.getRequests("/catalog")
assert requests.size() == 1
assert requests[0].headers['Content-Type'] == "application/xml"
assert requests[0].headers['X-MULE_ENDPOINT'] == "http://localhost:9001/catalog"
assert requests[0].headers.updatedBy == "Bill Murray"
assert requests[0].method == "PUT"
assert requests[0].body.startsWith("<list>")
}
}
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<context:property-placeholder location="classpath:mule-config.properties"/>
<vm:endpoint name="requestCatalog" path="catalog.request" exchange-pattern="request-response"/>
<vm:endpoint name="updateCatalog" path="catalog.update" exchange-pattern="one-way"/>
<flow name="RequestCatalogFlow">
<inbound-endpoint ref="requestCatalog"/>
<logger level="INFO" category="RequestCatalogFlow" message="Request payload: #[payload]"/>
<http:outbound-endpoint host="${service.host}" port="${service.port}" path="/catalog" method="GET"/>
<logger level="INFO" category="RequestCatalogFlow" message="Response payload: #[payload]"/>
</flow>
<flow name="UpdateCatalogFlow">
<inbound-endpoint ref="updateCatalog"/>
<logger level="INFO" category="UpdateCatalogFlow" message="Request payload: #[payload]"/>
<set-property propertyName="updatedBy" value="Bill Murray"/>
<mulexml:object-to-xml-transformer/>
<http:outbound-endpoint host="${service.host}" port="${service.port}" path="/catalog" method="PUT"
contentType="application/xml"/>
<logger level="INFO" category="UpdateCatalogFlow" message="Response payload: #[payload]"/>
</flow>
</mule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment