List of usage examples for org.apache.http.impl.client HttpClients createMinimal
public static CloseableHttpClient createMinimal()
From source file:com.linecorp.armeria.server.docs.DocServiceTest.java
@Test public void testOk() throws Exception { final Map<Class<?>, Iterable<EndpointInfo>> serviceMap = new HashMap<>(); serviceMap.put(HelloService.class, Collections .singletonList(EndpointInfo.of("*", "/", "hello", THRIFT_BINARY, SerializationFormat.ofThrift()))); serviceMap.put(SleepService.class, Collections .singletonList(EndpointInfo.of("*", "/", "sleep", THRIFT_BINARY, SerializationFormat.ofThrift()))); serviceMap.put(FooService.class, Collections .singletonList(EndpointInfo.of("*", "/foo", "", THRIFT_COMPACT, EnumSet.of(THRIFT_COMPACT)))); serviceMap.put(Cassandra.class, Arrays.asList(EndpointInfo.of("*", "/cassandra", "", THRIFT_BINARY, EnumSet.of(THRIFT_BINARY)), EndpointInfo.of("*", "/cassandra/debug", "", THRIFT_TEXT, EnumSet.of(THRIFT_TEXT)))); serviceMap.put(Hbase.class, Collections .singletonList(EndpointInfo.of("*", "/hbase", "", THRIFT_BINARY, SerializationFormat.ofThrift()))); serviceMap.put(OnewayHelloService.class, Collections .singletonList(EndpointInfo.of("*", "/oneway", "", THRIFT_BINARY, SerializationFormat.ofThrift()))); final ObjectMapper mapper = new ObjectMapper(); final String expectedJson = mapper.writeValueAsString(Specification.forServiceClasses(serviceMap, ImmutableMap.of(hello_args.class, SAMPLE_HELLO), SAMPLE_HTTP_HEADERS)); try (CloseableHttpClient hc = HttpClients.createMinimal()) { final HttpGet req = new HttpGet(specificationUri()); try (CloseableHttpResponse res = hc.execute(req)) { assertThat(res.getStatusLine().toString()).isEqualTo("HTTP/1.1 200 OK"); String responseJson = EntityUtils.toString(res.getEntity()); // Convert to Map for order-insensitive comparison. Map<?, ?> actual = mapper.readValue(responseJson, Map.class); Map<?, ?> expected = mapper.readValue(expectedJson, Map.class); assertThat(actual).isEqualTo(expected); }/* w w w.j a va 2 s. c o m*/ } }
From source file:com.linecorp.armeria.server.http.auth.AuthServiceTest.java
@Test public void testBasicAuth() throws Exception { try (CloseableHttpClient hc = HttpClients.createMinimal()) { try (CloseableHttpResponse res = hc .execute(basicGetRequest("/basic", BasicToken.of("brown", "cony")))) { assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK")); }// w ww. j a va 2 s.c o m try (CloseableHttpResponse res = hc .execute(basicGetRequest("/basic", BasicToken.of("pangyo", "choco")))) { assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK")); } try (CloseableHttpResponse res = hc.execute(new HttpGet(uri("/basic")))) { assertThat(res.getStatusLine().toString(), is("HTTP/1.1 401 Unauthorized")); } try (CloseableHttpResponse res = hc .execute(basicGetRequest("/basic", BasicToken.of("choco", "pangyo")))) { assertThat(res.getStatusLine().toString(), is("HTTP/1.1 401 Unauthorized")); } } }
From source file:com.linecorp.armeria.server.grpc.GrpcDocServiceTest.java
@Test public void testOk() throws Exception { List<ServiceEntry> entries = ImmutableList.of( new ServiceEntry(TEST_SERVICE_DESCRIPTOR, ImmutableList.of(new EndpointInfo("*", "/test/armeria.grpc.testing.TestService/", "", GrpcSerializationFormats.PROTO.mediaType(), ImmutableSet.of(GrpcSerializationFormats.PROTO.mediaType(), GrpcSerializationFormats.JSON.mediaType(), GrpcSerializationFormats.PROTO_WEB.mediaType(), GrpcSerializationFormats.JSON_WEB.mediaType(), MediaType.PROTOBUF.withParameter("protocol", "gRPC"), MediaType.JSON_UTF_8.withParameter("protocol", "gRPC"))))), new ServiceEntry(RECONNECT_SERVICE_DESCRIPTOR, ImmutableList.of(new EndpointInfo("*", "/armeria.grpc.testing.ReconnectService/", "", GrpcSerializationFormats.PROTO, ImmutableSet.of(GrpcSerializationFormats.PROTO, GrpcSerializationFormats.PROTO_WEB))))); final ObjectMapper mapper = new ObjectMapper(); final JsonNode expectedJson = mapper.valueToTree(new GrpcDocServicePlugin().generate(entries)); // The specification generated by GrpcDocServicePlugin does not include the examples specified // when building a DocService, so we add them manually here. addExamples(expectedJson);// w w w . j a va2s. c o m try (CloseableHttpClient hc = HttpClients.createMinimal()) { final HttpGet req = new HttpGet(specificationUri()); try (CloseableHttpResponse res = hc.execute(req)) { assertThat(res.getStatusLine().toString()).isEqualTo("HTTP/1.1 200 OK"); final JsonNode actualJson = mapper.readTree(EntityUtils.toString(res.getEntity())); // The specification generated by ThriftDocServicePlugin does not include the docstrings // because it's injected by the DocService, so we remove them here for easier comparison. removeDocStrings(actualJson); // Convert to the prettified strings for human-readable comparison. final ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter(); final String actualJsonString = writer.writeValueAsString(actualJson); final String expectedJsonString = writer.writeValueAsString(expectedJson); assertThat(actualJsonString).isEqualTo(expectedJsonString); } } }
From source file:com.linecorp.armeria.server.thrift.ThriftDocServiceTest.java
@Test public void testOk() throws Exception { final Map<Class<?>, Iterable<EndpointInfo>> serviceMap = new HashMap<>(); serviceMap.put(HelloService.class, Collections .singletonList(new EndpointInfo("*", "/", "hello", THRIFT_BINARY, SerializationFormat.ofThrift()))); serviceMap.put(SleepService.class, Collections .singletonList(new EndpointInfo("*", "/", "sleep", THRIFT_BINARY, SerializationFormat.ofThrift()))); serviceMap.put(FooService.class, Collections .singletonList(new EndpointInfo("*", "/foo", "", THRIFT_COMPACT, EnumSet.of(THRIFT_COMPACT)))); serviceMap.put(Cassandra.class, Arrays.asList(new EndpointInfo("*", "/cassandra", "", THRIFT_BINARY, EnumSet.of(THRIFT_BINARY)), new EndpointInfo("*", "/cassandra/debug", "", THRIFT_TEXT, EnumSet.of(THRIFT_TEXT)))); serviceMap.put(Hbase.class, Collections .singletonList(new EndpointInfo("*", "/hbase", "", THRIFT_BINARY, SerializationFormat.ofThrift()))); serviceMap.put(OnewayHelloService.class, Collections.singletonList( new EndpointInfo("*", "/oneway", "", THRIFT_BINARY, SerializationFormat.ofThrift()))); final ObjectMapper mapper = new ObjectMapper(); final String expectedJson = mapper .writeValueAsString(ThriftServiceSpecificationGenerator.generate(serviceMap)); // FIXME(trustin): Bring this back. //ImmutableMap.of(hello_args.class, SAMPLE_HELLO), //SAMPLE_HTTP_HEADERS)); try (CloseableHttpClient hc = HttpClients.createMinimal()) { final HttpGet req = new HttpGet(specificationUri()); try (CloseableHttpResponse res = hc.execute(req)) { assertThat(res.getStatusLine().toString()).isEqualTo("HTTP/1.1 200 OK"); String responseJson = EntityUtils.toString(res.getEntity()); // Convert to Map for order-insensitive comparison. Map<?, ?> actual = mapper.readValue(responseJson, Map.class); Map<?, ?> expected = mapper.readValue(expectedJson, Map.class); assertThat(actual).isEqualTo(expected); }//from ww w . j ava 2 s .c o m } }
From source file:com.linecorp.armeria.server.http.file.HttpFileServiceTest.java
@Test public void testGetPreCompressedSupportsNone() throws Exception { try (CloseableHttpClient hc = HttpClients.createMinimal()) { HttpGet request = new HttpGet(newUri("/compressed/foo.txt")); try (CloseableHttpResponse res = hc.execute(request)) { assertThat(res.getFirstHeader("Content-Encoding"), is(nullValue())); assertThat(res.getFirstHeader("Content-Type").getValue(), is("text/plain; charset=utf-8")); final byte[] content = ByteStreams.toByteArray(res.getEntity().getContent()); assertThat(new String(content, StandardCharsets.UTF_8), is("foo")); }// www .j a v a 2 s. c o m } }
From source file:com.linecorp.armeria.server.ServerTest.java
@Test public void testRequestTimeoutInvocation() throws Exception { try (CloseableHttpClient hc = HttpClients.createMinimal()) { final HttpPost req = new HttpPost(uri("/timeout")); req.setEntity(new StringEntity("Hello, world!", StandardCharsets.UTF_8)); try (CloseableHttpResponse res = hc.execute(req)) { assertThat(HttpStatusClass.valueOf(res.getStatusLine().getStatusCode()), is(not(HttpStatusClass.SUCCESS))); }/*from w w w. ja v a2 s. c om*/ } }
From source file:com.linecorp.armeria.server.docs.DocServiceTest.java
@Test public void testMethodNotAllowed() throws Exception { try (CloseableHttpClient hc = HttpClients.createMinimal()) { final HttpPost req = new HttpPost(specificationUri()); try (CloseableHttpResponse res = hc.execute(req)) { assertThat(res.getStatusLine().toString()).isEqualTo("HTTP/1.1 405 Method Not Allowed"); }//from w ww. ja v a 2 s .c o m } }
From source file:com.linecorp.armeria.server.http.auth.HttpAuthServiceTest.java
@Test public void testBasicAuth() throws Exception { try (CloseableHttpClient hc = HttpClients.createMinimal()) { try (CloseableHttpResponse res = hc .execute(basicGetRequest("/basic", BasicToken.of("brown", "cony")))) { assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK")); }//from w w w . j a v a 2 s. com try (CloseableHttpResponse res = hc .execute(basicGetRequest("/basic", BasicToken.of("pangyo", "choco")))) { assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK")); } try (CloseableHttpResponse res = hc.execute(new HttpGet(server.uri("/basic")))) { assertThat(res.getStatusLine().toString(), is("HTTP/1.1 401 Unauthorized")); } try (CloseableHttpResponse res = hc .execute(basicGetRequest("/basic", BasicToken.of("choco", "pangyo")))) { assertThat(res.getStatusLine().toString(), is("HTTP/1.1 401 Unauthorized")); } } }
From source file:org.basinmc.maven.plugins.minecraft.AbstractArtifactMojo.java
/** * Fetches any resource from a remote HTTP server and writes it to a supplied channel. *//*ww w . j a v a2s . c o m*/ protected void fetch(@Nonnull URI uri, @Nonnull @WillNotClose WritableByteChannel outputChannel) throws IOException { HttpClient client = HttpClients.createMinimal(); HttpGet request = new HttpGet(uri); HttpResponse response = client.execute(request); StatusLine line = response.getStatusLine(); if (line.getStatusCode() != 200) { throw new IOException( "Unexpected status code: " + line.getStatusCode() + " - " + line.getReasonPhrase()); } try (InputStream inputStream = response.getEntity().getContent()) { try (ReadableByteChannel inputChannel = Channels.newChannel(inputStream)) { ByteStreams.copy(inputChannel, outputChannel); } } }
From source file:com.linecorp.armeria.server.http.file.HttpFileServiceTest.java
@Test public void testGetPreCompressedSupportsGzip() throws Exception { try (CloseableHttpClient hc = HttpClients.createMinimal()) { HttpGet request = new HttpGet(newUri("/compressed/foo.txt")); request.setHeader("Accept-Encoding", "gzip"); try (CloseableHttpResponse res = hc.execute(request)) { assertThat(res.getFirstHeader("Content-Encoding").getValue(), is("gzip")); assertThat(res.getFirstHeader("Content-Type").getValue(), is("text/plain; charset=utf-8")); final byte[] content; try (GZIPInputStream unzipper = new GZIPInputStream(res.getEntity().getContent())) { content = ByteStreams.toByteArray(unzipper); }//from w ww . j a v a2s . c o m assertThat(new String(content, StandardCharsets.UTF_8), is("foo")); } } }