Search in sources :

Example 31 with Request

use of com.tvd12.ezyhttp.client.request.Request in project ezyfox-server by youngmonkeys.

the class EzyHandShakeControllerTest method test.

@Test
public void test() {
    EzyServerContext ctx = newServerContext();
    EzySession session = newSession();
    EzyArray data = newHandShakeData();
    EzyHandshakeController controller = new EzyHandshakeController();
    EzySimpleHandshakeRequest request = new EzySimpleHandshakeRequest();
    request.deserializeParams(data);
    request.setSession(session);
    controller.handle(ctx, request);
}
Also used : EzyServerContext(com.tvd12.ezyfoxserver.context.EzyServerContext) EzyArray(com.tvd12.ezyfox.entity.EzyArray) EzySimpleHandshakeRequest(com.tvd12.ezyfoxserver.request.EzySimpleHandshakeRequest) EzyHandshakeController(com.tvd12.ezyfoxserver.controller.EzyHandshakeController) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) Test(org.testng.annotations.Test)

Example 32 with Request

use of com.tvd12.ezyhttp.client.request.Request in project ezyfox-server by youngmonkeys.

the class EzyHandShakeControllerTest method handleSocketSSLButSessionKeyNotNullTest.

@Test
public void handleSocketSSLButSessionKeyNotNullTest() {
    // given
    EzyHandshakeController sut = new EzyHandshakeController();
    byte[] sessionKey = RandomUtil.randomShortAlphabetString().getBytes();
    byte[] encryptedSessionKey = RandomUtil.randomShortAlphabetString().getBytes();
    EzyServerContext serverContext = mock(EzyServerContext.class);
    doAnswer(it -> {
        EzyHandshakeEvent event = it.getArgumentAt(1, EzyHandshakeEvent.class);
        event.setSessionKey(sessionKey);
        event.setEncryptedSessionKey(encryptedSessionKey);
        return null;
    }).when(serverContext).handleEvent(any(EzyEventType.class), any(EzyHandshakeEvent.class));
    EzyHandShakeRequest request = mock(EzyHandShakeRequest.class);
    EzyHandshakeParams params = mock(EzyHandshakeParams.class);
    when(request.getParams()).thenReturn(params);
    EzySession session = spy(EzyAbstractSession.class);
    when(session.getConnectionType()).thenReturn(EzyConnectionType.SOCKET);
    when(request.getSession()).thenReturn(session);
    EzyServer server = mock(EzyServer.class);
    EzySettings settings = mock(EzySettings.class);
    EzySocketSetting socketSetting = mock(EzySocketSetting.class);
    when(settings.getSocket()).thenReturn(socketSetting);
    when(socketSetting.isSslActive()).thenReturn(true);
    when(serverContext.getServer()).thenReturn(server);
    when(server.getSettings()).thenReturn(settings);
    String clientId = RandomUtil.randomShortHexString();
    String clientType = RandomUtil.randomShortAlphabetString();
    String clientVersion = RandomUtil.randomShortAlphabetString();
    String reconnectToken = RandomUtil.randomShortHexString();
    KeyPair keyPair = EzyKeysGenerator.builder().build().generate();
    byte[] clientKey = keyPair.getPublic().getEncoded();
    when(params.getClientId()).thenReturn(clientId);
    when(params.getClientKey()).thenReturn(clientKey);
    when(params.getClientType()).thenReturn(clientType);
    when(params.getClientVersion()).thenReturn(clientVersion);
    when(params.getReconnectToken()).thenReturn(reconnectToken);
    when(params.isEnableEncryption()).thenReturn(true);
    // when
    sut.handle(serverContext, request);
    // then
    verify(serverContext, times(1)).handleEvent(any(EzyEventType.class), any(EzyHandshakeEvent.class));
    verify(serverContext, times(1)).send(any(com.tvd12.ezyfoxserver.response.EzyResponse.class), any(EzySession.class), any(boolean.class));
    verify(session, times(1)).setClientId(clientId);
    verify(session, times(1)).setClientKey(clientKey);
    verify(session, times(1)).setClientType(clientType);
    verify(session, times(1)).setClientVersion(clientVersion);
    verify(session, times(1)).setSessionKey(sessionKey);
}
Also used : EzyHandshakeEvent(com.tvd12.ezyfoxserver.event.EzyHandshakeEvent) KeyPair(java.security.KeyPair) EzySocketSetting(com.tvd12.ezyfoxserver.setting.EzySocketSetting) EzyServerContext(com.tvd12.ezyfoxserver.context.EzyServerContext) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) EzyHandShakeRequest(com.tvd12.ezyfoxserver.request.EzyHandShakeRequest) EzyHandshakeParams(com.tvd12.ezyfoxserver.request.EzyHandshakeParams) EzyEventType(com.tvd12.ezyfoxserver.constant.EzyEventType) EzyHandshakeController(com.tvd12.ezyfoxserver.controller.EzyHandshakeController) EzyServer(com.tvd12.ezyfoxserver.EzyServer) EzySettings(com.tvd12.ezyfoxserver.setting.EzySettings) Test(org.testng.annotations.Test)

Example 33 with Request

use of com.tvd12.ezyhttp.client.request.Request in project ezyfox-server by youngmonkeys.

the class EzyHandShakeControllerTest method handleSocketSSLButInvalidClientKeyEmptyTest.

@Test
public void handleSocketSSLButInvalidClientKeyEmptyTest() {
    // given
    EzyHandshakeController sut = new EzyHandshakeController();
    EzyServerContext serverContext = mock(EzyServerContext.class);
    EzyHandShakeRequest request = mock(EzyHandShakeRequest.class);
    EzyHandshakeParams params = mock(EzyHandshakeParams.class);
    when(request.getParams()).thenReturn(params);
    EzySession session = spy(EzyAbstractSession.class);
    when(session.getConnectionType()).thenReturn(EzyConnectionType.SOCKET);
    when(request.getSession()).thenReturn(session);
    EzyServer server = mock(EzyServer.class);
    EzySettings settings = mock(EzySettings.class);
    EzySocketSetting socketSetting = mock(EzySocketSetting.class);
    when(settings.getSocket()).thenReturn(socketSetting);
    when(socketSetting.isSslActive()).thenReturn(true);
    when(serverContext.getServer()).thenReturn(server);
    when(server.getSettings()).thenReturn(settings);
    String clientId = RandomUtil.randomShortHexString();
    String clientType = RandomUtil.randomShortAlphabetString();
    String clientVersion = RandomUtil.randomShortAlphabetString();
    String reconnectToken = RandomUtil.randomShortHexString();
    byte[] clientKey = new byte[] { 1, 2, 3 };
    when(params.getClientId()).thenReturn(clientId);
    when(params.getClientKey()).thenReturn(clientKey);
    when(params.getClientType()).thenReturn(clientType);
    when(params.getClientVersion()).thenReturn(clientVersion);
    when(params.getReconnectToken()).thenReturn(reconnectToken);
    when(params.isEnableEncryption()).thenReturn(true);
    // when
    sut.handle(serverContext, request);
    // then
    verify(session, times(1)).setClientId(clientId);
    verify(session, times(1)).setClientKey(clientKey);
    verify(session, times(1)).setClientType(clientType);
    verify(session, times(1)).setClientVersion(clientVersion);
    verify(session, times(1)).setSessionKey(any(byte[].class));
}
Also used : EzyHandShakeRequest(com.tvd12.ezyfoxserver.request.EzyHandShakeRequest) EzyHandshakeParams(com.tvd12.ezyfoxserver.request.EzyHandshakeParams) EzySocketSetting(com.tvd12.ezyfoxserver.setting.EzySocketSetting) EzyServerContext(com.tvd12.ezyfoxserver.context.EzyServerContext) EzyHandshakeController(com.tvd12.ezyfoxserver.controller.EzyHandshakeController) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) EzyServer(com.tvd12.ezyfoxserver.EzyServer) EzySettings(com.tvd12.ezyfoxserver.setting.EzySettings) Test(org.testng.annotations.Test)

Example 34 with Request

use of com.tvd12.ezyhttp.client.request.Request in project ezyfox-server by youngmonkeys.

the class EzyHandShakeControllerTest method handleSocketSSLTest.

@Test
public void handleSocketSSLTest() {
    // given
    EzyHandshakeController sut = new EzyHandshakeController();
    EzyServerContext serverContext = mock(EzyServerContext.class);
    EzyHandShakeRequest request = mock(EzyHandShakeRequest.class);
    EzyHandshakeParams params = mock(EzyHandshakeParams.class);
    when(request.getParams()).thenReturn(params);
    EzySession session = spy(EzyAbstractSession.class);
    when(session.getConnectionType()).thenReturn(EzyConnectionType.SOCKET);
    when(request.getSession()).thenReturn(session);
    EzyServer server = mock(EzyServer.class);
    EzySettings settings = mock(EzySettings.class);
    EzySocketSetting socketSetting = mock(EzySocketSetting.class);
    when(settings.getSocket()).thenReturn(socketSetting);
    when(socketSetting.isSslActive()).thenReturn(true);
    when(serverContext.getServer()).thenReturn(server);
    when(server.getSettings()).thenReturn(settings);
    String clientId = RandomUtil.randomShortHexString();
    String clientType = RandomUtil.randomShortAlphabetString();
    String clientVersion = RandomUtil.randomShortAlphabetString();
    String reconnectToken = RandomUtil.randomShortHexString();
    KeyPair keyPair = EzyKeysGenerator.builder().build().generate();
    byte[] clientKey = keyPair.getPublic().getEncoded();
    when(params.getClientId()).thenReturn(clientId);
    when(params.getClientKey()).thenReturn(clientKey);
    when(params.getClientType()).thenReturn(clientType);
    when(params.getClientVersion()).thenReturn(clientVersion);
    when(params.getReconnectToken()).thenReturn(reconnectToken);
    when(params.isEnableEncryption()).thenReturn(true);
    // when
    sut.handle(serverContext, request);
    // then
    verify(session, times(1)).setClientId(clientId);
    verify(session, times(1)).setClientKey(clientKey);
    verify(session, times(1)).setClientType(clientType);
    verify(session, times(1)).setClientVersion(clientVersion);
    verify(session, times(1)).setSessionKey(any(byte[].class));
    Asserts.assertNotNull(session.getSessionKey());
}
Also used : EzyHandShakeRequest(com.tvd12.ezyfoxserver.request.EzyHandShakeRequest) EzyHandshakeParams(com.tvd12.ezyfoxserver.request.EzyHandshakeParams) KeyPair(java.security.KeyPair) EzySocketSetting(com.tvd12.ezyfoxserver.setting.EzySocketSetting) EzyServerContext(com.tvd12.ezyfoxserver.context.EzyServerContext) EzyHandshakeController(com.tvd12.ezyfoxserver.controller.EzyHandshakeController) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) EzyServer(com.tvd12.ezyfoxserver.EzyServer) EzySettings(com.tvd12.ezyfoxserver.setting.EzySettings) Test(org.testng.annotations.Test)

Example 35 with Request

use of com.tvd12.ezyhttp.client.request.Request in project ezyfox-server by youngmonkeys.

the class EzyPluginInfoControllerTest method test.

@Test
public void test() {
    EzyPluginInfoController controller = new EzyPluginInfoController();
    EzyServerContext serverContext = mock(EzyServerContext.class);
    EzyZoneContext zoneContext = mock(EzyZoneContext.class);
    when(serverContext.getZoneContext(1)).thenReturn(zoneContext);
    EzySimplePluginInfoRequest request = new EzySimplePluginInfoRequest();
    EzyAbstractSession session = spy(EzyAbstractSession.class);
    EzySimpleUser user = new EzySimpleUser();
    user.setZoneId(1);
    request.setSession(session);
    request.setUser(user);
    EzyArray data = EzyEntityFactory.newArrayBuilder().append("test").build();
    request.deserializeParams(data);
    controller.handle(serverContext, request);
    EzyPluginContext pluginContext = mock(EzyPluginContext.class);
    EzySimplePlugin plugin = new EzySimplePlugin();
    EzySimplePluginSetting pluginSetting = new EzySimplePluginSetting();
    plugin.setSetting(pluginSetting);
    when(pluginContext.getPlugin()).thenReturn(plugin);
    when(zoneContext.getPluginContext("test")).thenReturn(pluginContext);
    controller.handle(serverContext, request);
}
Also used : EzySimplePluginInfoRequest(com.tvd12.ezyfoxserver.request.EzySimplePluginInfoRequest) EzySimpleUser(com.tvd12.ezyfoxserver.entity.EzySimpleUser) EzyZoneContext(com.tvd12.ezyfoxserver.context.EzyZoneContext) EzyAbstractSession(com.tvd12.ezyfoxserver.entity.EzyAbstractSession) EzyServerContext(com.tvd12.ezyfoxserver.context.EzyServerContext) EzyPluginContext(com.tvd12.ezyfoxserver.context.EzyPluginContext) EzyPluginInfoController(com.tvd12.ezyfoxserver.controller.EzyPluginInfoController) EzyArray(com.tvd12.ezyfox.entity.EzyArray) EzySimplePlugin(com.tvd12.ezyfoxserver.EzySimplePlugin) EzySimplePluginSetting(com.tvd12.ezyfoxserver.setting.EzySimplePluginSetting) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Aggregations

Test (org.testng.annotations.Test)128 HttpServletResponse (javax.servlet.http.HttpServletResponse)48 HttpServletRequest (javax.servlet.http.HttpServletRequest)45 EzySession (com.tvd12.ezyfoxserver.entity.EzySession)37 BeforeTest (org.testng.annotations.BeforeTest)36 BaseTest (com.tvd12.test.base.BaseTest)31 EzyArray (com.tvd12.ezyfox.entity.EzyArray)30 ComponentManager (com.tvd12.ezyhttp.server.core.manager.ComponentManager)29 BlockingServlet (com.tvd12.ezyhttp.server.core.servlet.BlockingServlet)29 ToString (lombok.ToString)29 RequestURI (com.tvd12.ezyhttp.server.core.request.RequestURI)27 ServletOutputStream (javax.servlet.ServletOutputStream)25 EzyServerContext (com.tvd12.ezyfoxserver.context.EzyServerContext)24 RequestHandlerManager (com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager)24 Cookie (javax.servlet.http.Cookie)24 HttpClientProxy (com.tvd12.ezyhttp.client.HttpClientProxy)22 RequestCookie (com.tvd12.ezyhttp.server.core.annotation.RequestCookie)22 EzyZoneContext (com.tvd12.ezyfoxserver.context.EzyZoneContext)21 HttpClient (com.tvd12.ezyhttp.client.HttpClient)21 PostRequest (com.tvd12.ezyhttp.client.request.PostRequest)21