Search in sources :

Example 16 with Matchers.containsString

use of org.hamcrest.Matchers.containsString in project jetty.project by eclipse.

the class SniSslConnectionFactoryTest method testBadSNIConnect.

@Test
public void testBadSNIConnect() throws Exception {
    String response = getResponse("www.example.com", "some.other.com", "www.example.com");
    Assert.assertThat(response, Matchers.containsString("HTTP/1.1 400 "));
    Assert.assertThat(response, Matchers.containsString("Host does not match SNI"));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 17 with Matchers.containsString

use of org.hamcrest.Matchers.containsString in project elasticsearch by elastic.

the class ScriptContextRegistryTests method testValidateCustomScriptContextsOperation.

public void testValidateCustomScriptContextsOperation() throws IOException {
    for (final String rejectedContext : ScriptContextRegistry.RESERVED_SCRIPT_CONTEXTS) {
        try {
            //try to register a prohibited script context
            new ScriptContextRegistry(Arrays.asList(new ScriptContext.Plugin("test", rejectedContext)));
            fail("ScriptContextRegistry initialization should have failed");
        } catch (IllegalArgumentException e) {
            assertThat(e.getMessage(), Matchers.containsString("[" + rejectedContext + "] is a reserved name, it cannot be registered as a custom script context"));
        }
    }
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 18 with Matchers.containsString

use of org.hamcrest.Matchers.containsString in project jetty.project by eclipse.

the class ContextHandlerCollectionTest method testVirtualHosts.

@Test
public void testVirtualHosts() throws Exception {
    Server server = new Server();
    LocalConnector connector0 = new LocalConnector(server);
    LocalConnector connector1 = new LocalConnector(server);
    connector1.setName("connector1");
    server.setConnectors(new Connector[] { connector0, connector1 });
    ContextHandler contextA = new ContextHandler("/ctx");
    contextA.setVirtualHosts(new String[] { "www.example.com", "alias.example.com" });
    IsHandledHandler handlerA = new IsHandledHandler("A");
    contextA.setHandler(handlerA);
    contextA.setAllowNullPathInfo(true);
    ContextHandler contextB = new ContextHandler("/ctx");
    IsHandledHandler handlerB = new IsHandledHandler("B");
    contextB.setHandler(handlerB);
    contextB.setVirtualHosts(new String[] { "*.other.com", "@connector1" });
    ContextHandler contextC = new ContextHandler("/ctx");
    IsHandledHandler handlerC = new IsHandledHandler("C");
    contextC.setHandler(handlerC);
    ContextHandler contextD = new ContextHandler("/");
    IsHandledHandler handlerD = new IsHandledHandler("D");
    contextD.setHandler(handlerD);
    ContextHandler contextE = new ContextHandler("/ctx/foo");
    IsHandledHandler handlerE = new IsHandledHandler("E");
    contextE.setHandler(handlerE);
    ContextHandler contextF = new ContextHandler("/ctxlong");
    IsHandledHandler handlerF = new IsHandledHandler("F");
    contextF.setHandler(handlerF);
    ContextHandlerCollection c = new ContextHandlerCollection();
    c.addHandler(contextA);
    c.addHandler(contextB);
    c.addHandler(contextC);
    HandlerList list = new HandlerList();
    list.addHandler(contextE);
    list.addHandler(contextF);
    list.addHandler(contextD);
    c.addHandler(list);
    server.setHandler(c);
    try {
        server.start();
        Object[][] tests = new Object[][] { { connector0, "www.example.com.", "/ctx", handlerA }, { connector0, "www.example.com.", "/ctx/", handlerA }, { connector0, "www.example.com.", "/ctx/info", handlerA }, { connector0, "www.example.com", "/ctx/info", handlerA }, { connector0, "alias.example.com", "/ctx/info", handlerA }, { connector1, "www.example.com.", "/ctx/info", handlerA }, { connector1, "www.example.com", "/ctx/info", handlerA }, { connector1, "alias.example.com", "/ctx/info", handlerA }, { connector1, "www.other.com", "/ctx", null }, { connector1, "www.other.com", "/ctx/", handlerB }, { connector1, "www.other.com", "/ctx/info", handlerB }, { connector0, "www.other.com", "/ctx/info", handlerC }, { connector0, "www.example.com", "/ctxinfo", handlerD }, { connector1, "unknown.com", "/unknown", handlerD }, { connector0, "alias.example.com", "/ctx/foo/info", handlerE }, { connector0, "alias.example.com", "/ctxlong/info", handlerF } };
        for (int i = 0; i < tests.length; i++) {
            Object[] test = tests[i];
            LocalConnector connector = (LocalConnector) test[0];
            String host = (String) test[1];
            String uri = (String) test[2];
            IsHandledHandler handler = (IsHandledHandler) test[3];
            handlerA.reset();
            handlerB.reset();
            handlerC.reset();
            handlerD.reset();
            handlerE.reset();
            handlerF.reset();
            String t = String.format("test   %d %s@%s --> %s | %s%n", i, uri, host, connector.getName(), handler);
            String response = connector.getResponses("GET " + uri + " HTTP/1.0\nHost: " + host + "\n\n");
            if (handler == null) {
                Assert.assertThat(t, response, Matchers.containsString(" 302 "));
            } else {
                assertThat(t, response, endsWith(handler.toString()));
                if (!handler.isHandled()) {
                    System.err.printf("FAILED %s", t);
                    System.err.println(response);
                    Assert.fail();
                }
            }
        }
    } finally {
        server.stop();
    }
}
Also used : Server(org.eclipse.jetty.server.Server) LocalConnector(org.eclipse.jetty.server.LocalConnector) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 19 with Matchers.containsString

use of org.hamcrest.Matchers.containsString in project jetty.project by eclipse.

the class ResponseTest method testSetRFC6265Cookie.

@Test
public void testSetRFC6265Cookie() throws Exception {
    Response response = _channel.getResponse();
    HttpFields fields = response.getHttpFields();
    response.addSetRFC6265Cookie("null", null, null, null, -1, false, false);
    assertEquals("null=", fields.get("Set-Cookie"));
    fields.clear();
    response.addSetRFC6265Cookie("minimal", "value", null, null, -1, false, false);
    assertEquals("minimal=value", fields.get("Set-Cookie"));
    fields.clear();
    //test cookies with same name, domain and path
    response.addSetRFC6265Cookie("everything", "something", "domain", "path", 0, true, true);
    response.addSetRFC6265Cookie("everything", "value", "domain", "path", 0, true, true);
    Enumeration<String> e = fields.getValues("Set-Cookie");
    assertTrue(e.hasMoreElements());
    assertEquals("everything=something;Path=path;Domain=domain;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Max-Age=0;Secure;HttpOnly", e.nextElement());
    assertEquals("everything=value;Path=path;Domain=domain;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Max-Age=0;Secure;HttpOnly", e.nextElement());
    assertFalse(e.hasMoreElements());
    assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", fields.get("Expires"));
    assertFalse(e.hasMoreElements());
    //test cookies with same name, different domain
    fields.clear();
    response.addSetRFC6265Cookie("everything", "other", "domain1", "path", 0, true, true);
    response.addSetRFC6265Cookie("everything", "value", "domain2", "path", 0, true, true);
    e = fields.getValues("Set-Cookie");
    assertTrue(e.hasMoreElements());
    assertEquals("everything=other;Path=path;Domain=domain1;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Max-Age=0;Secure;HttpOnly", e.nextElement());
    assertTrue(e.hasMoreElements());
    assertEquals("everything=value;Path=path;Domain=domain2;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Max-Age=0;Secure;HttpOnly", e.nextElement());
    assertFalse(e.hasMoreElements());
    //test cookies with same name, same path, one with domain, one without
    fields.clear();
    response.addSetRFC6265Cookie("everything", "other", "domain1", "path", 0, true, true);
    response.addSetRFC6265Cookie("everything", "value", "", "path", 0, true, true);
    e = fields.getValues("Set-Cookie");
    assertTrue(e.hasMoreElements());
    assertEquals("everything=other;Path=path;Domain=domain1;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Max-Age=0;Secure;HttpOnly", e.nextElement());
    assertTrue(e.hasMoreElements());
    assertEquals("everything=value;Path=path;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Max-Age=0;Secure;HttpOnly", e.nextElement());
    assertFalse(e.hasMoreElements());
    //test cookies with same name, different path
    fields.clear();
    response.addSetRFC6265Cookie("everything", "other", "domain1", "path1", 0, true, true);
    response.addSetRFC6265Cookie("everything", "value", "domain1", "path2", 0, true, true);
    e = fields.getValues("Set-Cookie");
    assertTrue(e.hasMoreElements());
    assertEquals("everything=other;Path=path1;Domain=domain1;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Max-Age=0;Secure;HttpOnly", e.nextElement());
    assertTrue(e.hasMoreElements());
    assertEquals("everything=value;Path=path2;Domain=domain1;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Max-Age=0;Secure;HttpOnly", e.nextElement());
    assertFalse(e.hasMoreElements());
    //test cookies with same name, same domain, one with path, one without
    fields.clear();
    response.addSetRFC6265Cookie("everything", "other", "domain1", "path1", 0, true, true);
    response.addSetRFC6265Cookie("everything", "value", "domain1", "", 0, true, true);
    e = fields.getValues("Set-Cookie");
    assertTrue(e.hasMoreElements());
    assertEquals("everything=other;Path=path1;Domain=domain1;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Max-Age=0;Secure;HttpOnly", e.nextElement());
    assertTrue(e.hasMoreElements());
    assertEquals("everything=value;Domain=domain1;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Max-Age=0;Secure;HttpOnly", e.nextElement());
    assertFalse(e.hasMoreElements());
    //test cookies same name only, no path, no domain
    fields.clear();
    response.addSetRFC6265Cookie("everything", "other", "", "", 0, true, true);
    response.addSetRFC6265Cookie("everything", "value", "", "", 0, true, true);
    e = fields.getValues("Set-Cookie");
    assertTrue(e.hasMoreElements());
    assertEquals("everything=other;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Max-Age=0;Secure;HttpOnly", e.nextElement());
    assertEquals("everything=value;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Max-Age=0;Secure;HttpOnly", e.nextElement());
    assertFalse(e.hasMoreElements());
    String[] badNameExamples = { "\"name\"", "name\t", "na me", "name‚", "na\tme", "na;me", "{name}", "[name]", "\"" };
    for (String badNameExample : badNameExamples) {
        fields.clear();
        try {
            response.addSetRFC6265Cookie(badNameExample, "value", null, "/", 1, true, true);
        } catch (IllegalArgumentException ex) {
            // System.err.printf("%s: %s%n", ex.getClass().getSimpleName(), ex.getMessage());
            assertThat("Testing bad name: [" + badNameExample + "]", ex.getMessage(), allOf(containsString("RFC6265"), containsString("RFC2616")));
        }
    }
    String[] badValueExamples = { "va\tlue", "\t", "value", "val‚ue", "va lue", "va;lue", "\"value", "value\"", "val\\ue", "val\"ue", "\"" };
    for (String badValueExample : badValueExamples) {
        fields.clear();
        try {
            response.addSetRFC6265Cookie("name", badValueExample, null, "/", 1, true, true);
        } catch (IllegalArgumentException ex) {
            // System.err.printf("%s: %s%n", ex.getClass().getSimpleName(), ex.getMessage());
            assertThat("Testing bad value [" + badValueExample + "]", ex.getMessage(), Matchers.containsString("RFC6265"));
        }
    }
    String[] goodNameExamples = { "name", "n.a.m.e", "na-me", "+name", "na*me", "na$me", "#name" };
    for (String goodNameExample : goodNameExamples) {
        fields.clear();
        response.addSetRFC6265Cookie(goodNameExample, "value", null, "/", 1, true, true);
    // should not throw an exception
    }
    String[] goodValueExamples = { "value", "", null, "val=ue", "val-ue", "val/ue", "v.a.l.u.e" };
    for (String goodValueExample : goodValueExamples) {
        fields.clear();
        response.addSetRFC6265Cookie("name", goodValueExample, null, "/", 1, true, true);
    // should not throw an exception
    }
    fields.clear();
    response.addSetRFC6265Cookie("name", "value", "domain", null, -1, false, false);
    response.addSetRFC6265Cookie("name", "other", "domain", null, -1, false, false);
    response.addSetRFC6265Cookie("name", "more", "domain", null, -1, false, false);
    e = fields.getValues("Set-Cookie");
    assertTrue(e.hasMoreElements());
    assertThat(e.nextElement(), Matchers.startsWith("name=value"));
    assertThat(e.nextElement(), Matchers.startsWith("name=other"));
    assertThat(e.nextElement(), Matchers.startsWith("name=more"));
    response.addSetRFC6265Cookie("foo", "bar", "domain", null, -1, false, false);
    response.addSetRFC6265Cookie("foo", "bob", "domain", null, -1, false, false);
    assertThat(fields.get("Set-Cookie"), Matchers.startsWith("name=value"));
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpFields(org.eclipse.jetty.http.HttpFields) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 20 with Matchers.containsString

use of org.hamcrest.Matchers.containsString in project jetty.project by eclipse.

the class SniSslConnectionFactoryTest method testSNIConnectNoWild.

@Test
public void testSNIConnectNoWild() throws Exception {
    // Use the alternate keystore without wildcard certificates.
    _server.stop();
    _server.removeConnector(_connector);
    SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setKeyStorePath("src/test/resources/snikeystore_nowild");
    sslContextFactory.setKeyStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
    sslContextFactory.setKeyManagerPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
    _connector = new ServerConnector(_server, new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()), new HttpConnectionFactory(_https_config));
    _server.addConnector(_connector);
    _server.start();
    _port = _connector.getLocalPort();
    // The first entry in the keystore is www.example.com, and it will
    // be returned by default, so make sure that here we don't ask for it.
    String response = getResponse("jetty.eclipse.org", "jetty.eclipse.org");
    Assert.assertThat(response, Matchers.containsString("X-HOST: jetty.eclipse.org"));
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) Matchers.containsString(org.hamcrest.Matchers.containsString) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) Test(org.junit.Test)

Aggregations

Matchers.containsString (org.hamcrest.Matchers.containsString)26 Test (org.junit.Test)24 Socket (java.net.Socket)12 OutputStream (java.io.OutputStream)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 ServletOutputStream (javax.servlet.ServletOutputStream)9 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)6 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 IOException (java.io.IOException)4 ServletException (javax.servlet.ServletException)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)4 InputStream (java.io.InputStream)3 ByteBuffer (java.nio.ByteBuffer)3 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)3 AbstractLogger (org.eclipse.jetty.util.log.AbstractLogger)3 SSLSocket (javax.net.ssl.SSLSocket)2 ServletInputStream (javax.servlet.ServletInputStream)2 ServerConnector (org.eclipse.jetty.server.ServerConnector)2 SslConnectionFactory (org.eclipse.jetty.server.SslConnectionFactory)2