Updated Branches: refs/heads/trunk 5bda4a70a -> 41f1e8afb
FLUME-2142. HTTPS tests for http source (Ashish Paliwal via Hari Shreedharan) Project: http://git-wip-us.apache.org/repos/asf/flume/repo Commit: http://git-wip-us.apache.org/repos/asf/flume/commit/41f1e8af Tree: http://git-wip-us.apache.org/repos/asf/flume/tree/41f1e8af Diff: http://git-wip-us.apache.org/repos/asf/flume/diff/41f1e8af Branch: refs/heads/trunk Commit: 41f1e8afbbc4854c516e1ffd0baf9d0de73d60af Parents: 5bda4a7 Author: Hari Shreedharan <[email protected]> Authored: Mon Aug 5 15:13:22 2013 -0700 Committer: Hari Shreedharan <[email protected]> Committed: Mon Aug 5 15:13:59 2013 -0700 ---------------------------------------------------------------------- .../flume/source/http/TestHTTPSource.java | 63 ++++++++++++++++---- 1 file changed, 51 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flume/blob/41f1e8af/flume-ng-core/src/test/java/org/apache/flume/source/http/TestHTTPSource.java ---------------------------------------------------------------------- diff --git a/flume-ng-core/src/test/java/org/apache/flume/source/http/TestHTTPSource.java b/flume-ng-core/src/test/java/org/apache/flume/source/http/TestHTTPSource.java index 6c9fd86..9e14648 100644 --- a/flume-ng-core/src/test/java/org/apache/flume/source/http/TestHTTPSource.java +++ b/flume-ng-core/src/test/java/org/apache/flume/source/http/TestHTTPSource.java @@ -42,6 +42,7 @@ import javax.net.ssl.*; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.lang.reflect.Type; +import java.net.HttpURLConnection; import java.net.ServerSocket; import java.net.URL; import java.security.SecureRandom; @@ -83,12 +84,11 @@ public class TestHTTPSource { channel = new MemoryChannel(); httpsSource = new HTTPSource(); -// httpsChannel = new MemoryChannel(); + httpsSource.setName("HTTPS Source"); Context ctx = new Context(); ctx.put("capacity", "100"); Configurables.configure(channel, ctx); -// Configurables.configure(httpsChannel, ctx); List<Channel> channels = new ArrayList<Channel>(1); channels.add(channel); @@ -100,15 +100,7 @@ public class TestHTTPSource { channel.start(); - // Channel for HTTPS source -// List<Channel> sslChannels = new ArrayList<Channel>(1); -// channels.add(httpsChannel); -// -// ChannelSelector sslRcs = new ReplicatingChannelSelector(); -// rcs.setChannels(sslChannels); - httpsSource.setChannelProcessor(new ChannelProcessor(rcs)); -// httpsChannel.start(); // HTTP context Context context = new Context(); @@ -135,7 +127,6 @@ public class TestHTTPSource { source.stop(); channel.stop(); httpsSource.stop(); -// httpsChannel.stop(); } @Before @@ -312,6 +303,7 @@ public class TestHTTPSource { for (int j = 0; j < 10; j++) { input.put(String.valueOf(i) + String.valueOf(j), String.valueOf(i)); } + input.put("MsgNum", String.valueOf(i)); JSONEvent e = new JSONEvent(); e.setHeaders(input); e.setBody(String.valueOf(rand.nextGaussian()).getBytes("UTF-8")); @@ -360,14 +352,61 @@ public class TestHTTPSource { int statusCode = httpsURLConnection.getResponseCode(); Assert.assertEquals(200, statusCode); + + Transaction transaction = channel.getTransaction(); + transaction.begin(); + for(int i = 0; i < 10; i++) { + Event e = channel.take(); + Assert.assertNotNull(e); + Assert.assertEquals(String.valueOf(i), e.getHeaders().get("MsgNum")); + } + + transaction.commit(); + transaction.close(); } catch (Exception exception) { Assert.fail("Exception not expected"); - exception.printStackTrace(); } finally { httpsURLConnection.disconnect(); } } + @Test + public void testHttpsSourceNonHttpsClient() throws Exception { + Type listType = new TypeToken<List<JSONEvent>>() { + }.getType(); + List<JSONEvent> events = Lists.newArrayList(); + Random rand = new Random(); + for (int i = 0; i < 10; i++) { + Map<String, String> input = Maps.newHashMap(); + for (int j = 0; j < 10; j++) { + input.put(String.valueOf(i) + String.valueOf(j), String.valueOf(i)); + } + input.put("MsgNum", String.valueOf(i)); + JSONEvent e = new JSONEvent(); + e.setHeaders(input); + e.setBody(String.valueOf(rand.nextGaussian()).getBytes("UTF-8")); + events.add(e); + } + Gson gson = new Gson(); + String json = gson.toJson(events, listType); + HttpURLConnection httpURLConnection = null; + try { + URL url = new URL("http://0.0.0.0:" + sslPort); + httpURLConnection = (HttpURLConnection) url.openConnection(); + httpURLConnection.setDoInput(true); + httpURLConnection.setDoOutput(true); + httpURLConnection.setRequestMethod("POST"); + httpURLConnection.getOutputStream().write(json.getBytes()); + httpURLConnection.getResponseCode(); + + Assert.fail("HTTP Client cannot connect to HTTPS source"); + } catch (Exception exception) { + Assert.assertTrue("Exception expected", true); + } finally { + httpURLConnection.disconnect(); + } + } + private void takeWithEncoding(String encoding, int n, List<JSONEvent> events) throws Exception{ Transaction tx = channel.getTransaction();
