package com.wizzardo.http.websocket;

import com.wizzardo.http.ServerTest;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author: wizzardo
 * Date: 09.10.14
 */
public class WebSocketTest extends ServerTest {

    @Test
    public void echoTest() throws IOException, URISyntaxException, InterruptedException {
        handler = new WebSocketHandler() {
            @Override
            public void onMessage(WebSocketListener listener, Message message) {
                listener.sendMessage(message);
            }
        };

        AtomicReference<String> messageHolder = new AtomicReference<>();
        SimpleWebSocketClient client = new SimpleWebSocketClient("ws://localhost:" + getPort()) {
            @Override
            public void onMessage(Message message) {
                messageHolder.set(message.asString());
            }
        };
        client.send("foo bar");
        client.waitForMessage();
        Assert.assertEquals("foo bar", messageHolder.get());
    }

    @Test
    public void testFrames() throws IOException, URISyntaxException, InterruptedException {
        handler = new WebSocketHandler() {
            @Override
            public void onMessage(WebSocketListener listener, Message message) {
                listener.sendMessage(message);
            }
        };

        AtomicReference<String> messageHolder = new AtomicReference<>();
        SimpleWebSocketClient client = new SimpleWebSocketClient("ws://localhost:" + getPort()) {
            @Override
            public void onMessage(Message message) {
                messageHolder.set(message.asString());
            }
        };
        Message message = new Message().append("foo").append(" ").append("bar");
        client.send(message);
        client.waitForMessage();
        Assert.assertEquals("foo bar", messageHolder.get());
    }

}

@@ -68,16 +68,56 @@
 erTest;%0A
+import com.wizzardo.tools.security.MD5;%0A
 import o
@@ -213,32 +213,79 @@
 yntaxException;%0A
+import java.util.concurrent.ThreadLocalRandom;%0A
 import java.util
@@ -2054,10 +2054,1401 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void echoDifferentSizeTest() throws IOException, URISyntaxException, InterruptedException %7B%0A        handler = new WebSocketHandler() %7B%0A            @Override%0A            public void onMessage(WebSocketListener listener, Message message) %7B%0A                listener.sendMessage(message);%0A            %7D%0A        %7D;%0A%0A        AtomicReference%3CString%3E md5Holder = new AtomicReference%3C%3E();%0A        SimpleWebSocketClient client = new SimpleWebSocketClient(%22ws://localhost:%22 + getPort()) %7B%0A            @Override%0A            public void onMessage(Message message) %7B%0A                md5Holder.set(MD5.create().update(message.asBytes()).asString());%0A            %7D%0A        %7D;%0A        byte%5B%5D data = new byte%5B126%5D;%0A        ThreadLocalRandom.current().nextBytes(data);%0A        client.send(data);%0A        client.waitForMessage();%0A        Assert.assertEquals(MD5.create().update(data).asString(), md5Holder.get());%0A%0A        data = new byte%5B64 * 1024%5D;%0A        ThreadLocalRandom.current().nextBytes(data);%0A        client.send(data);%0A        client.waitForMessage();%0A        Assert.assertEquals(MD5.create().update(data).asString(), md5Holder.get());%0A%0A        data = new byte%5B1024 * 1024%5D;%0A        ThreadLocalRandom.current().nextBytes(data);%0A        client.send(data);%0A        client.waitForMessage();%0A        Assert.assertEquals(MD5.create().update(data).asString(), md5Holder.get());%0A    %7D%0A
 %7D%0A

test different frame sizes