Example usage for com.squareup.okhttp Protocol HTTP_1_1

List of usage examples for com.squareup.okhttp Protocol HTTP_1_1

Introduction

In this page you can find the example usage for com.squareup.okhttp Protocol HTTP_1_1.

Prototype

Protocol HTTP_1_1

To view the source code for com.squareup.okhttp Protocol HTTP_1_1.

Click Source Link

Document

A plaintext framing that includes persistent connections.

Usage

From source file:com.facebook.buck.artifact_cache.HttpArtifactCacheTest.java

License:Apache License

@Test
public void testStoreMultipleKeys() throws Exception {
    final RuleKey ruleKey1 = new RuleKey("00000000000000000000000000000000");
    final RuleKey ruleKey2 = new RuleKey("11111111111111111111111111111111");
    final String data = "data";
    FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
    Path output = Paths.get("output/file");
    filesystem.writeContentsToPath(data, output);
    final Set<RuleKey> stored = Sets.newHashSet();
    HttpArtifactCache cache = new HttpArtifactCache("http", null, null, new URI("http://localhost:8080"),
            /* doStore */ true, filesystem, BUCK_EVENT_BUS) {
        @Override//  www. ja va  2s . com
        protected Response storeCall(Request request) throws IOException {
            Buffer buf = new Buffer();
            request.body().writeTo(buf);
            try (DataInputStream in = new DataInputStream(new ByteArrayInputStream(buf.readByteArray()))) {
                int keys = in.readInt();
                for (int i = 0; i < keys; i++) {
                    stored.add(new RuleKey(in.readUTF()));
                }
            }
            return new Response.Builder().code(HttpURLConnection.HTTP_ACCEPTED).protocol(Protocol.HTTP_1_1)
                    .request(request).build();
        }
    };
    cache.storeImpl(ImmutableSet.of(ruleKey1, ruleKey2), ImmutableMap.<String, String>of(), output,
            createFinishedEventBuilder());
    assertThat(stored, Matchers.containsInAnyOrder(ruleKey1, ruleKey2));
    cache.close();
}

From source file:com.facebook.buck.artifact_cache.HttpArtifactCacheTest.java

License:Apache License

@Test
public void testFetchWrongKey() throws Exception {
    FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
    final RuleKey ruleKey = new RuleKey("00000000000000000000000000000000");
    final RuleKey otherRuleKey = new RuleKey("11111111111111111111111111111111");
    final String data = "data";
    HttpArtifactCache cache = new HttpArtifactCache("http", null, null, new URI("http://localhost:8080"),
            /* doStore */ true, filesystem, BUCK_EVENT_BUS) {
        @Override/*from w w w  . ja  va2 s .  c  om*/
        protected Response fetchCall(Request request) throws IOException {
            return new Response.Builder().request(request).protocol(Protocol.HTTP_1_1)
                    .code(HttpURLConnection.HTTP_OK)
                    .body(createResponseBody(ImmutableSet.of(otherRuleKey), ImmutableMap.<String, String>of(),
                            ByteSource.wrap(data.getBytes(Charsets.UTF_8)), data))
                    .build();
        }
    };
    Path output = Paths.get("output/file");
    CacheResult result = cache.fetch(ruleKey, output);
    assertEquals(CacheResultType.ERROR, result.getType());
    assertEquals(Optional.<String>absent(), filesystem.readFileIfItExists(output));
    cache.close();
}

From source file:com.facebook.buck.artifact_cache.HttpArtifactCacheTest.java

License:Apache License

@Test
public void testFetchMetadata() throws Exception {
    Path output = Paths.get("output/file");
    final String data = "test";
    final RuleKey ruleKey = new RuleKey("00000000000000000000000000000000");
    final ImmutableMap<String, String> metadata = ImmutableMap.of("some", "metadata");
    FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
    HttpArtifactCache cache = new HttpArtifactCache("http", null, null, new URI("http://localhost:8080"),
            /* doStore */ true, filesystem, BUCK_EVENT_BUS) {
        @Override/*from w w w . j a  va2 s  .  c o  m*/
        protected Response fetchCall(Request request) throws IOException {
            return new Response.Builder().request(request).protocol(Protocol.HTTP_1_1)
                    .code(HttpURLConnection.HTTP_OK).body(createResponseBody(ImmutableSet.of(ruleKey), metadata,
                            ByteSource.wrap(data.getBytes(Charsets.UTF_8)), data))
                    .build();
        }
    };
    CacheResult result = cache.fetch(ruleKey, output);
    assertEquals(CacheResultType.HIT, result.getType());
    assertEquals(metadata, result.getMetadata());
    cache.close();
}

From source file:com.facebook.buck.rules.HttpArtifactCacheTest.java

License:Apache License

@Test
public void testFetchNotFound() throws Exception {
    HttpArtifactCache cache = new HttpArtifactCache(null, null, new URL("http://localhost:8080"),
            /* doStore */ true, new FakeProjectFilesystem(), HASH_FUNCTION) {
        @Override/*from w w  w . j a v a 2  s  .c om*/
        protected Response fetchCall(Request request) throws IOException {
            return new Response.Builder().code(HttpURLConnection.HTTP_NOT_FOUND).protocol(Protocol.HTTP_1_1)
                    .request(request).build();
        }
    };
    CacheResult result = cache.fetch(new RuleKey("00000000000000000000000000000000"), new File("output/file"));
    assertEquals(result, CacheResult.MISS);
    cache.close();
}

From source file:com.facebook.buck.rules.HttpArtifactCacheTest.java

License:Apache License

@Test
public void testFetchOK() throws Exception {
    File output = new File("output/file");
    final String data = "test";
    final RuleKey ruleKey = new RuleKey("00000000000000000000000000000000");
    FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
    HttpArtifactCache cache = new HttpArtifactCache(null, null, new URL("http://localhost:8080"),
            /* doStore */ true, filesystem, HASH_FUNCTION) {
        @Override//from ww  w . java2  s . c  o  m
        protected Response fetchCall(Request request) throws IOException {
            return new Response.Builder().code(HttpURLConnection.HTTP_OK).request(request)
                    .protocol(Protocol.HTTP_1_1).body(createBody(data)).build();
        }
    };
    CacheResult result = cache.fetch(ruleKey, output);
    assertEquals(CacheResult.HTTP_HIT, result);
    assertEquals(Optional.of(data), filesystem.readFileIfItExists(output.toPath()));
    cache.close();
}

From source file:com.facebook.buck.rules.HttpArtifactCacheTest.java

License:Apache License

@Test
public void testFetchUrl() throws Exception {
    final URL url = new URL("http://localhost:8080");
    final RuleKey ruleKey = new RuleKey("00000000000000000000000000000000");
    HttpArtifactCache cache = new HttpArtifactCache(null, null, new URL("http://localhost:8080"),
            /* doStore */ true, new FakeProjectFilesystem(), HASH_FUNCTION) {
        @Override//from  w w  w  . j  a  va 2  s  . co m
        protected Response fetchCall(Request request) throws IOException {
            assertEquals(new URL(url, "artifact/key/" + ruleKey.toString()), request.url());
            return new Response.Builder().code(HttpURLConnection.HTTP_OK).protocol(Protocol.HTTP_1_1)
                    .request(request).body(createBody("data")).build();
        }
    };
    cache.fetch(ruleKey, new File("output/file"));
    cache.close();
}

From source file:com.facebook.buck.rules.HttpArtifactCacheTest.java

License:Apache License

@Test
public void testFetchBadChecksum() throws Exception {
    FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
    HttpArtifactCache cache = new HttpArtifactCache(null, null, new URL("http://localhost:8080"),
            /* doStore */ true, filesystem, HASH_FUNCTION) {
        @Override/*ww w.  j  a v a 2s  .c  o m*/
        protected Response fetchCall(Request request) throws IOException {
            return new Response.Builder().code(HttpURLConnection.HTTP_OK).protocol(Protocol.HTTP_1_1)
                    .request(request).body(createBody(HashCode.fromInt(0), "test")).build();
        }
    };
    File output = new File("output/file");
    CacheResult result = cache.fetch(new RuleKey("00000000000000000000000000000000"), output);
    assertEquals(CacheResult.MISS, result);
    assertEquals(Optional.<String>absent(), filesystem.readFileIfItExists(output.toPath()));
    cache.close();
}

From source file:com.facebook.buck.rules.HttpArtifactCacheTest.java

License:Apache License

@Test
public void testFetchExtraPayload() throws Exception {
    FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
    HttpArtifactCache cache = new HttpArtifactCache(null, null, new URL("http://localhost:8080"),
            /* doStore */ true, filesystem, HASH_FUNCTION) {
        @Override/*from  www.  ja v a 2 s.  c  om*/
        protected Response fetchCall(Request request) throws IOException {
            return new Response.Builder().code(HttpURLConnection.HTTP_OK).protocol(Protocol.HTTP_1_1)
                    .request(request).body(createBody(4, "more data than length")).build();
        }
    };
    File output = new File("output/file");
    CacheResult result = cache.fetch(new RuleKey("00000000000000000000000000000000"), output);
    assertEquals(CacheResult.MISS, result);
    assertEquals(Optional.<String>absent(), filesystem.readFileIfItExists(output.toPath()));
    cache.close();
}

From source file:com.facebook.buck.rules.HttpArtifactCacheTest.java

License:Apache License

@Test
public void testStore() throws Exception {
    final String data = "data";
    FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
    File output = new File("output/file");
    filesystem.writeContentsToPath(data, output.toPath());
    final AtomicBoolean hasCalled = new AtomicBoolean(false);
    HttpArtifactCache cache = new HttpArtifactCache(null, null, new URL("http://localhost:8080"),
            /* doStore */ true, filesystem, HASH_FUNCTION) {
        @Override//from w  w  w  .j  a va 2s.  c o m
        protected Response storeCall(Request request) throws IOException {
            hasCalled.set(true);
            Buffer buf = new Buffer();
            request.body().writeTo(buf);
            assertArrayEquals(createArtifact(data), buf.readByteArray());
            return new Response.Builder().code(HttpURLConnection.HTTP_ACCEPTED).protocol(Protocol.HTTP_1_1)
                    .request(request).build();
        }
    };
    cache.store(new RuleKey("00000000000000000000000000000000"), output);
    assertTrue(hasCalled.get());
    cache.close();
}

From source file:com.facebook.buck.slb.LoadBalancedHttpResponseTest.java

License:Apache License

@Before
public void setUp() throws IOException {
    mockLoadBalancer = createMock(HttpLoadBalancer.class);
    mockInputStream = createMock(InputStream.class);

    mockBufferedSource = createMock(BufferedSource.class);
    mockBufferedSource.close();/*  www .  j a v a 2  s . c  om*/
    EasyMock.expectLastCall().once();

    responseBody = ResponseBody.create(MediaType.parse("text/plain"), 42, mockBufferedSource);
    request = new Request.Builder().url(SERVER.toString()).build();
    response = new Response.Builder().body(responseBody).code(200).protocol(Protocol.HTTP_1_1).request(request)
            .build();
}