Search in sources :

Example 91 with Connection

use of okhttp3.Connection in project okhttp by square.

the class URLConnectionTest method fullyBufferedPostIsTooLong.

@Test
public void fullyBufferedPostIsTooLong() throws Exception {
    server.enqueue(new MockResponse().setBody("A"));
    connection = urlFactory.open(server.url("/b").url());
    connection.setRequestProperty("Content-Length", "3");
    connection.setRequestMethod("POST");
    OutputStream out = connection.getOutputStream();
    out.write('a');
    out.write('b');
    out.write('c');
    try {
        out.write('d');
        out.flush();
        fail();
    } catch (IOException expected) {
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 92 with Connection

use of okhttp3.Connection in project okhttp by square.

the class URLConnectionTest method fullyBufferedPostIsTooShort.

@Test
public void fullyBufferedPostIsTooShort() throws Exception {
    server.enqueue(new MockResponse().setBody("A"));
    connection = urlFactory.open(server.url("/b").url());
    connection.setRequestProperty("Content-Length", "4");
    connection.setRequestMethod("POST");
    OutputStream out = connection.getOutputStream();
    out.write('a');
    out.write('b');
    out.write('c');
    try {
        out.close();
        fail();
    } catch (IOException expected) {
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 93 with Connection

use of okhttp3.Connection in project okhttp by square.

the class URLConnectionTest method bufferedBodyWithClientRequestTimeout.

@Test
public void bufferedBodyWithClientRequestTimeout() throws Exception {
    enqueueClientRequestTimeoutResponses();
    HttpURLConnection connection = urlFactory.open(server.url("/").url());
    connection.setRequestMethod("POST");
    connection.getOutputStream().write("Hello".getBytes("UTF-8"));
    assertEquals(200, connection.getResponseCode());
    assertEquals("Body", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
    RecordedRequest request1 = server.takeRequest();
    assertEquals("Hello", request1.getBody().readUtf8());
    RecordedRequest request2 = server.takeRequest();
    assertEquals("Hello", request2.getBody().readUtf8());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) HttpURLConnection(java.net.HttpURLConnection) OkHttpURLConnection(okhttp3.internal.huc.OkHttpURLConnection) Test(org.junit.Test)

Example 94 with Connection

use of okhttp3.Connection in project okhttp by square.

the class URLConnectionTest method zeroLengthPayload.

private void zeroLengthPayload(String method) throws IOException, InterruptedException {
    server.enqueue(new MockResponse());
    connection = urlFactory.open(server.url("/").url());
    connection.setRequestProperty("Content-Length", "0");
    connection.setRequestMethod(method);
    connection.setFixedLengthStreamingMode(0);
    connection.setDoOutput(true);
    assertContent("", connection);
    RecordedRequest zeroLengthPayload = server.takeRequest();
    assertEquals(method, zeroLengthPayload.getMethod());
    assertEquals("0", zeroLengthPayload.getHeader("content-length"));
    assertEquals(0L, zeroLengthPayload.getBodySize());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse)

Example 95 with Connection

use of okhttp3.Connection in project okhttp by square.

the class URLConnectionTest method testAuthenticateWithStreamingPost.

private void testAuthenticateWithStreamingPost(StreamingMode streamingMode) throws Exception {
    MockResponse pleaseAuthenticate = new MockResponse().setResponseCode(401).addHeader("WWW-Authenticate: Basic realm=\"protected area\"").setBody("Please authenticate.");
    server.enqueue(pleaseAuthenticate);
    Authenticator.setDefault(new RecordingAuthenticator());
    urlFactory.setClient(urlFactory.client().newBuilder().authenticator(new JavaNetAuthenticator()).build());
    connection = urlFactory.open(server.url("/").url());
    connection.setDoOutput(true);
    byte[] requestBody = { 'A', 'B', 'C', 'D' };
    if (streamingMode == StreamingMode.FIXED_LENGTH) {
        connection.setFixedLengthStreamingMode(requestBody.length);
    } else if (streamingMode == StreamingMode.CHUNKED) {
        connection.setChunkedStreamingMode(0);
    }
    OutputStream outputStream = connection.getOutputStream();
    outputStream.write(requestBody);
    outputStream.close();
    try {
        connection.getInputStream();
        fail();
    } catch (HttpRetryException expected) {
    }
    // no authorization header for the request...
    RecordedRequest request = server.takeRequest();
    assertNull(request.getHeader("Authorization"));
    assertEquals("ABCD", request.getBody().readUtf8());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) OutputStream(java.io.OutputStream) HttpRetryException(java.net.HttpRetryException) RecordingAuthenticator(okhttp3.internal.RecordingAuthenticator)

Aggregations

Test (org.junit.Test)226 MockResponse (okhttp3.mockwebserver.MockResponse)215 HttpURLConnection (java.net.HttpURLConnection)106 IOException (java.io.IOException)73 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)67 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)59 URLConnection (java.net.URLConnection)53 Response (okhttp3.Response)41 InputStream (java.io.InputStream)39 Connection (com.trilead.ssh2.Connection)36 Request (okhttp3.Request)36 OkHttpURLConnection (okhttp3.internal.huc.OkHttpURLConnection)36 URL (java.net.URL)32 Session (com.trilead.ssh2.Session)31 InFrame (okhttp3.internal.http2.MockHttp2Peer.InFrame)28 Buffer (okio.Buffer)25 OutputStream (java.io.OutputStream)19 InterruptedIOException (java.io.InterruptedIOException)15 Call (okhttp3.Call)14 BufferedSink (okio.BufferedSink)14