From 25f70e90606cb12b017e1c6944ccbbeeaa89fabb Mon Sep 17 00:00:00 2001 From: Jesse McConnell Date: Wed, 2 Nov 2011 11:29:03 -0500 Subject: [PATCH] [Bug 362696] expand virtual host configuration options to ContextHandler and add associated test case for new behavior --- .../jetty/server/handler/ContextHandler.java | 6 ++-- .../server/handler/ContextHandlerTest.java | 31 +++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java index 024da10fb03..aa251dc1579 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java @@ -325,10 +325,10 @@ public class ContextHandler extends ScopedHandler implements Attributes, Server. for (int i = 0; i < virtualHosts.length; i++) { - String normalizedVirtualHost = normalizeHostname(virtualHosts[i]); - if (!existingVirtualHosts.contains(normalizedVirtualHost)) + String toRemoveVirtualHost = normalizeHostname(virtualHosts[i]); + if (existingVirtualHosts.contains(toRemoveVirtualHost)) { - existingVirtualHosts.remove(normalizedVirtualHost); + existingVirtualHosts.remove(toRemoveVirtualHost); } } diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ContextHandlerTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ContextHandlerTest.java index efb44bba5fe..6b1dcfa28cb 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ContextHandlerTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ContextHandlerTest.java @@ -232,6 +232,37 @@ public class ContextHandlerTest } } + @Test + public void testVirtualHostManagement() throws Exception + { + ContextHandler context = new ContextHandler("/"); + + // test singular + context.setVirtualHosts(new String[] { "www.example.com"} ); + Assert.assertEquals(1,context.getVirtualHosts().length); + + // test adding two more + context.addVirtualHosts(new String[] { "www.example2.com", "www.example3.com"}); + Assert.assertEquals(3,context.getVirtualHosts().length); + + // test adding existing context + context.addVirtualHosts(new String[] { "www.example.com" }); + Assert.assertEquals(3,context.getVirtualHosts().length); + + // test removing existing + context.removeVirtualHosts(new String[] { "www.example3.com" }); + Assert.assertEquals(2,context.getVirtualHosts().length); + + // test removing non-existent + context.removeVirtualHosts(new String[] { "www.example3.com" }); + Assert.assertEquals(2,context.getVirtualHosts().length); + + // test removing all remaining and resets to null + context.removeVirtualHosts(new String[] { "www.example.com", "www.example2.com" }); + Assert.assertEquals(null,context.getVirtualHosts()); + + } + @Test public void testAttributes() throws Exception {