Extract MockHttpHandler from GsaFeedFileSenderTest The mock in GsaFeedFileSenderTest is much more useful than the existing mock, which has been moved to AdministratorSecurityHandlerTest and renamed to CountingHttpHandler.
diff --git a/test/com/google/enterprise/adaptor/AdministratorSecurityHandlerTest.java b/test/com/google/enterprise/adaptor/AdministratorSecurityHandlerTest.java index d60382d..511f66e 100644 --- a/test/com/google/enterprise/adaptor/AdministratorSecurityHandlerTest.java +++ b/test/com/google/enterprise/adaptor/AdministratorSecurityHandlerTest.java
@@ -16,7 +16,8 @@ import static org.junit.Assert.*; -import com.sun.net.httpserver.*; +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; import org.junit.Test; @@ -29,7 +30,7 @@ private SessionManager<HttpExchange> sessionManager = new SessionManager<HttpExchange>(new MockTimeProvider(), new SessionManager.HttpExchangeClientStore(), 10000, 1000); - private MockHttpHandler mockHandler = new MockHttpHandler(); + private CountingHttpHandler mockHandler = new CountingHttpHandler(); private AdministratorSecurityHandler handler = new AdministratorSecurityHandler(mockHandler, sessionManager, new MockAuthnClient()); @@ -142,4 +143,18 @@ } } } + + private static class CountingHttpHandler implements HttpHandler { + private int executed; + + @Override + public void handle(HttpExchange ex) throws IOException { + executed++; + ex.sendResponseHeaders(200, -1); + } + + public int getTimesExecuted() { + return executed; + } + } }
diff --git a/test/com/google/enterprise/adaptor/GsaFeedFileSenderTest.java b/test/com/google/enterprise/adaptor/GsaFeedFileSenderTest.java index aacf092..0529aee 100644 --- a/test/com/google/enterprise/adaptor/GsaFeedFileSenderTest.java +++ b/test/com/google/enterprise/adaptor/GsaFeedFileSenderTest.java
@@ -16,7 +16,6 @@ import static org.junit.Assert.*; -import com.sun.net.httpserver.Headers; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; @@ -277,50 +276,4 @@ thrown.expect(IllegalArgumentException.class); sender.sendGroups("bad#source", "<payload/>", false); } - - private static class MockHttpHandler implements HttpHandler { - private final int responseCode; - private final byte[] responseBytes; - private String requestMethod; - private URI requestUri; - private Headers requestHeaders; - private byte[] requestBytes; - - public MockHttpHandler(int responseCode, byte[] responseBytes) { - this.responseCode = responseCode; - this.responseBytes = responseBytes; - } - - @Override - public void handle(HttpExchange ex) throws IOException { - requestMethod = ex.getRequestMethod(); - requestUri = ex.getRequestURI(); - requestHeaders = new Headers(); - requestHeaders.putAll(ex.getRequestHeaders()); - requestBytes = IOHelper.readInputStreamToByteArray(ex.getRequestBody()); - ex.sendResponseHeaders(responseCode, responseBytes == null ? -1 : 0); - if (responseBytes != null) { - ex.getResponseBody().write(responseBytes); - ex.getResponseBody().flush(); - ex.getResponseBody().close(); - } - ex.close(); - } - - public String getRequestMethod() { - return requestMethod; - } - - public URI getRequestUri() { - return requestUri; - } - - public Headers getRequestHeaders() { - return requestHeaders; - } - - public byte[] getRequestBytes() { - return requestBytes; - } - } }
diff --git a/test/com/google/enterprise/adaptor/LoggingFilterTest.java b/test/com/google/enterprise/adaptor/LoggingFilterTest.java index fa9e032..33fce89 100644 --- a/test/com/google/enterprise/adaptor/LoggingFilterTest.java +++ b/test/com/google/enterprise/adaptor/LoggingFilterTest.java
@@ -32,7 +32,7 @@ private LoggingFilter filter = new LoggingFilter(); private List<Filter> filters = Arrays.<Filter>asList(filter); private MockHttpExchange ex = new MockHttpExchange("GET", "/", - new MockHttpContext(new MockHttpHandler(), "/")); + new MockHttpContext(new MockHttpHandler(200, null), "/")); @Test public void testDescription() {
diff --git a/test/com/google/enterprise/adaptor/MockHttpHandler.java b/test/com/google/enterprise/adaptor/MockHttpHandler.java index e252a20..c338824 100644 --- a/test/com/google/enterprise/adaptor/MockHttpHandler.java +++ b/test/com/google/enterprise/adaptor/MockHttpHandler.java
@@ -14,23 +14,68 @@ package com.google.enterprise.adaptor; -import com.sun.net.httpserver.*; +import com.sun.net.httpserver.Headers; +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; import java.io.IOException; +import java.net.URI; +import java.util.*; /** * Mock {@link HttpHandler}. */ public class MockHttpHandler implements HttpHandler { - private int executed; + private final int responseCode; + private final byte[] responseBytes; + private final Map<String, List<String>> responseHeaders; + private String requestMethod; + private URI requestUri; + private Headers requestHeaders; + private byte[] requestBytes; + + public MockHttpHandler(int responseCode, byte[] responseBytes) { + this(responseCode, responseBytes, + Collections.<String, List<String>>emptyMap()); + } + + public MockHttpHandler(int responseCode, byte[] responseBytes, + Map<String, List<String>> responseHeaders) { + this.responseCode = responseCode; + this.responseBytes = responseBytes; + this.responseHeaders = responseHeaders; + } @Override public void handle(HttpExchange ex) throws IOException { - executed++; - ex.sendResponseHeaders(200, -1); + requestMethod = ex.getRequestMethod(); + requestUri = ex.getRequestURI(); + requestHeaders = new Headers(); + requestHeaders.putAll(ex.getRequestHeaders()); + requestBytes = IOHelper.readInputStreamToByteArray(ex.getRequestBody()); + ex.getResponseHeaders().putAll(responseHeaders); + ex.sendResponseHeaders(responseCode, responseBytes == null ? -1 : 0); + if (responseBytes != null) { + ex.getResponseBody().write(responseBytes); + ex.getResponseBody().flush(); + ex.getResponseBody().close(); + } + ex.close(); } - public int getTimesExecuted() { - return executed; + public String getRequestMethod() { + return requestMethod; + } + + public URI getRequestUri() { + return requestUri; + } + + public Headers getRequestHeaders() { + return requestHeaders; + } + + public byte[] getRequestBytes() { + return requestBytes; } }