Search in sources :

Example 1 with MockHttpServletRequest

use of com.zimbra.cs.service.MockHttpServletRequest in project zm-mailbox by Zimbra.

the class ImapLoadBalancingMechanismTest method accountIdHashMechanismHashFromPool.

@Test
public void accountIdHashMechanismHashFromPool() throws Exception {
    ImapLoadBalancingMechanism mech = ImapLoadBalancingMechanism.newInstance(ImapLoadBalancingMechanism.ImapLBMech.AccountIdHash.name());
    ArrayList<Server> pool = new ArrayList<Server>();
    Server s0 = new MockServer("server-0", "0");
    Server s1 = new MockServer("server-1", "1");
    Server s2 = new MockServer("server-2", "2");
    // Add to pool out-of-order to verify that we are sorting correctly
    pool.add(s1);
    pool.add(s0);
    pool.add(s2);
    HashMap<String, String> headers = new HashMap<String, String>();
    HttpServletRequest req = new MockHttpServletRequest(null, null, null, 123, "127.0.0.1", headers);
    String acctId0 = "79e9a595-c34b-469a-a1ee-e2b9d03f9aa7";
    /* hash=1536494685, hash % 3 = 0 */
    String acctId1 = "615922e5-2318-4b8b-8734-d209a99f8252";
    /* hash=454270162,  hash % 3 = 1 */
    String acctId2 = "f5c68357-61d9-4658-a7fc-e7273929ca0c";
    /* hash=1373626454, hash % 3 = 2 */
    assertEquals("Should have got 0th entry from sorted pool", s0, mech.getImapServerFromPool(req, acctId0, pool));
    assertEquals("Should have got 1st entry from sorted pool", s1, mech.getImapServerFromPool(req, acctId1, pool));
    assertEquals("Should have got 2nd entry from sorted pool", s2, mech.getImapServerFromPool(req, acctId2, pool));
}
Also used : MockHttpServletRequest(com.zimbra.cs.service.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockServer(com.zimbra.cs.account.MockServer) Server(com.zimbra.cs.account.Server) MockServer(com.zimbra.cs.account.MockServer) HashMap(java.util.HashMap) MockHttpServletRequest(com.zimbra.cs.service.MockHttpServletRequest) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 2 with MockHttpServletRequest

use of com.zimbra.cs.service.MockHttpServletRequest in project zm-mailbox by Zimbra.

the class TestRemoteIP method testNoXForwardedHeaders.

@Test
public void testNoXForwardedHeaders() throws UnsupportedEncodingException, MalformedURLException {
    HashMap<String, String> headers = new HashMap<String, String>();
    MockHttpServletRequest req = new MockHttpServletRequest("test".getBytes("UTF-8"), new URL("http://localhost:7070/service/FooRequest"), "", 80, "192.168.1.1", headers);
    RemoteIP remoteIp = new RemoteIP(req, new RemoteIP.TrustedIPs(new String[] { "192.168.1.1" }));
    assertNull("originating IP should be null", remoteIp.getOrigIP());
    assertNull("originating port should be null", remoteIp.getOrigPort());
    assertNull("originating protocol should be null", remoteIp.getOrigProto());
    assertEquals("wrong request IP", "192.168.1.1", remoteIp.getRequestIP());
    assertEquals("wrong request port", "80", remoteIp.getRequestPort().toString());
    assertEquals("wrong client IP", "192.168.1.1", remoteIp.getClientIP());
    assertEquals("wrong client port", "80", remoteIp.getClientPort().toString());
}
Also used : HashMap(java.util.HashMap) RemoteIP(com.zimbra.common.util.RemoteIP) MockHttpServletRequest(com.zimbra.cs.service.MockHttpServletRequest) URL(java.net.URL) Test(org.junit.Test)

Example 3 with MockHttpServletRequest

use of com.zimbra.cs.service.MockHttpServletRequest in project zm-mailbox by Zimbra.

the class TestRemoteIP method testNonTrustedIPLogString.

@Test
public void testNonTrustedIPLogString() throws Exception {
    HashMap<String, String> headers = new HashMap<String, String>();
    headers.put(RemoteIP.X_ORIGINATING_IP_HEADER, "172.16.150.11");
    headers.put(RemoteIP.X_ORIGINATING_PORT_HEADER, "8080");
    headers.put(RemoteIP.X_ORIGINATING_PROTOCOL_HEADER, "IMAP");
    MockHttpServletRequest req = new MockHttpServletRequest("test".getBytes("UTF-8"), new URL("http://localhost:7070/service/FooRequest"), "", 80, "10.10.1.1", headers);
    RemoteIP remoteIp = new RemoteIP(req, new RemoteIP.TrustedIPs(new String[] { "192.168.1.1" }));
    remoteIp.addToLoggingContext();
    String updatedLogContext = ZimbraLog.getContextString();
    // we should ignore X-Forwarded-XXX headers from non-trusted clients
    assertTrue(updatedLogContext.indexOf("oip=172.16.150.11") == -1);
    assertTrue(updatedLogContext.indexOf("oport=8080") == -1);
    assertTrue(updatedLogContext.indexOf("ip=10.10.1.1") > -1);
    assertTrue(updatedLogContext.indexOf("port=80") > -1);
    assertTrue(updatedLogContext.indexOf("oproto=IMAP") == -1);
}
Also used : HashMap(java.util.HashMap) RemoteIP(com.zimbra.common.util.RemoteIP) MockHttpServletRequest(com.zimbra.cs.service.MockHttpServletRequest) URL(java.net.URL) Test(org.junit.Test)

Example 4 with MockHttpServletRequest

use of com.zimbra.cs.service.MockHttpServletRequest in project zm-mailbox by Zimbra.

the class ZimbraServletTest method proxyTest.

@Ignore("until bug 60345 is fixed")
@Test
public void proxyTest() throws Exception {
    MockHttpServletRequest req = new MockHttpServletRequest("test".getBytes("UTF-8"), new URL("http://localhost:7070/user1" + uri), "");
    MockHttpServletResponse resp = new MockHttpServletResponse();
    ZimbraServlet.proxyServletRequest(req, resp, Provisioning.getInstance().getLocalServer(), uri, null);
}
Also used : MockHttpServletRequest(com.zimbra.cs.service.MockHttpServletRequest) URL(java.net.URL) MockHttpServletResponse(com.zimbra.cs.service.MockHttpServletResponse) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with MockHttpServletRequest

use of com.zimbra.cs.service.MockHttpServletRequest in project zm-mailbox by Zimbra.

the class GetContactBackupListTest method testGetContactBackupListJSON.

@Test
public void testGetContactBackupListJSON() throws Exception {
    Account acct = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
    Folder folder = mbox.createFolder(null, "Briefcase/ContactsBackup", new Folder.FolderOptions().setDefaultView(MailItem.Type.DOCUMENT));
    OperationContext octxt = new OperationContext(acct);
    // Upload the contacts backup file to ContactsBackup folder in briefcase
    mbox.createDocument(octxt, folder.getId(), "backup_dummy_test1.tgz", MimeConstants.CT_APPLICATION_ZIMBRA_DOC, "author", "description", new ByteArrayInputStream("dummy data".getBytes()));
    mbox.createDocument(octxt, folder.getId(), "backup_dummy_test2.tgz", MimeConstants.CT_APPLICATION_ZIMBRA_DOC, "author", "description", new ByteArrayInputStream("dummy data".getBytes()));
    Element request = new Element.JSONElement(MailConstants.E_GET_CONTACT_BACKUP_LIST_REQUEST);
    Map<String, Object> context = new HashMap<String, Object>();
    context.put(SoapEngine.ZIMBRA_CONTEXT, new ZimbraSoapContext(AuthProvider.getAuthToken(acct), acct.getId(), SoapProtocol.Soap12, SoapProtocol.SoapJS));
    context.put(SoapServlet.SERVLET_REQUEST, new MockHttpServletRequest("test".getBytes("UTF-8"), new URL("http://localhost:7070/service/FooRequest"), ""));
    context.put(SoapEngine.ZIMBRA_ENGINE, new MockSoapEngine(new MailService()));
    Element response = new GetContactBackupList().handle(request, context);
    String expectedResponse = "{\n" + "  \"backups\": [{\n" + "      \"backup\": [\n" + "        {\n" + "          \"_content\": \"backup_dummy_test1.tgz\"\n" + "        },\n" + "        {\n" + "          \"_content\": \"backup_dummy_test2.tgz\"\n" + "        }]\n" + "    }],\n" + "  \"_jsns\": \"urn:zimbraMail\"\n" + "}";
    Assert.assertEquals("GetContactBackupListResponse is not as expected", expectedResponse, response.prettyPrint());
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) MockSoapEngine(com.zimbra.soap.MockSoapEngine) HashMap(java.util.HashMap) MockHttpServletRequest(com.zimbra.cs.service.MockHttpServletRequest) Element(com.zimbra.common.soap.Element) Folder(com.zimbra.cs.mailbox.Folder) URL(java.net.URL) Mailbox(com.zimbra.cs.mailbox.Mailbox) ByteArrayInputStream(java.io.ByteArrayInputStream) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Test(org.junit.Test)

Aggregations

MockHttpServletRequest (com.zimbra.cs.service.MockHttpServletRequest)11 HashMap (java.util.HashMap)10 Test (org.junit.Test)10 URL (java.net.URL)9 RemoteIP (com.zimbra.common.util.RemoteIP)5 MockSoapEngine (com.zimbra.soap.MockSoapEngine)3 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)3 Element (com.zimbra.common.soap.Element)2 Account (com.zimbra.cs.account.Account)2 MockServer (com.zimbra.cs.account.MockServer)2 Server (com.zimbra.cs.account.Server)2 MockHttpServletResponse (com.zimbra.cs.service.MockHttpServletResponse)2 ArrayList (java.util.ArrayList)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 Folder (com.zimbra.cs.mailbox.Folder)1 Mailbox (com.zimbra.cs.mailbox.Mailbox)1 OperationContext (com.zimbra.cs.mailbox.OperationContext)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Ignore (org.junit.Ignore)1