package net.zephyrizing.http_server_test;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

import net.zephyrizing.http_server.HttpInputStream;

import org.junit.Ignore;
import org.junit.Test;

import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.*;

public class HttpInputStreamTest {

    @Test(expected=IOException.class)
    public void throwsWhenNoNewlineAvailable() throws Exception {
        String content = "hello";
        HttpInputStream s =
            new HttpInputStream(
                5,
                new BufferedInputStream(
                    new ByteArrayInputStream(content.getBytes())));

        s.readLine();
    }

    @Test
    public void canReadNetworkLines() throws Exception {
        String content = "hello\r\nworld\r\n";
        HttpInputStream s =
            new HttpInputStream(
                5,
                new BufferedInputStream(
                    new ByteArrayInputStream(content.getBytes())));

        assertThat(s.readLine(), equalTo("hello"));
        assertThat(s.readLine(), equalTo("world"));
    }

    @Test
    public void canReadVaryingLengthLines() throws Exception {
        String content = "short\r\ntoolong\r\n";
        HttpInputStream s =
            new HttpInputStream(
                7,
                new BufferedInputStream(
                    new ByteArrayInputStream(content.getBytes())));

        assertThat(s.readLine(), equalTo("short"));
        assertThat(s.readLine(), equalTo("toolong"));
    }

    @Test
    public void canReadLinesTwiceAsBigAsBuffer() throws Exception {
        String content = "muchtoolong!\r\n";
        HttpInputStream s =
            new HttpInputStream(
                7,
                new BufferedInputStream(
                    new ByteArrayInputStream(content.getBytes())));

        assertThat(s.readLine(), equalTo("muchtoolong!"));
    }

    @Test
    public void returnsNullOnEmptyLineRead() throws Exception {
        String content = "line1\r\nline2\r\nline3\r\n\r\n";
        HttpInputStream s =
            new HttpInputStream(
                12,
                new BufferedInputStream(
                    new ByteArrayInputStream(content.getBytes())));

        assertThat(s.readLine(), equalTo("line1"));
        assertThat(s.readLine(), equalTo("line2"));
        assertThat(s.readLine(), equalTo("line3"));
        assertThat(s.readLine(), nullValue());
    }

    @Test
    public void canReadCRAndLFInSeparateBuffers() throws Exception {
        String content = "line\r\notherstuff\r\n";
        HttpInputStream s =
            new HttpInputStream(
                5,
                new BufferedInputStream(
                    new ByteArrayInputStream(content.getBytes())));

        assertThat(s.readLine(), equalTo("line"));
        assertThat(s.readLine(), equalTo("otherstuff"));
    }

    @Test
    public void stopsReadingByLinesAfterBlankLine() throws Exception {
        String content = "line\r\n\r\notherthings";
        HttpInputStream s =
            new HttpInputStream(
                12,
                new BufferedInputStream(
                    new ByteArrayInputStream(content.getBytes())));

        assertThat(s.readLine(), equalTo("line"));
        assertThat(s.readLine(), nullValue());
        assertThat(s.readLine(), nullValue());
        assertThat(s.readLine(), nullValue());

        String bodyContent = "otherthings";
        ByteBuffer bodyBytes = ByteBuffer.wrap(bodyContent.getBytes());
        assertThat(s.readBody(bodyContent.length()), equalTo(bodyBytes));
    }

    @Test
    public void cantReadBodyUntilBlankLineRead() throws Exception {
        String content = "line\r\n\r\notherthings";
        HttpInputStream s =
            new HttpInputStream(
                12,
                new BufferedInputStream(
                    new ByteArrayInputStream(content.getBytes())));

        assertThat(s.readBody(1), nullValue());
        assertThat(s.readBody(5), nullValue());
        assertThat(s.readBody(1000), nullValue());
    }
}

@@ -1155,32 +1155,455 @@
 orld%22));%0A    %7D%0A%0A
+    @Test%0A    public void canReadWithSingleCharBuffer() throws Exception %7B%0A        String content = %22hello%5Cr%5Cnworld%5Cr%5Cn%22;%0A        HttpInputStream s =%0A            new HttpInputStream(%0A                1,%0A                new BufferedInputStream(%0A                    new ByteArrayInputStream(content.getBytes())));%0A%0A        assertThat(s.readLine(), equalTo(%22hello%22));%0A        assertThat(s.readLine(), equalTo(%22world%22));%0A    %7D%0A%0A
     @Test%0A    pu

Add a test for using single byte buffer