Search in sources :

Example 51 with BasicStatusLine

use of org.apache.http.message.BasicStatusLine in project SimplifyReader by chentao0707.

the class OkHttpStack method performRequest.

@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError {
    OkHttpClient client = mClient.clone();
    int timeoutMs = request.getTimeoutMs();
    client.setConnectTimeout(timeoutMs, TimeUnit.MILLISECONDS);
    client.setReadTimeout(timeoutMs, TimeUnit.MILLISECONDS);
    client.setWriteTimeout(timeoutMs, TimeUnit.MILLISECONDS);
    com.squareup.okhttp.Request.Builder okHttpRequestBuilder = new com.squareup.okhttp.Request.Builder();
    okHttpRequestBuilder.url(request.getUrl());
    Map<String, String> headers = request.getHeaders();
    for (final String name : headers.keySet()) {
        okHttpRequestBuilder.addHeader(name, headers.get(name));
    }
    for (final String name : additionalHeaders.keySet()) {
        okHttpRequestBuilder.addHeader(name, additionalHeaders.get(name));
    }
    setConnectionParametersForRequest(okHttpRequestBuilder, request);
    com.squareup.okhttp.Request okHttpRequest = okHttpRequestBuilder.build();
    Call okHttpCall = client.newCall(okHttpRequest);
    Response okHttpResponse = okHttpCall.execute();
    StatusLine responseStatus = new BasicStatusLine(parseProtocol(okHttpResponse.protocol()), okHttpResponse.code(), okHttpResponse.message());
    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    response.setEntity(entityFromOkHttpResponse(okHttpResponse));
    Headers responseHeaders = okHttpResponse.headers();
    for (int i = 0, len = responseHeaders.size(); i < len; i++) {
        final String name = responseHeaders.name(i), value = responseHeaders.value(i);
        if (name != null) {
            response.addHeader(new BasicHeader(name, value));
        }
    }
    return response;
}
Also used : Call(com.squareup.okhttp.Call) OkHttpClient(com.squareup.okhttp.OkHttpClient) Headers(com.squareup.okhttp.Headers) Request(com.android.volley.Request) BasicStatusLine(org.apache.http.message.BasicStatusLine) Response(com.squareup.okhttp.Response) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) BasicHeader(org.apache.http.message.BasicHeader)

Example 52 with BasicStatusLine

use of org.apache.http.message.BasicStatusLine in project android-uploader by nightscout.

the class RestV1UploaderTest method setUpExecuteCaptor.

public void setUpExecuteCaptor(int status) throws IOException {
    captor = ArgumentCaptor.forClass(HttpUriRequest.class);
    HttpResponse response = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("mock", 1, 2), status, ""));
    response.setEntity(new StringEntity(""));
    when(mockHttpClient.execute(captor.capture())).thenReturn(response);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) StringEntity(org.apache.http.entity.StringEntity) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) ProtocolVersion(org.apache.http.ProtocolVersion) BasicStatusLine(org.apache.http.message.BasicStatusLine)

Example 53 with BasicStatusLine

use of org.apache.http.message.BasicStatusLine in project LiveSDK-for-Android by liveservices.

the class UploadRequestTest method testSendPathQueryParameterToHttpPut.

/**
     * WinLive 633441: Make sure the query parameters on path get sent to
     * the HTTP PUT part of the upload.
     */
public void testSendPathQueryParameterToHttpPut() throws Throwable {
    JSONObject jsonResponseBody = new JSONObject();
    jsonResponseBody.put(JsonKeys.UPLOAD_LOCATION, "http://test.com/location");
    InputStream responseStream = new ByteArrayInputStream(jsonResponseBody.toString().getBytes());
    MockHttpEntity responseEntity = new MockHttpEntity(responseStream);
    BasicStatusLine ok = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "");
    final MockHttpResponse uploadLocationResponse = new MockHttpResponse(responseEntity, ok);
    HttpClient client = new HttpClient() {

        /** the first request to the client is the upload location request. */
        boolean uploadLocationRequest = true;

        @Override
        public HttpResponse execute(HttpUriRequest request) throws IOException, ClientProtocolException {
            if (uploadLocationRequest) {
                uploadLocationRequest = false;
                return uploadLocationResponse;
            }
            // This is really the only part we care about in this test.
            // That the 2nd request's uri has foo=bar in the query string.
            URI uri = request.getURI();
            assertEquals("foo=bar&overwrite=choosenewname", uri.getQuery());
            // just return the previous reponse.
            return uploadLocationResponse;
        }

        @Override
        public HttpResponse execute(HttpUriRequest request, HttpContext context) throws IOException, ClientProtocolException {
            throw new UnsupportedOperationException();
        }

        @Override
        public HttpResponse execute(HttpHost target, HttpRequest request) throws IOException, ClientProtocolException {
            throw new UnsupportedOperationException();
        }

        @Override
        public <T> T execute(HttpUriRequest arg0, ResponseHandler<? extends T> arg1) throws IOException, ClientProtocolException {
            throw new UnsupportedOperationException();
        }

        @Override
        public HttpResponse execute(HttpHost target, HttpRequest request, HttpContext context) throws IOException, ClientProtocolException {
            throw new UnsupportedOperationException();
        }

        @Override
        public <T> T execute(HttpUriRequest arg0, ResponseHandler<? extends T> arg1, HttpContext arg2) throws IOException, ClientProtocolException {
            throw new UnsupportedOperationException();
        }

        @Override
        public <T> T execute(HttpHost arg0, HttpRequest arg1, ResponseHandler<? extends T> arg2) throws IOException, ClientProtocolException {
            throw new UnsupportedOperationException();
        }

        @Override
        public <T> T execute(HttpHost arg0, HttpRequest arg1, ResponseHandler<? extends T> arg2, HttpContext arg3) throws IOException, ClientProtocolException {
            throw new UnsupportedOperationException();
        }

        @Override
        public ClientConnectionManager getConnectionManager() {
            throw new UnsupportedOperationException();
        }

        @Override
        public HttpParams getParams() {
            throw new UnsupportedOperationException();
        }
    };
    LiveConnectSession session = TestUtils.newMockLiveConnectSession();
    HttpEntity entity = new MockHttpEntity();
    String path = Paths.ME_SKYDRIVE + "?foo=bar";
    String filename = "filename";
    UploadRequest uploadRequest = new UploadRequest(session, client, path, entity, filename, OverwriteOption.Rename);
    uploadRequest.execute();
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpRequest(org.apache.http.HttpRequest) ResponseHandler(org.apache.http.client.ResponseHandler) HttpEntity(org.apache.http.HttpEntity) MockHttpEntity(com.microsoft.live.mock.MockHttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpContext(org.apache.http.protocol.HttpContext) MockHttpEntity(com.microsoft.live.mock.MockHttpEntity) URI(java.net.URI) BasicStatusLine(org.apache.http.message.BasicStatusLine) JSONObject(org.json.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpHost(org.apache.http.HttpHost) HttpClient(org.apache.http.client.HttpClient) MockHttpResponse(com.microsoft.live.mock.MockHttpResponse)

Example 54 with BasicStatusLine

use of org.apache.http.message.BasicStatusLine in project LiveSDK-for-Android by liveservices.

the class ApiTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    // Set up the MockClient
    this.mockEntity = new MockHttpEntity();
    StatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
    this.mockResponse = new MockHttpResponse(this.mockEntity, statusLine);
    this.mockClient = new MockHttpClient(this.mockResponse);
    this.loadLiveLibraryHeaderChecker();
    this.exceptionQueue = new LinkedBlockingQueue<LiveOperationException>();
    this.liveConnectClient = TestUtils.newLiveConnectClient(this.mockClient);
}
Also used : BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) MockHttpEntity(com.microsoft.live.mock.MockHttpEntity) LiveOperationException(com.microsoft.live.LiveOperationException) MockHttpClient(com.microsoft.live.mock.MockHttpClient) BasicStatusLine(org.apache.http.message.BasicStatusLine) MockHttpResponse(com.microsoft.live.mock.MockHttpResponse)

Example 55 with BasicStatusLine

use of org.apache.http.message.BasicStatusLine in project LiveSDK-for-Android by liveservices.

the class UploadTest method loadValidResponseBody.

@Override
protected void loadValidResponseBody() throws Exception {
    JSONObject jsonResponseBody = new JSONObject();
    jsonResponseBody.put(JsonKeys.ID, FILE_ID);
    jsonResponseBody.put(JsonKeys.SOURCE, SOURCE);
    InputStream responseStream = new ByteArrayInputStream(jsonResponseBody.toString().getBytes());
    MockHttpEntity responseEntity = new MockHttpEntity(responseStream);
    StatusLine created = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_CREATED, "");
    MockHttpResponse response = new MockHttpResponse(responseEntity, created);
    this.mockClient.addHttpResponse(response);
}
Also used : BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) JSONObject(org.json.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MockHttpEntity(com.microsoft.live.mock.MockHttpEntity) BasicStatusLine(org.apache.http.message.BasicStatusLine) MockHttpResponse(com.microsoft.live.mock.MockHttpResponse)

Aggregations

BasicStatusLine (org.apache.http.message.BasicStatusLine)70 ProtocolVersion (org.apache.http.ProtocolVersion)54 StatusLine (org.apache.http.StatusLine)44 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)37 Test (org.junit.Test)27 Header (org.apache.http.Header)17 StringEntity (org.apache.http.entity.StringEntity)17 BasicHeader (org.apache.http.message.BasicHeader)17 URL (java.net.URL)16 HttpResponse (org.apache.http.HttpResponse)16 HttpURLConnection (java.net.HttpURLConnection)15 List (java.util.List)15 HashMap (java.util.HashMap)12 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)12 ByteArrayInputStream (java.io.ByteArrayInputStream)10 IOException (java.io.IOException)9 HttpEntity (org.apache.http.HttpEntity)9 HttpHost (org.apache.http.HttpHost)6 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)6 AgentAutoRegistrationPropertiesImpl (com.thoughtworks.go.agent.AgentAutoRegistrationPropertiesImpl)5