package info.vividcode.util.oauth;

import static org.junit.Assert.*;

import java.net.MalformedURLException;
import java.security.GeneralSecurityException;
import java.util.Arrays;

import org.junit.Test;

public class OAuthRequestHelperTest {

    @Test
    public void testGetNonceString() {
        String nonceStr;

        nonceStr = OAuthRequestHelper.getNonceString();
        assertEquals("Default length of nonce is 16", 16, nonceStr.length());

        nonceStr = OAuthRequestHelper.getNonceString(1);
        assertEquals(1, nonceStr.length());

        nonceStr = OAuthRequestHelper.getNonceString(5);
        assertEquals(5, nonceStr.length());

        nonceStr = OAuthRequestHelper.getNonceString(200);
        assertEquals(200, nonceStr.length());
    }

    @Test( expected = IllegalArgumentException.class )
    public void testGetNonceStringWithZeroLength() {
        OAuthRequestHelper.getNonceString(0);
    }

    @Test( expected = IllegalArgumentException.class )
    public void testGetNonceStringWithNegativeLength() {
        OAuthRequestHelper.getNonceString(-1);
    }

    @Test
    public void testOAuthRequestHelper() throws GeneralSecurityException, MalformedURLException {
        String url = "https://photos.example.com/oauth/initiate";
        String method = "POST";
        String consumerKey    = "TEST_KEY==";
        String consumerSecret = "TEST_SECRET==";
        String tokenSecret = "";
        String secrets = OAuthEncoder.encode(consumerSecret) + "&" + OAuthEncoder.encode(tokenSecret);
        OAuthRequestHelper.ParamList paramList = new OAuthRequestHelper.ParamList(
              new String[][]{
                { "oauth_consumer_key", consumerKey },
                { "oauth_signature_method", "HMAC-SHA1" },
                { "oauth_nonce", "faQeA8ae22" },
                { "oauth_timestamp", "1368908614" },
                { "oauth_version", "1.0" },
                { "oauth_callback", "oob" },
              } );
        OAuthRequestHelper helper = new OAuthRequestHelper( url, method, secrets, paramList, null, null );

        // Authorization header field
        String authHeaderStr = helper.getAuthorizationHeaderString("rlm");
        final String PREF = "OAuth realm=\"rlm\",";
        authHeaderStr.startsWith(PREF);
        String s = authHeaderStr.substring(PREF.length(), authHeaderStr.length());
        String[] ss = s.split("\\s*,\\s*");
        for (int i = 0; i < ss.length; ++i) ss[i] = ss[i].trim();
        Arrays.sort(ss);
        assertArrayEquals(new String[] {
                "oauth_callback=\"oob\"",
                "oauth_consumer_key=\"TEST_KEY%3D%3D\"",
                "oauth_nonce=\"faQeA8ae22\"",
                "oauth_signature=\"e%2FDKfY2wLhBB9yqj5WtoJv2Ff6w%3D\"",
                "oauth_signature_method=\"HMAC-SHA1\"",
                "oauth_timestamp=\"1368908614\"",
                "oauth_version=\"1.0\"",
        }, ss);

        // URI
        String urlNotIncludingAuthQuery = helper.getUrlStringIncludeQueryParams();
        assertEquals(url, urlNotIncludingAuthQuery);
    }

}

@@ -3069,16 +3069,1564 @@
 hQuery);
+%0A%0A        // URI including OAuth parameters%0A        %7B%0A            String urlIncludingAuthQuery = helper.getUrlStringIncludeQueryParams(true);%0A            assertTrue(%22URL %E3%81%AE%E3%82%AF%E3%82%A8%E3%83%AA%E3%83%91%E3%83%A9%E3%83%A1%E3%83%BC%E3%82%BF%E7%9B%B4%E5%89%8D%E3%81%BE%E3%81%A7%22, urlIncludingAuthQuery.startsWith(url + %22?%22));%0A            String queryParamsStr = urlIncludingAuthQuery.substring((url + %22?%22).length());%0A            String%5B%5D params = queryParamsStr.split(%22&%22);%0A            Arrays.sort(params);%0A            assertArrayEquals(new String%5B%5D %7B%0A                    %22oauth_callback=oob%22,%0A                    %22oauth_consumer_key=TEST_KEY%253D%253D%22,%0A                    %22oauth_nonce=faQeA8ae22%22,%0A                    %22oauth_signature=e%252FDKfY2wLhBB9yqj5WtoJv2Ff6w%253D%22,%0A                    %22oauth_signature_method=HMAC-SHA1%22,%0A                    %22oauth_timestamp=1368908614%22,%0A                    %22oauth_version=1.0%22,%0A            %7D, params);%0A        %7D%0A%0A        // HTTP Request body including OAuth parameters%0A        %7B%0A            String reqBodyIncludingAuthQuery = helper.getRequestBodyString(true);%0A            String%5B%5D params = reqBodyIncludingAuthQuery.split(%22&%22);%0A            Arrays.sort(params);%0A            assertArrayEquals(new String%5B%5D %7B%0A                    %22oauth_callback=oob%22,%0A                    %22oauth_consumer_key=TEST_KEY%253D%253D%22,%0A                    %22oauth_nonce=faQeA8ae22%22,%0A                    %22oauth_signature=e%252FDKfY2wLhBB9yqj5WtoJv2Ff6w%253D%22,%0A                    %22oauth_signature_method=HMAC-SHA1%22,%0A                    %22oauth_timestamp=1368908614%22,%0A                    %22oauth_version=1.0%22,%0A            %7D, params);%0A        %7D
 %0A    %7D%0A%0A

Add tests