@Test public void portPropertiesOnDifferentPort() throws Exception { new ServerPortInfoApplicationContextInitializer() .initialize(this.applicationContext); this.applicationContext.register(RootConfig.class, DifferentPortConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); this.applicationContext.refresh(); Integer localServerPort = this.applicationContext.getEnvironment() .getProperty("local.server.port", Integer.class); Integer localManagementPort = this.applicationContext.getEnvironment() .getProperty("local.management.port", Integer.class); assertThat(localServerPort).isNotNull(); assertThat(localManagementPort).isNotNull(); assertThat(localServerPort).isNotEqualTo(localManagementPort); assertThat(this.applicationContext.getBean(ServerPortConfig.class).getCount()) .isEqualTo(2); }
@Test public void componentsAreRegistered() { this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context.register(TestConfiguration.class); new ServerPortInfoApplicationContextInitializer().initialize(this.context); this.context.refresh(); String port = this.context.getEnvironment().getProperty("local.server.port"); String response = new RestTemplate() .getForObject("http://localhost:" + port + "/test", String.class); assertThat(response).isEqualTo("alpha bravo"); }
@Test public void portPropertiesOnSamePort() throws Exception { this.applicationContext.register(RootConfig.class, BaseConfiguration.class, ServerPortConfig.class, EndpointWebMvcAutoConfiguration.class); new ServerPortInfoApplicationContextInitializer() .initialize(this.applicationContext); this.applicationContext.refresh(); Integer localServerPort = this.applicationContext.getEnvironment() .getProperty("local.server.port", Integer.class); Integer localManagementPort = this.applicationContext.getEnvironment() .getProperty("local.management.port", Integer.class); assertThat(localServerPort).isNotNull(); assertThat(localManagementPort).isNotNull(); assertThat(localServerPort).isEqualTo(localManagementPort); }
@Test public void sessionExpiry() throws Exception { String port = null; try { ConfigurableApplicationContext context = new SpringApplicationBuilder() .sources(SampleSessionRedisApplication.class) .properties("server.port:0") .initializers(new ServerPortInfoApplicationContextInitializer()) .run(); port = context.getEnvironment().getProperty("local.server.port"); } catch (RuntimeException ex) { if (!redisServerRunning(ex)) { return; } throw ex; } URI uri = URI.create("http://localhost:" + port + "/"); RestTemplate restTemplate = new RestTemplate(); ResponseEntity<String> response = restTemplate.getForEntity(uri, String.class); String uuid1 = response.getBody(); HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.set("Cookie", response.getHeaders().getFirst("Set-Cookie")); RequestEntity<Void> request = new RequestEntity<Void>(requestHeaders, HttpMethod.GET, uri); String uuid2 = restTemplate.exchange(request, String.class).getBody(); assertThat(uuid1).isEqualTo(uuid2); Thread.sleep(5000); String uuid3 = restTemplate.exchange(request, String.class).getBody(); assertThat(uuid2).isNotEqualTo(uuid3); }