// Path: GgaRestAndroidLib/ggarest/src/androidTest/java/com/greengrowapps/ggarest/GgaRestTest.java
// public class GgaRestTest extends AndroidTestCase{
// 
//     UrlConnectionAuthorizator authorizator;
// 
//     protected WebserviceImpl getWebserviceInstance() {
// 
//         RequestExecutionFactory factory = new RequestExecutionFactory() {
//             @Override
//             public RequestExecution newInstance(ConnectionDefinition connectionDefinition, final RequestExecutionCallbacks requestExecutionCallbacks, List<MockRequest> mockRequests, WebserviceImpl webservice) {
//                 return new RequestExecutionImpl(connectionDefinition, new RequestCallbackCaller() {
//                     @Override
//                     public void callRequestCompleted(ResponseImpl response) {
//                         requestExecutionCallbacks.onRequestCompleted(response);
//                     }
// 
//                     @Override
//                     public void callError(Exception e) {
//                         requestExecutionCallbacks.onException(e);
//                     }
// 
//                     @Override
//                     public void callTimeout() {
//                         requestExecutionCallbacks.onTimeout();
//                     }
//                 }, webservice, authorizator, mockRequests);
//             }
//         };
// 
//         return new WebserviceImpl(factory, new JsonSerializer(), new StreamConverterImpl("UTF-8"));
//     }
// 
//     protected void setLogin(String user, String pass) {
//         authorizator = new UrlConnectionBasicAuthorizator(new CredentialsImpl(user,pass));
//     }
// 
//     public static String random(int randomLength) {
//         Random generator = new Random();
//         StringBuilder randomStringBuilder = new StringBuilder();
//         char tempChar;
//         for (int i = 0; i < randomLength; i++){
//             tempChar = (char) (generator.nextInt(96) + 32);
//             randomStringBuilder.append(tempChar);
//         }
//         return randomStringBuilder.toString();
//     }
// }
// 
// Path: GgaRestJavaLib/ggarest/src/main/java/com/greengrowapps/ggarest/exceptions/AlreadyExecutingException.java
// public class AlreadyExecutingException extends RuntimeException{
// }
// 
// Path: GgaRestJavaLib/ggarest/src/main/java/com/greengrowapps/ggarest/listeners/OnObjResponseListener.java
// public interface OnObjResponseListener<T> {
// 
//     void onResponse(int code, T object, Response fullResponse);
// }
// 
// Path: GgaRestJavaLib/ggarest/src/main/java/com/greengrowapps/ggarest/listeners/OnResponseListener.java
// public interface OnResponseListener {
// 
//     void onResponse(int code, Response fullResponse, Exception e);
// 
// }
// 
// Path: GgaRestAndroidLib/ggarest/src/androidTest/java/com/greengrowapps/ggarest/tools/AsyncBlockRunner.java
// public interface AsyncBlockRunner {
// 
//     void run( AsyncTimeoutHelper asyncTimeoutHelper ) throws Exception;
// 
//     void onEndCalled( );
// 
//     void onTimeout( );
// 
// }
// 
// Path: GgaRestAndroidLib/ggarest/src/androidTest/java/com/greengrowapps/ggarest/tools/AsyncTimeoutHelper.java
// public class AsyncTimeoutHelper {
// 
//     private final int timeoutMilis;
//     private final AsyncBlockRunner asyncBlockRunner;
// 
//     private final CountDownLatch latch = new CountDownLatch(1);
//     private String failMessage;
//     private Runnable assertions;
// 
//     private AsyncTimeoutHelper(int timeoutMilis, AsyncBlockRunner asyncBlockRunner){
//         this.timeoutMilis = timeoutMilis;
//         this.asyncBlockRunner = asyncBlockRunner;
//     }
// 
//     public static void runAndWaitForEnd(AndroidTestCase testCase, int timeoutMilis, AsyncBlockRunner asyncBlockRunner) throws Exception {
// 
//         new AsyncTimeoutHelper(timeoutMilis,asyncBlockRunner).run();
//     }
// 
//     private void run() throws Exception {
//         asyncBlockRunner.run(this);
// 
//         if(latch.await(timeoutMilis, TimeUnit.MILLISECONDS)){
//             if(failMessage==null) {
//                 if(assertions!=null){
//                     assertions.run();
//                 }
//                 asyncBlockRunner.onEndCalled();
//             }
//             else{
//                 AndroidTestCase.fail(failMessage);
//             }
//         }
//         else {
//             asyncBlockRunner.onTimeout();
//         }
//     }
// 
//     public void end(){
//         latch.countDown();
//     }
// 
//     public void failSync(String failMessage){
//         this.failMessage = failMessage;
// 
//         latch.countDown();
//     }
// 
//     public void performAssertions(Runnable runnable){
//         this.assertions = runnable;
//     }
// }
// 
// Path: GgaRestAndroidLib/ggarest/src/androidTest/java/com/greengrowapps/ggarest/tools/AsyncTimeoutHelper.java
// public static void runAndWaitForEnd(AndroidTestCase testCase, int timeoutMilis, AsyncBlockRunner asyncBlockRunner) throws Exception {
// 
//     new AsyncTimeoutHelper(timeoutMilis,asyncBlockRunner).run();
// }


import com.greengrowapps.ggarest.GgaRestTest;
import com.greengrowapps.ggarest.exceptions.AlreadyExecutingException;
import com.greengrowapps.ggarest.listeners.OnObjResponseListener;
import com.greengrowapps.ggarest.listeners.OnResponseListener;
import com.greengrowapps.ggarest.tools.AsyncBlockRunner;
import com.greengrowapps.ggarest.tools.AsyncTimeoutHelper;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
import java.io.IOException;
import java.util.Random;
import static com.greengrowapps.ggarest.tools.AsyncTimeoutHelper.runAndWaitForEnd;
    }
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Headers{

        @JsonProperty("Content-Length")
        public String contentLenth;
    }

    public void testPostContentLength() throws Exception {

        checkPostWithStringLength(1);
        checkPostWithStringLength(4);
        checkPostWithStringLength(256);
        checkPostWithStringLength(1024);
        checkPostWithStringLength(2048);
        checkPostWithStringLength(4194304);
    }

    private void checkPostWithStringLength(int length) throws IOException {
        final String content=random(length);
        final String contentJson = getWebserviceInstance().getSerializer().fromObject(content);
        final int realLength=contentJson.getBytes().length;
        getWebserviceInstance()
                .post("http://httpbin.org/post")
                .withBody(content)
                .onSuccess(EchoPostResponse.class, new OnObjResponseListener<EchoPostResponse>() {
                    @Override
                    public void onResponse(int code, final EchoPostResponse message, final Response fullResponse) {
                        assertEquals(contentJson, message.data);
                        assertEquals(content, message.json);
// Path: GgaRestAndroidLib/ggarest/src/androidTest/java/com/greengrowapps/ggarest/GgaRestTest.java
// public class GgaRestTest extends AndroidTestCase{
// 
//     UrlConnectionAuthorizator authorizator;
// 
//     protected WebserviceImpl getWebserviceInstance() {
// 
//         RequestExecutionFactory factory = new RequestExecutionFactory() {
//             @Override
//             public RequestExecution newInstance(ConnectionDefinition connectionDefinition, final RequestExecutionCallbacks requestExecutionCallbacks, List<MockRequest> mockRequests, WebserviceImpl webservice) {
//                 return new RequestExecutionImpl(connectionDefinition, new RequestCallbackCaller() {
//                     @Override
//                     public void callRequestCompleted(ResponseImpl response) {
//                         requestExecutionCallbacks.onRequestCompleted(response);
//                     }
// 
//                     @Override
//                     public void callError(Exception e) {
//                         requestExecutionCallbacks.onException(e);
//                     }
// 
//                     @Override
//                     public void callTimeout() {
//                         requestExecutionCallbacks.onTimeout();
//                     }
//                 }, webservice, authorizator, mockRequests);
//             }
//         };
// 
//         return new WebserviceImpl(factory, new JsonSerializer(), new StreamConverterImpl("UTF-8"));
//     }
// 
//     protected void setLogin(String user, String pass) {
//         authorizator = new UrlConnectionBasicAuthorizator(new CredentialsImpl(user,pass));
//     }
// 
//     public static String random(int randomLength) {
//         Random generator = new Random();
//         StringBuilder randomStringBuilder = new StringBuilder();
//         char tempChar;
//         for (int i = 0; i < randomLength; i++){
//             tempChar = (char) (generator.nextInt(96) + 32);
//             randomStringBuilder.append(tempChar);
//         }
//         return randomStringBuilder.toString();
//     }
// }
// 
// Path: GgaRestJavaLib/ggarest/src/main/java/com/greengrowapps/ggarest/exceptions/AlreadyExecutingException.java
// public class AlreadyExecutingException extends RuntimeException{
// }
// 
// Path: GgaRestJavaLib/ggarest/src/main/java/com/greengrowapps/ggarest/listeners/OnObjResponseListener.java
// public interface OnObjResponseListener<T> {
// 
//     void onResponse(int code, T object, Response fullResponse);
// }
// 
// Path: GgaRestJavaLib/ggarest/src/main/java/com/greengrowapps/ggarest/listeners/OnResponseListener.java
// public interface OnResponseListener {
// 
//     void onResponse(int code, Response fullResponse, Exception e);
// 
// }
// 
// Path: GgaRestAndroidLib/ggarest/src/androidTest/java/com/greengrowapps/ggarest/tools/AsyncBlockRunner.java
// public interface AsyncBlockRunner {
// 
//     void run( AsyncTimeoutHelper asyncTimeoutHelper ) throws Exception;
// 
//     void onEndCalled( );
// 
//     void onTimeout( );
// 
// }
// 
// Path: GgaRestAndroidLib/ggarest/src/androidTest/java/com/greengrowapps/ggarest/tools/AsyncTimeoutHelper.java
// public class AsyncTimeoutHelper {
// 
//     private final int timeoutMilis;
//     private final AsyncBlockRunner asyncBlockRunner;
// 
//     private final CountDownLatch latch = new CountDownLatch(1);
//     private String failMessage;
//     private Runnable assertions;
// 
//     private AsyncTimeoutHelper(int timeoutMilis, AsyncBlockRunner asyncBlockRunner){
//         this.timeoutMilis = timeoutMilis;
//         this.asyncBlockRunner = asyncBlockRunner;
//     }
// 
//     public static void runAndWaitForEnd(AndroidTestCase testCase, int timeoutMilis, AsyncBlockRunner asyncBlockRunner) throws Exception {
// 
//         new AsyncTimeoutHelper(timeoutMilis,asyncBlockRunner).run();
//     }
// 
//     private void run() throws Exception {
//         asyncBlockRunner.run(this);
// 
//         if(latch.await(timeoutMilis, TimeUnit.MILLISECONDS)){
//             if(failMessage==null) {
//                 if(assertions!=null){
//                     assertions.run();
//                 }
//                 asyncBlockRunner.onEndCalled();
//             }
//             else{
//                 AndroidTestCase.fail(failMessage);
//             }
//         }
//         else {
//             asyncBlockRunner.onTimeout();
//         }
//     }
// 
//     public void end(){
//         latch.countDown();
//     }
// 
//     public void failSync(String failMessage){
//         this.failMessage = failMessage;
// 
//         latch.countDown();
//     }
// 
//     public void performAssertions(Runnable runnable){
//         this.assertions = runnable;
//     }
// }
// 
// Path: GgaRestAndroidLib/ggarest/src/androidTest/java/com/greengrowapps/ggarest/tools/AsyncTimeoutHelper.java
// public static void runAndWaitForEnd(AndroidTestCase testCase, int timeoutMilis, AsyncBlockRunner asyncBlockRunner) throws Exception {
// 
//     new AsyncTimeoutHelper(timeoutMilis,asyncBlockRunner).run();
// }


// Path: GgaRestAndroidLib/ggarest/src/androidTest/java/com/greengrowapps/ggarest/PostContentLengthTest.java
import com.greengrowapps.ggarest.GgaRestTest;
import com.greengrowapps.ggarest.exceptions.AlreadyExecutingException;
import com.greengrowapps.ggarest.listeners.OnObjResponseListener;
import com.greengrowapps.ggarest.listeners.OnResponseListener;
import com.greengrowapps.ggarest.tools.AsyncBlockRunner;
import com.greengrowapps.ggarest.tools.AsyncTimeoutHelper;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
import java.io.IOException;
import java.util.Random;
import static com.greengrowapps.ggarest.tools.AsyncTimeoutHelper.runAndWaitForEnd;

    }
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Headers{

        @JsonProperty("Content-Length")
        public String contentLenth;
    }

    public void testPostContentLength() throws Exception {

        checkPostWithStringLength(1);
        checkPostWithStringLength(4);
        checkPostWithStringLength(256);
        checkPostWithStringLength(1024);
        checkPostWithStringLength(2048);
        checkPostWithStringLength(4194304);
    }

    private void checkPostWithStringLength(int length) throws IOException {
        final String content=random(length);
        final String contentJson = getWebserviceInstance().getSerializer().fromObject(content);
        final int realLength=contentJson.getBytes().length;
        getWebserviceInstance()
                .post("http://httpbin.org/post")
                .withBody(content)
                .onSuccess(EchoPostResponse.class, new OnObjResponseListener<EchoPostResponse>() {
                    @Override
                    public void onResponse(int code, final EchoPostResponse message, final Response fullResponse) {
                        assertEquals(contentJson, message.data);
                        assertEquals(content, message.json);
                        assertEquals(realLength+"",message.headers.contentLenth);