package com.xtremelabs.robolectric.tester.org.apache.http;

import com.xtremelabs.robolectric.shadows.StatusLineStub;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpVersion;
import org.apache.http.ProtocolVersion;
import org.apache.http.StatusLine;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class TestHttpResponse extends HttpResponseStub {

    private int statusCode;
    private String responseBody;
    private Header contentType;
    private TestStatusLine statusLine = new TestStatusLine();
    private TestHttpEntity httpEntity = new TestHttpEntity();

    public TestHttpResponse(int statusCode, String responseBody) {
        this.statusCode = statusCode;
        this.responseBody = responseBody;
    }

    public TestHttpResponse(int statusCode, String responseBody, Header contentType) {
        this(statusCode, responseBody);
        this.contentType = contentType;
    }

    @Override public StatusLine getStatusLine() {
        return statusLine;
    }

    @Override public HttpEntity getEntity() {
        return httpEntity;
    }

    public class TestHttpEntity extends HttpEntityStub {
        @Override public long getContentLength() {
            return responseBody.length();
        }
        
        @Override public Header getContentType() {
            return contentType;
        }
        
        @Override public boolean isStreaming() {
            return true;
        }
        
        @Override public boolean isRepeatable() {
            return true;
        }

        @Override public InputStream getContent() throws IOException, IllegalStateException {
            return new ByteArrayInputStream(responseBody.getBytes());
        }

        @Override public void writeTo(OutputStream outputStream) throws IOException {
            outputStream.write(responseBody.getBytes());
        }

        @Override public void consumeContent() throws IOException {
        }
    }

    public class TestStatusLine extends StatusLineStub {
        @Override public ProtocolVersion getProtocolVersion() {
            return new HttpVersion(1, 0);
        }

        @Override public int getStatusCode() {
            return statusCode;
        }

        @Override public String getReasonPhrase() {
            return "HTTP status " + statusCode;
        }
    }
}

@@ -1172,32 +1172,132 @@
 pEntity;%0A    %7D%0A%0A
+    @Override public Header%5B%5D getAllHeaders() %7B%0A        return new Header%5B%5D %7B contentType %7D;%0A    %7D%0A%0A
     public class

Add method getAllHeaders to make RoboElectric work with SpringTemplate