Dashboard > DDSteps Website > HTTP Server Mock
  DDSteps Website Log In | Sign Up   View a printable version of the current page.  
  HTTP Server Mock
Added by Adam Skogman, last edited by Adam Skogman on Sep 27, 2007  (view change)
Labels: 
(None)

Get it

<repository>
            <id>ddsteps.org</id>
            <name>DDSteps Repo</name>
            <url>http://repo.ddsteps.org/maven/release/</url>
        </repository>
<dependency>
            <groupId>org.ddsteps</groupId>
            <artifactId>ddsteps-httpserver-mock</artifactId>
            <version>1.0-m1</version>
        </dependency>

Use it

This is a simple, call-yourself example. Uses HTTP-Client to call. Check out how powerful this can be - you can write basically any assert you can think of in the callback. And then serve up some content.

public class JettyMockServerTest extends TestCase {

	private Server server;
	private HttpClient httpclient;

	protected void setUp() throws Exception {
		server = new Server(3333);
		server.start();
		
		httpclient = new HttpClient();
	}

	public void testHello() throws Exception {

		JettyMockServer mockserver = new JettyMockServer(server);

		mockserver.expect("/foo", new Callback() {

			public void onExpectedRequest(String target,
					HttpServletRequest request, HttpServletResponse response)
					throws Exception {

				assertEquals("baz", request.getParameter("bar"));
				
				response.getWriter().println("Hello from Jetty Mock!");
				response.getWriter().flush();

			}
		});

		GetMethod getMethod = new GetMethod("http://localhost:3333/foo");
		getMethod.setQueryString(new NameValuePair[] { new NameValuePair("bar",
				"baz") });

		httpclient.executeMethod(getMethod);

		// assert response
		String response = getMethod.getResponseBodyAsString().trim();
		assertEquals("Hello from Jetty Mock!", response);

		// Check that the server was called
		mockserver.verify();

	}

Are you enjoying Confluence? Please consider purchasing it today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2 Build:#512 Apr 26, 2006) - Bug/feature request - Contact Administrators