Example usage for com.squareup.okhttp Response code

List of usage examples for com.squareup.okhttp Response code

Introduction

In this page you can find the example usage for com.squareup.okhttp Response code.

Prototype

int code

To view the source code for com.squareup.okhttp Response code.

Click Source Link

Usage

From source file:com.xing.api.CallSpecTest.java

License:Apache License

@Test
public void specThrowsIfExecutedTwice() throws Exception {
    server.enqueue(new MockResponse().setResponseCode(204));

    CallSpec spec = builder(HttpMethod.DELETE, "", false).responseAs(Object.class).build();
    Response response = spec.execute();

    assertThat(spec.isExecuted()).isTrue();
    assertThat(response.code()).isEqualTo(204);
    assertThat(response.body()).isNull();
    assertThat(response.raw()).isNotNull();

    try {/*from w  w w.ja v  a  2s. c o  m*/
        spec.execute();
        fail("#execute() should throw");
    } catch (IllegalStateException e) {
        assertThat(e.getMessage()).isNotEmpty().isEqualTo("Call already executed");
    }

    try {
        //noinspection unchecked,ConstantConditions
        spec.enqueue(null);
        fail("#enqueue() should throw");
    } catch (IllegalStateException e) {
        assertThat(e.getMessage()).isNotEmpty().isEqualTo("Call already executed");
    }
}

From source file:com.xing.api.CallSpecTest.java

License:Apache License

private static void assertNoBodySuccessResponse(Response response, int code) throws IOException {
    assertThat(response.code()).isEqualTo(code);
    assertThat(response.error()).isNull();
    assertThat(response.body()).isNull();
    assertThat(response.isSuccess()).isTrue();

    ResponseBody body = response.raw().body();
    assertThat(body).isNotNull();/*from   w ww. ja v a2  s  . c  o  m*/
    // User may want to ignore the content of another response.
    if (code == 204 || code == 205)
        assertThat(body.contentLength()).isEqualTo(0L);
    assertThat(body.contentType()).isNull();

    try {
        body.source();
    } catch (IllegalStateException e) {
        assertThat(e.getMessage()).isEqualTo("Cannot read raw response body of a parsed body.");
    }
}

From source file:com.xjeffrose.xio2.http.server.FileHandlerTest.java

License:Apache License

@Test
public void testHandle() throws Exception {
    Server s = Http.newServer();/*from  w w w . j  a va 2s. c o  m*/
    s.bind(9012, new FileHandler("src/test/resources"));
    s.serve();

    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url("http://localhost:9012/test.txt").build();

    Response response = client.newCall(request).execute();
    if (!response.isSuccessful())
        throw new IOException("Unexpected code " + response);

    assertTrue(response.isSuccessful());
    assertEquals(response.code(), 200);
    assertEquals("this is a test.txt", response.body().string());
}

From source file:com.xjeffrose.xio2.http.server.ServerTest.java

License:Apache License

@Test(expected = IOException.class)
public void testServe() throws Exception {
    s.bind(9000);/*  w  ww.ja  v  a  2 s  .  c  o m*/
    s.serve();

    Request request = new Request.Builder().url("http://localhost:9000/").build();

    Response response = client.newCall(request).execute();
    if (!response.isSuccessful())
        throw new IOException("Unexpected code " + response);

    assertEquals(response.code(), 404);
}

From source file:com.xjeffrose.xio2.http.server.ServerTest.java

License:Apache License

@Test
public void testServeMany() throws Exception {
    s.bind(9001);//  w  w w  .j av a 2  s.c  o  m
    s.serve();

    Request request = new Request.Builder().url("http://localhost:9001/").build();

    // Simulate 1500 obj's / second
    final int reqs = 1500;

    for (int i = 0; i < reqs; i++) {
        Response response = client.newCall(request).execute();
        assertEquals(response.code(), 404);
    }
}

From source file:com.xjeffrose.xio2.http.server.ServerTest.java

License:Apache License

@Test
public void testAddRoute() throws Exception {
    s.serve(9003, testHandler);/*  w  ww  .  ja  v a 2  s. c om*/

    Request request = new Request.Builder().url("http://localhost:9003/test").build();

    Response response = client.newCall(request).execute();
    if (!response.isSuccessful())
        throw new IOException("Unexpected code " + response);

    assertTrue(response.isSuccessful());
    assertEquals(response.code(), 200);
    assertEquals("THIS IS BODY", response.body().string());
}

From source file:com.xjeffrose.xio2.http.server.ServerTest.java

License:Apache License

@Test
public void testAddRouteMany() throws Exception {
    s.serve(9004, testHandler);/*www .j av  a 2 s. c o  m*/

    Request request = new Request.Builder().url("http://localhost:9004/test").build();

    // Simulate 1500 obj's / second
    final int reqs = 1500;

    for (int i = 0; i < reqs; i++) {
        Response response = client.newCall(request).execute();
        if (!response.isSuccessful())
            throw new IOException("Unexpected code " + response);

        assertTrue(response.isSuccessful());
        assertEquals(response.code(), 200);
        assertEquals("THIS IS BODY", response.body().string());
    }
}

From source file:com.xjeffrose.xio2.http.server.ServerTest.java

License:Apache License

@Test
public void testSsl() throws Exception {
    OkHttpClient unsafeClient = getUnsafeOkHttpClient();

    s.serve(9005, testHttpsHandler);// w  ww  .j  a v a  2  s.  c o  m

    Request request = new Request.Builder().url("https://localhost:9005/test").build();

    Response response = unsafeClient.newCall(request).execute();
    if (!response.isSuccessful())
        throw new IOException("Unexpected code " + response);

    assertEquals(response.code(), 200);
    assertEquals("THIS IS BODY", response.body().string());
}

From source file:com.xjeffrose.xio2.http.server.ServerTest.java

License:Apache License

@Test
public void testSslMany() throws Exception {
    OkHttpClient unsafeClient = getUnsafeOkHttpClient();

    s.serve(9006, testHttpsHandler);//w  w w  . j  av a2s  .c  o m

    Request request = new Request.Builder().url("https://localhost:9006/test").build();

    // Simulate 100 req's / second
    final int reqs = 100;

    for (int i = 0; i < reqs; i++) {
        Response response = unsafeClient.newCall(request).execute();
        if (!response.isSuccessful())
            throw new IOException("Unexpected code " + response);

        assertTrue(response.isSuccessful());
        assertEquals(response.code(), 200);
        assertEquals("THIS IS BODY", response.body().string());
    }
}

From source file:com.xjeffrose.xio2.http.server.ServiceTest.java

License:Apache License

@Test
public void testHandleGet() throws Exception {
    HttpHandler testHandler = new HttpHandler();
    testHandler.addRoute("/service_test", new TestHttpService());
    s.serve(9011, testHandler);//www  .  j a  va 2  s .c  o  m

    Request request = new Request.Builder().url("http://localhost:9011/service_test").build();

    Response response = client.newCall(request).execute();
    if (!response.isSuccessful())
        throw new IOException("Unexpected code " + response);

    assertTrue(response.isSuccessful());
    assertEquals(response.code(), 200);
}