package fi.nls.oskari.util;

import org.junit.Test;

import java.util.LinkedHashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;

/**
 * Created with IntelliJ IDEA.
 * User: SMAKINEN
 * Date: 12.6.2014
 * Time: 17:08
 * To change this template use File | Settings | File Templates.
 */
public class IOHelperTest {
    @Test
    public void testConstructUrl() throws Exception {
        String baseUrl = "/testing";
        assertEquals("Null params should return given base URL", baseUrl, IOHelper.constructUrl(baseUrl, null));

        // NOTE! using LinkedHashMap to ensure params order in result
        Map<String, String> params = new LinkedHashMap<String, String>();
        assertEquals("Empty params should return given base URL", baseUrl, IOHelper.constructUrl(baseUrl, params));

        params.put("test", "testing");
        assertEquals("Simple params should return expected URL", baseUrl + "?test=testing", IOHelper.constructUrl(baseUrl, params));

        baseUrl = "/testing?";
        assertEquals("BaseUrl with ending ? should return expected URL", baseUrl + "test=testing", IOHelper.constructUrl(baseUrl, params));

        baseUrl = "/testing?t=1";
        assertEquals("BaseUrl having existing queryString should return expected URL", baseUrl + "&test=testing", IOHelper.constructUrl(baseUrl, params));

        baseUrl = "/testing?t=1&";
        assertEquals("BaseUrl with ending & should return expected URL", baseUrl + "test=testing", IOHelper.constructUrl(baseUrl, params));

        params.put("t2", "3");
        assertEquals("Multiple params should return expected URL", baseUrl + "test=testing&t2=3", IOHelper.constructUrl(baseUrl, params));

        params.put("t3", "&&&");
        assertEquals("Problematic params should return expected encoded URL", baseUrl + "test=testing&t2=3&t3=%26%26%26", IOHelper.constructUrl(baseUrl, params));
    }

    @Test
    public void testFixPath() {
        assertEquals("Duplicate slashes should be removed", "http://testing/if/path/OK", IOHelper.fixPath("http://testing//if/path//OK"));
        assertEquals("Triple slashes should be duplicate slashes", "https://testing//if/path/OK", IOHelper.fixPath("https://testing///if/path//OK"));
    }
}

@@ -2228,11 +2228,1199 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testFormURLEncode() %7B%0A        assertEquals(%22null map should return an empty string%22, %22%22, IOHelper.formURLEncode(null));%0A%0A        Map%3CString, String%3E params = new LinkedHashMap%3CString, String%3E();%0A        assertEquals(%22Empty map should return an empty string%22, %22%22, IOHelper.formURLEncode(params));%0A%0A        params.put(%22key1%22, %22foo%22);%0A        assertEquals(%22Key and value should be separated with a '='%22, %22key1=foo%22, IOHelper.formURLEncode(params));%0A%0A        params.put(%22key2%22, %22bar%22);%0A        assertEquals(%22Values should be separated with a '&'%22, %22key1=foo&key2=bar%22, IOHelper.formURLEncode(params));%0A        params.remove(%22key2%22);%0A%0A        params.put(%22key1%22, %22baz+qux%22);%0A        assertEquals(%22Values should be URL encoded%22, %22key1=baz%252Bqux%22, IOHelper.formURLEncode(params));%0A        params.remove(%22key1%22);%0A%0A        params.put(%22foo+bar%22, %22baz+qux%22);%0A        assertEquals(%22Keys should be URL encoded%22, %22foo%252Bbar=baz%252Bqux%22, IOHelper.formURLEncode(params));%0A        params.remove(%22foo+bar%22);%0A%0A        params.put(%22key1%22, %22baz qux%22);%0A        assertEquals(%22Space characters are replaced by '+' in form encoding%22, %22key1=baz+qux%22, IOHelper.formURLEncode(params));%0A    %7D
 %0A%7D%0A

Add tests for IOHelper.formURLEncode