Search in sources :

Example 66 with Request

use of com.tonyodev.fetch2.Request in project keywhiz by square.

the class XsrfServletFilterIntegrationTest method xsrfNotRequiredForLogout.

@Test
public void xsrfNotRequiredForLogout() throws Exception {
    Request request = new Request.Builder().post(RequestBody.create(MediaType.parse("text/plain"), "")).url(testUrl("/admin/logout")).build();
    Response response = client.newCall(request).execute();
    assertThat(response.code()).isNotEqualTo(401);
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) Test(org.junit.Test)

Example 67 with Request

use of com.tonyodev.fetch2.Request in project keywhiz by square.

the class XsrfServletFilterIntegrationTest method allowsForAdminUrlWithXsrf.

@Test
public void allowsForAdminUrlWithXsrf() throws Exception {
    client.newCall(buildLoginPost(DbSeedCommand.defaultUser, DbSeedCommand.defaultPassword)).execute();
    Request request = new Request.Builder().url(testUrl("/admin/clients")).get().build();
    Response response = client.newCall(request).execute();
    assertThat(response.code()).isNotEqualTo(401);
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) Test(org.junit.Test)

Example 68 with Request

use of com.tonyodev.fetch2.Request in project keywhiz by square.

the class SecretResourceTest method listExpiringV3.

List<SanitizedSecretWithGroups> listExpiringV3(Long time, String groupName) throws IOException {
    String requestURL = "/automation/v2/secrets/expiring/v3/";
    if (time != null && time > 0) {
        requestURL += time.toString() + "/";
    }
    if (groupName != null && groupName.length() > 0) {
        requestURL += groupName;
    }
    Request get = clientRequest(requestURL).get().build();
    Response httpResponse = mutualSslClient.newCall(get).execute();
    assertThat(httpResponse.code()).isEqualTo(200);
    return mapper.readValue(httpResponse.body().byteStream(), new TypeReference<List<SanitizedSecretWithGroups>>() {
    });
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) TestClients.clientRequest(keywhiz.TestClients.clientRequest) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList)

Example 69 with Request

use of com.tonyodev.fetch2.Request in project okhttp by square.

the class MockWebServer method handleWebSocketUpgrade.

private void handleWebSocketUpgrade(Socket socket, BufferedSource source, BufferedSink sink, RecordedRequest request, MockResponse response) throws IOException {
    String key = request.getHeader("Sec-WebSocket-Key");
    response.setHeader("Sec-WebSocket-Accept", WebSocketProtocol.acceptHeader(key));
    writeHttpResponse(socket, sink, response);
    // Adapt the request and response into our Request and Response domain model.
    String scheme = request.getTlsVersion() != null ? "https" : "http";
    // Has host and port.
    String authority = request.getHeader("Host");
    final Request fancyRequest = new Request.Builder().url(scheme + "://" + authority + "/").headers(request.getHeaders()).build();
    final Response fancyResponse = new Response.Builder().code(Integer.parseInt(response.getStatus().split(" ")[1])).message(response.getStatus().split(" ", 3)[2]).headers(response.getHeaders()).request(fancyRequest).protocol(Protocol.HTTP_1_1).build();
    final CountDownLatch connectionClose = new CountDownLatch(1);
    RealWebSocket.Streams streams = new RealWebSocket.Streams(false, source, sink) {

        @Override
        public void close() {
            connectionClose.countDown();
        }
    };
    RealWebSocket webSocket = new RealWebSocket(fancyRequest, response.getWebSocketListener(), new SecureRandom());
    response.getWebSocketListener().onOpen(webSocket, fancyResponse);
    String name = "MockWebServer WebSocket " + request.getPath();
    webSocket.initReaderAndWriter(name, 0, streams);
    try {
        webSocket.loopReader();
        // Even if messages are no longer being read we need to wait for the connection close signal.
        try {
            connectionClose.await();
        } catch (InterruptedException ignored) {
        }
    } catch (IOException e) {
        webSocket.failWebSocket(e, null);
    } finally {
        closeQuietly(sink);
        closeQuietly(source);
    }
}
Also used : Response(okhttp3.Response) RealWebSocket(okhttp3.internal.ws.RealWebSocket) Request(okhttp3.Request) SecureRandom(java.security.SecureRandom) ByteString(okio.ByteString) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 70 with Request

use of com.tonyodev.fetch2.Request in project okhttp by square.

the class JavaApiConverterTest method createOkRequest_nonNullRequestHeaders.

@Test
public void createOkRequest_nonNullRequestHeaders() throws Exception {
    URI uri = new URI("https://foo/bar");
    Map<String, List<String>> javaRequestHeaders = new LinkedHashMap<>();
    javaRequestHeaders.put("Foo", Arrays.asList("Bar"));
    Request request = JavaApiConverter.createOkRequest(uri, "POST", javaRequestHeaders);
    assertTrue(request.isHttps());
    assertEquals(uri, request.url().uri());
    Headers okRequestHeaders = request.headers();
    assertEquals(1, okRequestHeaders.size());
    assertEquals("Bar", okRequestHeaders.get("Foo"));
    assertEquals("POST", request.method());
}
Also used : Headers(okhttp3.Headers) Request(okhttp3.Request) List(java.util.List) URI(java.net.URI) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Aggregations

Request (okhttp3.Request)1617 Response (okhttp3.Response)1022 IOException (java.io.IOException)525 Test (org.junit.Test)407 OkHttpClient (okhttp3.OkHttpClient)331 RequestBody (okhttp3.RequestBody)256 Call (okhttp3.Call)239 ResponseBody (okhttp3.ResponseBody)189 HttpUrl (okhttp3.HttpUrl)146 Callback (okhttp3.Callback)109 Map (java.util.Map)85 File (java.io.File)77 InputStream (java.io.InputStream)77 JSONObject (org.json.JSONObject)76 MediaType (okhttp3.MediaType)75 Buffer (okio.Buffer)73 List (java.util.List)71 Headers (okhttp3.Headers)71 FormBody (okhttp3.FormBody)64 HashMap (java.util.HashMap)63