package com.onedrive.sdk.http;

import android.graphics.Path;
import android.net.Uri;
import android.test.AndroidTestCase;

import com.onedrive.sdk.core.MockClient;
import com.onedrive.sdk.extensions.IOneDriveClient;
import com.onedrive.sdk.options.Option;
import com.onedrive.sdk.options.QueryOption;

import junit.framework.Assert;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Test cases for (@see BaseRequest)
 */
public class BaseRequestTests extends AndroidTestCase{
    private IOneDriveClient mockClient;
    private BaseRequest mRequest;

    private final String baseUrl = "https://localhost:8080/";
    private final String[] testingSegments = { "Hello World", "你好世界", "Καλημέρα κόσμε", "안녕하세요", "コンニチハ", "แผ่นดินฮั่นเสื่อมโทรมแสนสังเวช" };

    @Override
    public void setUp() {
        mockClient = new MockClient();
        StringBuilder sb = new StringBuilder(baseUrl);

        for (String segment : testingSegments) {
            sb.append(segment);
            sb.append("/");
        }

        sb.deleteCharAt(sb.length()-1);

        mRequest = new BaseRequest(sb.toString(), mockClient, /*options:*/ null, null) {
            @Override
            public IOneDriveClient getClient() {
                return mockClient;
            }
        };
    }
    
    public void testUrlEncoded() throws Exception {
        URL requestUrl = mRequest.getRequestUrl();
        final Uri.Builder expectBuilder = Uri.parse(baseUrl).buildUpon();

        for (String segment : testingSegments) {
            expectBuilder.appendPath(segment);
        }

        Assert.assertEquals(expectBuilder.build().toString(), requestUrl.toString());
    }

    public void testUrlWithQuery() throws Exception {
        final String queryInUrl = "expand=foo&$select=id,name";
        final String testUrl = baseUrl + "?" + queryInUrl;

        final List<Option> options = new ArrayList<Option>();
        final QueryOption queryInOption = new QueryOption("queryWithEncode", "!");
        options.add(queryInOption);

        mRequest = new BaseRequest(testUrl, mockClient, options, null) {
            public IOneDriveClient getClient() {
                return mockClient;
            }
        };

        URL requestUrl = mRequest.getRequestUrl();

        final Uri.Builder expectBuilder = Uri.parse(testUrl).buildUpon();
        for (final Option option: options) {
            expectBuilder.appendQueryParameter(option.getName(), option.getValue());
        }

        Assert.assertEquals(expectBuilder.build().toString(), requestUrl.toString());
    }
}

@@ -634,17 +634,16 @@
 ost:8080
-/
 %22;%0A    p
@@ -986,23 +986,19 @@
 .append(
-segment
+%22/%22
 );%0A     
@@ -1006,35 +1006,39 @@
       sb.append(
-%22/%22
+segment
 );%0A        %7D%0A%0A  
@@ -1039,49 +1039,8 @@
  %7D%0A%0A
-        sb.deleteCharAt(sb.length()-1);%0A%0A
     
@@ -1261,20 +1261,16 @@
 ;%0A    %7D%0A
-    
 %0A    pub

update UT