package com.qiniu.android;

import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.Log;

import com.qiniu.android.http.Client;
import com.qiniu.android.http.CompletionHandler;
import com.qiniu.android.http.ResponseInfo;

import junit.framework.Assert;

import org.json.JSONObject;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * Created by bailong on 15/12/1.
 */
public class HttpsTest extends InstrumentationTestCase {
    final CountDownLatch signal = new CountDownLatch(1);
    private Client httpManager;
    private ResponseInfo info;

    private static URI newURI(String s) {
        try {
            return new URI(s);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void setUp() throws Exception {
        httpManager = new Client();
    }

    @SmallTest
    public void testPost1() throws Throwable {
        httpManager.asyncPost("https://www.baidu.com/", "hello".getBytes(), null, null, new CompletionHandler() {
            @Override
            public void complete(ResponseInfo rinfo, JSONObject response) {
                Log.d("qiniutest", rinfo.toString());
                info = rinfo;
                signal.countDown();
            }
        }, null);

        try {
            signal.await(6000, TimeUnit.SECONDS); // wait for callback
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Assert.assertEquals(info.error, 200, info.statusCode);
        Assert.assertNull(info.reqId);
    }

    @SmallTest
    public void testPost2() throws Throwable {
        httpManager.asyncPost("https://static-fw.qbox.me/public/v28812/add-on/ga/analytics.js",
                "hello".getBytes(), null, null, new CompletionHandler() {
                    @Override
                    public void complete(ResponseInfo rinfo, JSONObject response) {
                        Log.d("qiniutest", rinfo.toString());
                        info = rinfo;
                        signal.countDown();
                    }
                }, null);

        try {
            signal.await(60000, TimeUnit.SECONDS); // wait for callback
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Assert.assertEquals(info.error, 200, info.statusCode);
        Assert.assertNotNull(info.reqId);
    }

    @SmallTest
    public void testPost3() throws Throwable {
        httpManager.asyncPost("https://up.qiniu.com",
                "hello".getBytes(), null, null, new CompletionHandler() {
                    @Override
                    public void complete(ResponseInfo rinfo, JSONObject response) {
                        Log.d("qiniutest", rinfo.toString());
                        info = rinfo;
                        signal.countDown();
                    }
                }, null);

        try {
            signal.await(60000, TimeUnit.SECONDS); // wait for callback
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // cert is not match
        Assert.assertEquals(info.error, -1, info.statusCode);
    }
}

@@ -2524,32 +2524,34 @@
 .reqId);%0A    %7D%0A%0A
+//
     @SmallTest%0A 
@@ -2541,32 +2541,34 @@
 /    @SmallTest%0A
+//
     public void 
@@ -2590,32 +2590,34 @@
 ows Throwable %7B%0A
+//
         httpMana
@@ -2654,16 +2654,18 @@
 u.com%22,%0A
+//
         
@@ -2722,32 +2722,34 @@
 tionHandler() %7B%0A
+//
                 
@@ -2754,32 +2754,34 @@
       @Override%0A
+//
                 
@@ -2840,32 +2840,34 @@
 ect response) %7B%0A
+//
                 
@@ -2904,32 +2904,34 @@
 fo.toString());%0A
+//
                 
@@ -2944,32 +2944,34 @@
   info = rinfo;%0A
+//
                 
@@ -2990,32 +2990,34 @@
 al.countDown();%0A
+//
                 
@@ -3014,32 +3014,34 @@
               %7D%0A
+//
                 
@@ -3042,33 +3042,37 @@
       %7D, null);%0A
-%0A
+//%0A//
         try %7B%0A  
@@ -3061,32 +3061,34 @@
 //        try %7B%0A
+//
             sign
@@ -3135,32 +3135,34 @@
 it for callback%0A
+//
         %7D catch 
@@ -3180,32 +3180,34 @@
 dException e) %7B%0A
+//
             e.pr
@@ -3215,32 +3215,34 @@
 ntStackTrace();%0A
+//
         %7D%0A      
@@ -3227,32 +3227,34 @@
 ();%0A//        %7D%0A
+//
         // cert 
@@ -3266,16 +3266,18 @@
 t match%0A
+//
         
@@ -3326,20 +3326,22 @@
 tatusCode);%0A
+//
     %7D%0A%7D%0A

remove useless test