// Copyright 2014, Kevin Ko <kevin@faveset.com>

package com.faveset.khttp;

import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class HttpConnectionParserTest {
    private static final Charset US_ASCII_CHARSET = Charset.forName("US-ASCII");

    private void compare(ByteBuffer buf, String v) {
        String dataStr = new String(buf.array(), buf.position(), buf.remaining(), US_ASCII_CHARSET);
        assertTrue(dataStr.equals(v));
    }

    @Test
    public void testParseLine() {
        String testStr = "line1\nline2\r\n\nline3";
        ByteBuffer buf = ByteBuffer.wrap(testStr.getBytes(US_ASCII_CHARSET));

        ByteBuffer result = HttpConnectionParser.parseLine(buf);
        compare(result, "line1");

        result = HttpConnectionParser.parseLine(buf);
        compare(result, "line2\r");

        result = HttpConnectionParser.parseLine(buf);
        compare(result, "");

        result = HttpConnectionParser.parseLine(buf);
        assertEquals(result, null);
        ByteBuffer cmp = buf.duplicate();
        cmp.flip();
        compare(cmp, "line3");

        // Now, try appending.
        String appendStr = "line3b\nline4";
        ByteBuffer appendBuf = ByteBuffer.wrap(appendStr.getBytes(US_ASCII_CHARSET));
        buf.put(appendBuf);
        buf.flip();

        // Compare the appended buffer.
        result = HttpConnectionParser.parseLine(buf);
        compare(result, "line3line3b");

        result = HttpConnectionParser.parseLine(buf);
        assertEquals(result, null);
        cmp = buf.duplicate();
        cmp.flip();
        compare(cmp, "line4");
    }

    @Test(expected=BufferOverflowException.class)
    public void testParseLineOverflow() {
        String testStr = "line1";
        ByteBuffer buf = ByteBuffer.wrap(testStr.getBytes(US_ASCII_CHARSET));
        // This should throw an exception, since we assume that buf is of fixed
        // size and nothing can be appended to it to lead to a newline.
        HttpConnectionParser.parseLine(buf);
    }
}

@@ -2286,11 +2286,689 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testParseWord() %7B%0A        String testStr = %22%5Cn%5Cr %5Ctword1 word2  word3%22;%0A        ByteBuffer buf = ByteBuffer.wrap(testStr.getBytes(US_ASCII_CHARSET));%0A        assertEquals(HttpConnectionParser.parseWord(buf), %22word1%22);%0A        assertEquals(HttpConnectionParser.parseWord(buf), %22word2%22);%0A        assertEquals(HttpConnectionParser.parseWord(buf), %22word3%22);%0A        assertEquals(HttpConnectionParser.parseWord(buf), %22%22);%0A%0A        testStr = %22word1%5Cn%22;%0A        buf = ByteBuffer.wrap(testStr.getBytes(US_ASCII_CHARSET));%0A        assertEquals(HttpConnectionParser.parseWord(buf), %22word1%22);%0A        assertEquals(HttpConnectionParser.parseWord(buf), %22%22);%0A    %7D
 %0A%7D%0A

Add parseWord test.