String applicationName = applicationConfiguration.getName().orElse(Environment.DEFAULT_NAME); boolean explicitInstanceId = instanceId != null; setPreferIpAddress(embeddedServer.getApplicationContext().get(PREFER_IP_ADDRESS, Boolean.class).orElse(false)); String serverHost = embeddedServer.getHost(); int serverPort = embeddedServer.getPort();
@BeforeClass public static void setupServer() { System.setProperty(Environment.BOOTSTRAP_CONTEXT_PROPERTY, "true"); Map<String, Object> map = new HashMap<>(); map.put(MockSpringCloudConfigServer.ENABLED, true); map.put("micronaut.server.port", -1); map.put("spring.cloud.config.enabled", false); map.put("micronaut.environments", "dev,test"); springCloudServer = ApplicationContext.run(EmbeddedServer.class, map); server = ApplicationContext.run(EmbeddedServer.class, CollectionUtils.mapOf( "micronaut.environments","test", "micronaut.application.name", "spring-config-sample", "micronaut.config-client.enabled", true, "spring.cloud.config.enabled", true, "spring.cloud.config.uri", springCloudServer.getURI() )); client = server .getApplicationContext() .createBean(HttpClient.class, LoadBalancer.fixed(server.getURL())); }
@Test public void testSenderAttributes() throws Exception { try(EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class)) { StoryClient client = embeddedServer.getApplicationContext().getBean(StoryClient.class); StoryClientFilter filter = embeddedServer.getApplicationContext().getBean(StoryClientFilter.class); Story story = client.getById("jan2019").blockingGet(); Assert.assertNotNull(story); Map<String, Object> attributes = filter.getLatestRequestAttributes(); Assert.assertNotNull(attributes); Assert.assertEquals("jan2019", attributes.get("story-id")); Assert.assertEquals("storyClient", attributes.get("client-name")); Assert.assertEquals("1", attributes.get("version")); } } }
@Test public void testSenderHeaders() throws Exception { Map<String,Object> config =Collections.singletonMap( "pet.client.id", "11" ); try(EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class, config)) { PetClient client = embeddedServer.getApplicationContext().getBean(PetClient.class); Pet pet = client.get("Fred").blockingGet(); Assert.assertNotNull(pet); Assert.assertEquals(pet.getAge(), 11); } } }
@Test public void testStreamingClient() { EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class); RxStreamingHttpClient client = embeddedServer.getApplicationContext().createBean(RxStreamingHttpClient.class, embeddedServer.getURL());
@Test public void testClientAnnotationStreaming() throws Exception { try( EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class) ) { HeadlineClient headlineClient = embeddedServer .getApplicationContext() .getBean(HeadlineClient.class); Event<Headline> headline = headlineClient.streamHeadlines().blockFirst(); assertNotNull( headline ); assertTrue( headline.getData().getText().startsWith("Latest Headline") ); } } // end::streamingClient[]
@Test public void testPostInvalidFormData() { EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class); RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL()); // tag::postform[] Map<String, String> data = new LinkedHashMap<>(); data.put("title", "The Stand"); data.put("pages", "notnumber"); data.put("url", "noturl"); Flowable<HttpResponse<Book>> call = client.exchange( POST("/binding/book", data) .contentType(MediaType.APPLICATION_FORM_URLENCODED), Book.class ); // end::postform[] thrown.expect(HttpClientResponseException.class); thrown.expectMessage(CoreMatchers.startsWith("Failed to convert argument [pages] for value [notnumber]")); HttpResponse<Book> response = call.blockingFirst(); embeddedServer.stop(); client.stop(); } }
@Test public void testClientAnnotationStreaming() throws Exception { try( EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class) ) { HeadlineClient headlineClient = embeddedServer .getApplicationContext() .getBean(HeadlineClient.class); // <1> Maybe<Headline> firstHeadline = headlineClient.streamHeadlines().firstElement(); // <2> Headline headline = firstHeadline.blockingGet(); // <3> assertNotNull( headline ); assertTrue( headline.getText().startsWith("Latest Headline") ); } } // end::streamingClient[]
@Test public void testPostPetValidation() { EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class); PetClient client = embeddedServer.getApplicationContext().getBean(PetClient.class); // tag::error[] thrown.expect(HttpClientResponseException.class); thrown.expectMessage("age: must be greater than or equal to 1"); client.save("Fred", -1).blockingGet(); // end::error[] embeddedServer.stop(); } }
@Test public void testSimpleRetrieve() { EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class); RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL()); // tag::simple[] String uri = UriBuilder.of("/hello/{name}") .expand(Collections.singletonMap("name", "John")) .toString(); assertEquals("/hello/John", uri); String result = client.toBlocking().retrieve(uri); assertEquals( "Hello John", result ); // end::simple[] embeddedServer.stop(); client.stop(); }
@Test public void testPostPet() { EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class); PetClient client = embeddedServer.getApplicationContext().getBean(PetClient.class); // tag::post[] Pet pet = client.save("Dino", 10).blockingGet(); assertEquals("Dino", pet.getName()); assertEquals(10, pet.getAge()); // end::post[] embeddedServer.stop(); }
@Test public void testRetrieveWithJSON() { EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class); RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL()); // tag::jsonmap[] Flowable<Map> response = client.retrieve( GET("/greet/John"), Map.class ); // end::jsonmap[] assertEquals( "Hello John", response.blockingFirst().get("text") ); // tag::jsonmaptypes[] response = client.retrieve( GET("/greet/John"), Argument.of(Map.class, String.class, String.class) // <1> ); // end::jsonmaptypes[] assertEquals( "Hello John", response.blockingFirst().get("text") ); embeddedServer.stop(); client.stop(); }
@Test public void testRetrieveWithPOJOResponse() { EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class); RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL()); // tag::pojoresponse[] Flowable<HttpResponse<Message>> call = client.exchange( GET("/greet/John"), Message.class // <1> ); HttpResponse<Message> response = call.blockingFirst(); Optional<Message> message = response.getBody(Message.class); // <2> // check the status assertEquals( HttpStatus.OK, response.getStatus() // <3> ); // check the body assertTrue(message.isPresent()); assertEquals( "Hello John", message.get().getText() ); // end::pojoresponse[] embeddedServer.stop(); client.stop(); }
@Test public void testPostWithURITemplate() { EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class); RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL()); // tag::posturitemplate[] Flowable<HttpResponse<Book>> call = client.exchange( POST("/amazon/book/{title}", new Book("The Stand")), Book.class ); // end::posturitemplate[] HttpResponse<Book> response = call.blockingFirst(); Optional<Book> message = response.getBody(Book.class); // <2> // check the status assertEquals( HttpStatus.CREATED, response.getStatus() // <3> ); // check the body assertTrue(message.isPresent()); assertEquals( "The Stand", message.get().getTitle() ); embeddedServer.stop(); client.stop(); }
@Test public void testPostRequestWithPOJO() { EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class); RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL());
@Test public void testPostRequestWithString() { EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class); RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL()); // tag::poststring[] Flowable<HttpResponse<String>> call = client.exchange( POST("/hello", "Hello John") // <1> .contentType(MediaType.TEXT_PLAIN_TYPE) .accept(MediaType.TEXT_PLAIN_TYPE), // <2> String.class // <3> ); // end::poststring[] HttpResponse<String> response = call.blockingFirst(); Optional<String> message = response.getBody(String.class); // <2> // check the status assertEquals( HttpStatus.CREATED, response.getStatus() // <3> ); // check the body assertTrue(message.isPresent()); assertEquals( "Hello John", message.get() ); embeddedServer.stop(); client.stop(); }
@Test public void testRetrieveWithPOJO() { EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class); RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL()); // tag::jsonpojo[] Flowable<Message> response = client.retrieve( GET("/greet/John"), Message.class ); assertEquals( "Hello John", response.blockingFirst().getText() ); // end::jsonpojo[] embeddedServer.stop(); client.stop(); }
@Test public void testRetrieveWithHeaders() { EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class); RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL()); // tag::headers[] Flowable<String> response = client.retrieve( GET("/hello/John") .header("X-My-Header", "SomeValue") ); // end::headers[] assertEquals( "Hello John", response.blockingFirst() ); embeddedServer.stop(); client.stop(); }
@Test public void testPostFormData() { EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class); RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL());
@BeforeClass public static void setupServer() { server = ApplicationContext.run(EmbeddedServer.class, Collections.singletonMap( "consul.client.enabled", false )); client = server .getApplicationContext() .createBean(HttpClient.class, server.getURL()); }