package test.http.router.handler.body;

import com.firefly.$;
import com.firefly.codec.http2.model.HttpHeader;
import com.firefly.codec.http2.model.HttpStatus;
import com.firefly.codec.http2.stream.HTTPOutputStream;
import com.firefly.server.http2.HTTP2ServerBuilder;
import com.firefly.utils.StringUtils;
import com.firefly.utils.lang.URIUtils;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.concurrent.Phaser;

import static org.hamcrest.Matchers.is;

/**
 * @author Pengtao Qiu
 */
public class TestHTTPBodyHandler {

    @Test
    public void testPostData() {
        String host = "localhost";
        int port = 8083;
        StringBuilder uri = URIUtils.newURIBuilder("http", host, port);
        System.out.println(uri);
        Phaser phaser = new Phaser(3);

        HTTP2ServerBuilder httpServer = $.httpServer();
        httpServer.router().post("/data").handler(ctx -> {
            System.out.println(ctx.getStringBody());
            Assert.assertThat(ctx.getStringBody(), is("test post data"));
            ctx.end("server received data");
            phaser.arrive();
        }).listen(host, port);

        $.httpClient().post(uri + "/data").body("test post data").submit()
         .thenAccept(res -> {
             System.out.println(res.getStringBody());
             Assert.assertThat(res.getStatus(), is(HttpStatus.OK_200));
             Assert.assertThat(res.getStringBody(), is("server received data"));
             phaser.arrive();
         });

        phaser.arriveAndAwaitAdvance();
        httpServer.stop();
        $.httpClient().stop();
    }

    @Test
    public void testPostBigData() {
        String host = "localhost";
        int port = 8084;
        StringBuilder uri = URIUtils.newURIBuilder("http", host, port);
        System.out.println(uri);
        Phaser phaser = new Phaser(3);

        StringBuilder bigData = new StringBuilder();
        int dataSize = 1024 * 1024;
        for (int i = 0; i < dataSize; i++) {
            bigData.append(i);
        }
        byte[] data = StringUtils.getBytes(bigData.toString());
        System.out.println("data len: " + data.length);

        HTTP2ServerBuilder httpServer = $.httpServer();
        httpServer.router().post("/bigData").handler(ctx -> {
            System.out.println("big data size: " + ctx.getContentLength());
            Assert.assertThat((int) ctx.getContentLength(), is(data.length));
            ctx.end("server received big data");
            phaser.arrive();
        }).listen(host, port);

        $.httpClient().post(uri + "/bigData").put(HttpHeader.CONTENT_LENGTH, data.length + "")
         .output(output -> {
             try (HTTPOutputStream out = output) {
                 out.write(ByteBuffer.wrap(data));
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }).submit()
         .thenAccept(res -> {
             Assert.assertThat(res.getStatus(), is(HttpStatus.OK_200));
             Assert.assertThat(res.getStringBody(), is("server received big data"));
             phaser.arrive();
         });

        phaser.arriveAndAwaitAdvance();
        httpServer.stop();
        $.httpClient().stop();
    }
}

@@ -157,64 +157,8 @@
 us;%0A
-import com.firefly.codec.http2.stream.HTTPOutputStream;%0A
 impo
@@ -336,36 +336,8 @@
 t;%0A%0A
-import java.io.IOException;%0A
 impo
@@ -2259,16 +2259,24 @@
 rintln(%22
+receive 
 big data
@@ -2307,24 +2307,24 @@
 tLength());%0A
-
             
@@ -2385,24 +2385,162 @@
 a.length));%0A
+            Assert.assertThat($.io.toString(ctx.getInputStream()), is(bigData.toString()));%0A            $.io.close(ctx.getInputStream());%0A
             
@@ -2745,99 +2745,8 @@
     
-.output(output -%3E %7B%0A             try (HTTPOutputStream out = output) %7B%0A                 out
 .wri
@@ -2770,19 +2770,17 @@
 p(data))
-;%0A 
+%0A
         
@@ -2784,101 +2784,8 @@
     
-   %7D catch (IOException e) %7B%0A                 e.printStackTrace();%0A             %7D%0A         %7D)
 .sub

add body handler test case