Search in sources :

Example 6 with HttpOptions

use of org.apache.http.client.methods.HttpOptions in project android-volley by mcxiaoke.

the class HttpClientStackTest method createOptionsRequest.

@Test
public void createOptionsRequest() throws Exception {
    TestRequest.Options request = new TestRequest.Options();
    assertEquals(request.getMethod(), Method.OPTIONS);
    HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null);
    assertTrue(httpRequest instanceof HttpOptions);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpOptions(org.apache.http.client.methods.HttpOptions) HttpOptions(org.apache.http.client.methods.HttpOptions) TestRequest(com.android.volley.mock.TestRequest) Test(org.junit.Test)

Example 7 with HttpOptions

use of org.apache.http.client.methods.HttpOptions in project iosched by google.

the class HttpClientStackTest method testCreateOptionsRequest.

public void testCreateOptionsRequest() throws Exception {
    TestRequest.Options request = new TestRequest.Options();
    assertEquals(request.getMethod(), Method.OPTIONS);
    HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null);
    assertTrue(httpRequest instanceof HttpOptions);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpOptions(org.apache.http.client.methods.HttpOptions) HttpOptions(org.apache.http.client.methods.HttpOptions) TestRequest(com.android.volley.mock.TestRequest)

Example 8 with HttpOptions

use of org.apache.http.client.methods.HttpOptions in project jmeter by apache.

the class HTTPHC4Impl method sample.

@Override
protected HTTPSampleResult sample(URL url, String method, boolean areFollowingRedirect, int frameDepth) {
    if (log.isDebugEnabled()) {
        log.debug("Start : sample {} method {} followingRedirect {} depth {}", url, method, areFollowingRedirect, frameDepth);
    }
    HTTPSampleResult res = createSampleResult(url, method);
    CloseableHttpClient httpClient = setupClient(url);
    HttpRequestBase httpRequest = null;
    try {
        URI uri = url.toURI();
        if (method.equals(HTTPConstants.POST)) {
            httpRequest = new HttpPost(uri);
        } else if (method.equals(HTTPConstants.GET)) {
            // otherwise we use HttpGetWithEntity
            if (!areFollowingRedirect && ((!hasArguments() && getSendFileAsPostBody()) || getSendParameterValuesAsPostBody())) {
                httpRequest = new HttpGetWithEntity(uri);
            } else {
                httpRequest = new HttpGet(uri);
            }
        } else if (method.equals(HTTPConstants.PUT)) {
            httpRequest = new HttpPut(uri);
        } else if (method.equals(HTTPConstants.HEAD)) {
            httpRequest = new HttpHead(uri);
        } else if (method.equals(HTTPConstants.TRACE)) {
            httpRequest = new HttpTrace(uri);
        } else if (method.equals(HTTPConstants.OPTIONS)) {
            httpRequest = new HttpOptions(uri);
        } else if (method.equals(HTTPConstants.DELETE)) {
            httpRequest = new HttpDelete(uri);
        } else if (method.equals(HTTPConstants.PATCH)) {
            httpRequest = new HttpPatch(uri);
        } else if (HttpWebdav.isWebdavMethod(method)) {
            httpRequest = new HttpWebdav(method, uri);
        } else {
            throw new IllegalArgumentException("Unexpected method: '" + method + "'");
        }
        // can throw IOException
        setupRequest(url, httpRequest, res);
    } catch (Exception e) {
        res.sampleStart();
        res.sampleEnd();
        errorResult(e, res);
        return res;
    }
    HttpContext localContext = new BasicHttpContext();
    setupClientContextBeforeSample(localContext);
    res.sampleStart();
    final CacheManager cacheManager = getCacheManager();
    if (cacheManager != null && HTTPConstants.GET.equalsIgnoreCase(method) && cacheManager.inCache(url)) {
        return updateSampleResultForResourceInCache(res);
    }
    CloseableHttpResponse httpResponse = null;
    try {
        currentRequest = httpRequest;
        handleMethod(method, res, httpRequest, localContext);
        // store the SampleResult in LocalContext to compute connect time
        localContext.setAttribute(SAMPLER_RESULT_TOKEN, res);
        // perform the sample
        httpResponse = executeRequest(httpClient, httpRequest, localContext, url);
        // Needs to be done after execute to pick up all the headers
        final HttpRequest request = (HttpRequest) localContext.getAttribute(HttpCoreContext.HTTP_REQUEST);
        extractClientContextAfterSample(localContext);
        // We've finished with the request, so we can add the LocalAddress to it for display
        final InetAddress localAddr = (InetAddress) httpRequest.getParams().getParameter(ConnRoutePNames.LOCAL_ADDRESS);
        if (localAddr != null) {
            request.addHeader(HEADER_LOCAL_ADDRESS, localAddr.toString());
        }
        res.setRequestHeaders(getConnectionHeaders(request));
        Header contentType = httpResponse.getLastHeader(HTTPConstants.HEADER_CONTENT_TYPE);
        if (contentType != null) {
            String ct = contentType.getValue();
            res.setContentType(ct);
            res.setEncodingAndType(ct);
        }
        HttpEntity entity = httpResponse.getEntity();
        if (entity != null) {
            res.setResponseData(readResponse(res, entity.getContent(), entity.getContentLength()));
        }
        // Done with the sampling proper.
        res.sampleEnd();
        currentRequest = null;
        // Now collect the results into the HTTPSampleResult:
        StatusLine statusLine = httpResponse.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        res.setResponseCode(Integer.toString(statusCode));
        res.setResponseMessage(statusLine.getReasonPhrase());
        res.setSuccessful(isSuccessCode(statusCode));
        res.setResponseHeaders(getResponseHeaders(httpResponse, localContext));
        if (res.isRedirect()) {
            final Header headerLocation = httpResponse.getLastHeader(HTTPConstants.HEADER_LOCATION);
            if (headerLocation == null) {
                // HTTP protocol violation, but avoids NPE
                throw new IllegalArgumentException("Missing location header in redirect for " + httpRequest.getRequestLine());
            }
            String redirectLocation = headerLocation.getValue();
            res.setRedirectLocation(redirectLocation);
        }
        // record some sizes to allow HTTPSampleResult.getBytes() with different options
        HttpConnectionMetrics metrics = (HttpConnectionMetrics) localContext.getAttribute(CONTEXT_METRICS);
        long headerBytes = // condensed length (without \r)
        (long) res.getResponseHeaders().length() + // Add \r for each header
        (long) httpResponse.getAllHeaders().length + // Add \r for initial header
        1L + // final \r\n before data
        2L;
        long totalBytes = metrics.getReceivedBytesCount();
        res.setHeadersSize((int) headerBytes);
        res.setBodySize(totalBytes - headerBytes);
        res.setSentBytes(metrics.getSentBytesCount());
        if (log.isDebugEnabled()) {
            log.debug("ResponseHeadersSize={} Content-Length={} Total={}", res.getHeadersSize(), res.getBodySizeAsLong(), (res.getHeadersSize() + res.getBodySizeAsLong()));
        }
        // If we redirected automatically, the URL may have changed
        if (getAutoRedirects()) {
            HttpUriRequest req = (HttpUriRequest) localContext.getAttribute(HttpCoreContext.HTTP_REQUEST);
            HttpHost target = (HttpHost) localContext.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
            URI redirectURI = req.getURI();
            if (redirectURI.isAbsolute()) {
                res.setURL(redirectURI.toURL());
            } else {
                res.setURL(new URL(new URL(target.toURI()), redirectURI.toString()));
            }
        }
        // Store any cookies received in the cookie manager:
        saveConnectionCookies(httpResponse, res.getURL(), getCookieManager());
        // Save cache information
        if (cacheManager != null) {
            cacheManager.saveDetails(httpResponse, res);
        }
        // Follow redirects and download page resources if appropriate:
        res = resultProcessing(areFollowingRedirect, frameDepth, res);
    } catch (IOException e) {
        log.debug("IOException", e);
        if (res.getEndTime() == 0) {
            res.sampleEnd();
        }
        // pick up headers if failed to execute the request
        if (res.getRequestHeaders() != null) {
            log.debug("Overwriting request old headers: {}", res.getRequestHeaders());
        }
        res.setRequestHeaders(getConnectionHeaders((HttpRequest) localContext.getAttribute(HttpCoreContext.HTTP_REQUEST)));
        errorResult(e, res);
        return res;
    } catch (RuntimeException e) {
        log.debug("RuntimeException", e);
        if (res.getEndTime() == 0) {
            res.sampleEnd();
        }
        errorResult(e, res);
        return res;
    } finally {
        JOrphanUtils.closeQuietly(httpResponse);
        currentRequest = null;
        JMeterContextService.getContext().getSamplerContext().remove(HTTPCLIENT_TOKEN);
    }
    return res;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpEntity(org.apache.http.HttpEntity) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpGet(org.apache.http.client.methods.HttpGet) HttpOptions(org.apache.http.client.methods.HttpOptions) URI(java.net.URI) HttpPut(org.apache.http.client.methods.HttpPut) HttpHead(org.apache.http.client.methods.HttpHead) HttpPatch(org.apache.http.client.methods.HttpPatch) URL(java.net.URL) HttpConnectionMetrics(org.apache.http.HttpConnectionMetrics) HttpHost(org.apache.http.HttpHost) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) CacheManager(org.apache.jmeter.protocol.http.control.CacheManager) HttpRequest(org.apache.http.HttpRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpContext(org.apache.http.protocol.HttpContext) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) IOException(java.io.IOException) HttpException(org.apache.http.HttpException) ClientProtocolException(org.apache.http.client.ClientProtocolException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) StatusLine(org.apache.http.StatusLine) HttpTrace(org.apache.http.client.methods.HttpTrace) Header(org.apache.http.Header) BufferedHeader(org.apache.http.message.BufferedHeader) InetAddress(java.net.InetAddress)

Example 9 with HttpOptions

use of org.apache.http.client.methods.HttpOptions in project wildfly by wildfly.

the class DenyUncoveredHttpMethodsTestCase method testOptionsMethod.

@Test
public void testOptionsMethod() throws Exception {
    HttpOptions httpOptions = new HttpOptions(getURL());
    HttpResponse response = getHttpResponse(httpOptions);
    assertThat(statusCodeOf(response), is(HttpServletResponse.SC_FORBIDDEN));
}
Also used : HttpOptions(org.apache.http.client.methods.HttpOptions) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test)

Example 10 with HttpOptions

use of org.apache.http.client.methods.HttpOptions in project TaEmCasa by Dionen.

the class HttpClientStackTest method createOptionsRequest.

@Test
public void createOptionsRequest() throws Exception {
    TestRequest.Options request = new TestRequest.Options();
    assertEquals(request.getMethod(), Method.OPTIONS);
    HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null);
    assertTrue(httpRequest instanceof HttpOptions);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpOptions(org.apache.http.client.methods.HttpOptions) HttpOptions(org.apache.http.client.methods.HttpOptions) TestRequest(com.android.volley.mock.TestRequest) Test(org.junit.Test)

Aggregations

HttpOptions (org.apache.http.client.methods.HttpOptions)15 Test (org.junit.Test)8 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)7 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)6 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)5 HttpHead (org.apache.http.client.methods.HttpHead)5 TestRequest (com.android.volley.mock.TestRequest)4 HttpPatch (org.apache.http.client.methods.HttpPatch)4 HttpPost (org.apache.http.client.methods.HttpPost)4 HttpPut (org.apache.http.client.methods.HttpPut)4 HttpTrace (org.apache.http.client.methods.HttpTrace)4 HttpGet (org.apache.http.client.methods.HttpGet)3 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 URI (java.net.URI)2 Header (org.apache.http.Header)2 HttpEntity (org.apache.http.HttpEntity)2 HttpResponse (org.apache.http.HttpResponse)2 StatusLine (org.apache.http.StatusLine)2 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)2