package test.http.router.handler;

import com.firefly.$;
import com.firefly.client.http2.SimpleHTTPClient;
import com.firefly.client.http2.SimpleHTTPClientConfiguration;
import com.firefly.codec.http2.model.HttpVersion;
import com.firefly.server.http2.HTTP2ServerBuilder;
import org.junit.Assert;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;

import static org.hamcrest.Matchers.is;

/**
 * @author Pengtao Qiu
 */
public class TestPlaintextHTTP2ServerAndClient extends AbstractHTTPHandlerTest {

    @Test
    public void test() throws InterruptedException {
        int times = 10;
        CountDownLatch latch = new CountDownLatch(times);

        HTTP2ServerBuilder server = $.plaintextHTTP2Server();
        server.router().post("/plaintextHttp2").handler(ctx -> {
            System.out.println("Server: " +
                    ctx.getHttpVersion().asString() + "\r\n" +
                    ctx.getFields() +
                    ctx.getStringBody() +
                    "\r\n-----------------------\r\n");
            ctx.end("test plaintext http2");
        }).listen(host, port);

        SimpleHTTPClientConfiguration config = new SimpleHTTPClientConfiguration();
        config.setProtocol(HttpVersion.HTTP_2.asString());
        config.setPoolSize(1);
        SimpleHTTPClient client = new SimpleHTTPClient(config);
        for (int i = 0; i < times; i++) {
            client.post(uri + "/plaintextHttp2").body("post data").submit()
                  .thenAccept(res -> {
                      System.out.println("Client: " +
                              res.getStatus() + " " + res.getHttpVersion().asString() + "\r\n" +
                              res.getFields() +
                              res.getStringBody() +
                              "\r\n-----------------------\r\n");
                      Assert.assertThat(res.getStringBody(), is("test plaintext http2"));
                      latch.countDown();
                      System.out.println("Remain task: " + latch.getCount());
                  });
        }

        latch.await();
        System.out.println("Completed all tasks.");

        server.stop();
        client.stop();
    }
}

@@ -104,121 +104,8 @@
 nt;%0A
-import com.firefly.client.http2.SimpleHTTPClientConfiguration;%0Aimport com.firefly.codec.http2.model.HttpVersion;%0A
 impo
@@ -1021,219 +1021,41 @@
 ient
-Configuration config = new SimpleHTTPClientConfiguration();%0A        config.setProtocol(HttpVersion.HTTP_2.asString());%0A        config.setPoolSize(1);%0A        SimpleHTTPClient client = new Simple
+ client = $.plaintext
 HTTP
+2
 Client(
-config
 );%0A 

clean up codes