/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Yegor Bugayenko
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.takes.rq;

import java.io.IOException;
import java.util.Arrays;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

/**
 * Test case for {@link RqForm}.
 * @author Yegor Bugayenko (yegor@teamed.io)
 * @version $Id$
 * @since 0.9
 */
public final class RqFormTest {

    /**
     * RqForm can parse body.
     * @throws IOException If some problem inside
     */
    @Test
    public void parsesHttpBody() throws IOException {
        final String body = "alpha=a+b+c&beta=%20Yes%20";
        final RqForm req = new RqForm.Base(
            new RqBuffered(
                new RqFake(
                    Arrays.asList(
                        "GET /h?a=3",
                        "Host: www.example.com",
                        String.format(
                            "Content-Length: %d",
                            body.getBytes().length
                        )
                    ),
                    body
                )
            )
        );
        MatcherAssert.assertThat(
            req.param("beta"),
            Matchers.hasItem(" Yes ")
        );
        MatcherAssert.assertThat(
            req.names(),
            Matchers.hasItem("alpha")
        );
    }

    /**
     * Returns always same instances (Cache).
     * @throws IOException if fails
     */
    @Test
    public void sameInstance() throws IOException {
        final RqForm req = new RqForm.Base(
            new RqBuffered(
                new RqFake(
                    Arrays.asList(
                        "GET /path?a=3",
                        "Host: www.example2.com"
                    ),
                    "alpha=a+b+c&beta=%20No%20"
                )
            )
        );
        MatcherAssert.assertThat(
            req.names() == req.names(),
            Matchers.is(Boolean.TRUE)
        );
    }

    /**
     * RqForm.Fake can create fake forms with parameters list.
     * @throws Exception If something goes wrong.
     */
    @Test
    public void createsFormRequestWithParams() throws Exception {
        final String key = "key";
        final String akey = "anotherkey";
        final String value = "value";
        final String avalue = "a&b";
        final String aavalue = "againanothervalue";
        final RqForm req = new RqForm.Fake(
            new RqFake(
                Arrays.asList(
                    "GET /form",
                    "Host: www.example5.com",
                    "Content-Length: 49"
                ),
                ""
            ),
            key, value,
            key, avalue,
            akey, aavalue
        );
        MatcherAssert.assertThat(
            req.param(key),
            Matchers.hasItems(value, avalue)
        );
        MatcherAssert.assertThat(
            req.param(akey),
            Matchers.hasItems(aavalue)
        );
        MatcherAssert.assertThat(
            req.names(),
            Matchers.hasItems(key, akey)
        );
    }

    /**
     * RqForm.Fake throws an IllegalArgumentException when invoked with
     * wrong number of parameters.
     * @throws Exception If something goes wrong.
     */
    @Test(expected = IllegalArgumentException.class)
    public void throwsExceptionWhenNotCorrectlyCreated()
        throws Exception {
        new RqForm.Fake(
            new RqFake(),
            "param"
        );
    }
}

@@ -1458,16 +1458,135 @@
 Test %7B%0A%0A
+    /**%0A     * Content-Length header template.%0A     */%0A    private static final String HEADER = %22Content-Length: %25d%22;%0A%0A
     /**%0A
@@ -2084,36 +2084,22 @@
         
-%22Content-Length: %25d%22
+HEADER
 ,%0A      
@@ -3518,32 +3518,112 @@
 nanothervalue%22;%0A
+        final String body = %22key=value&key=a%2526b&anotherkey=againanothervalue%22;%0A
         final Rq
@@ -3642,32 +3642,32 @@
 ew RqForm.Fake(%0A
-
             new 
@@ -3808,28 +3808,114 @@
     
-%22Content-Length: 49%22
+String.format(%0A                        HEADER,%0A                        body.length()%0A                    )
 %0A   

calculate content length