use of org.springframework.web.client.RestTemplate in project spring-security-oauth by spring-projects.
the class DefaultOAuth2ExceptionRenderer method geDefaultMessageConverters.
private List<HttpMessageConverter<?>> geDefaultMessageConverters() {
List<HttpMessageConverter<?>> result = new ArrayList<HttpMessageConverter<?>>();
result.addAll(new RestTemplate().getMessageConverters());
result.add(new JaxbOAuth2ExceptionMessageConverter());
return result;
}
use of org.springframework.web.client.RestTemplate in project spring-security-oauth by spring-projects.
the class ClientCredentialsProviderTests method testHardCodedAuthenticationFineClient.
/**
* No Basic authentication provided, only the hard coded client_id.
*/
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void testHardCodedAuthenticationFineClient() {
RestTemplate restTemplate = new RestTemplate();
MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
params.add("grant_type", "client_credentials");
params.add("client_id", "my-client-with-secret");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
RequestEntity<MultiValueMap<String, String>> req = new RequestEntity<MultiValueMap<String, String>>(params, headers, HttpMethod.POST, tokenUri);
ResponseEntity<Map> response = restTemplate.exchange(req, Map.class);
assertEquals(HttpStatus.OK, response.getStatusCode());
Map<String, String> body = response.getBody();
String accessToken = body.get("access_token");
assertNotNull(accessToken);
}
use of org.springframework.web.client.RestTemplate in project incubator-servicecomb-java-chassis by apache.
the class ApolloClientTest method refreshApolloConfig.
@Test
public void refreshApolloConfig() {
ApolloConfig apolloConfig = ApolloConfig.INSTANCE;
RestTemplate rest = Mockito.mock(RestTemplate.class);
ResponseEntity<String> responseEntity = new ResponseEntity<>("{\"apollo\":\"mocked\", \"configurations\":{\"timeout\":1000}}", HttpStatus.OK);
Mockito.when(rest.exchange(Matchers.anyString(), Matchers.any(HttpMethod.class), Matchers.<HttpEntity<String>>any(), Matchers.<Class<String>>any())).thenReturn(responseEntity);
ApolloConfigurationSourceImpl impl = new ApolloConfigurationSourceImpl();
UpdateHandler updateHandler = impl.new UpdateHandler();
ApolloClient apolloClient = new ApolloClient(updateHandler);
Deencapsulation.setField(apolloClient, "rest", rest);
ConfigRefresh cr = apolloClient.new ConfigRefresh(apolloConfig.getServerUri());
cr.run();
Map<String, Object> originMap = Deencapsulation.getField(apolloClient, "originalConfigMap");
Assert.assertEquals(1, originMap.size());
}
use of org.springframework.web.client.RestTemplate in project spring-cloud-config by spring-cloud.
the class VaultEnvironmentRepositoryTests method testFindOneDefaultKeySetAndDifferentToApplication.
@Test
public void testFindOneDefaultKeySetAndDifferentToApplication() throws IOException {
MockHttpServletRequest configRequest = new MockHttpServletRequest();
configRequest.addHeader("X-CONFIG-TOKEN", "mytoken");
RestTemplate rest = Mockito.mock(RestTemplate.class);
ResponseEntity<VaultEnvironmentRepository.VaultResponse> myAppResp = Mockito.mock(ResponseEntity.class);
Mockito.when(myAppResp.getStatusCode()).thenReturn(HttpStatus.OK);
VaultEnvironmentRepository.VaultResponse myAppVaultResp = Mockito.mock(VaultEnvironmentRepository.VaultResponse.class);
Mockito.when(myAppVaultResp.getData()).thenReturn("{\"foo\":\"bar\"}");
Mockito.when(myAppResp.getBody()).thenReturn(myAppVaultResp);
Mockito.when(rest.exchange(Mockito.eq("http://127.0.0.1:8200/v1/{backend}/{key}"), Mockito.eq(HttpMethod.GET), Mockito.any(HttpEntity.class), Mockito.eq(VaultEnvironmentRepository.VaultResponse.class), Mockito.eq("secret"), Mockito.eq("myapp"))).thenReturn(myAppResp);
ResponseEntity<VaultEnvironmentRepository.VaultResponse> myDefaultKeyResp = Mockito.mock(ResponseEntity.class);
Mockito.when(myDefaultKeyResp.getStatusCode()).thenReturn(HttpStatus.OK);
VaultEnvironmentRepository.VaultResponse myDefaultKeyVaultResp = Mockito.mock(VaultEnvironmentRepository.VaultResponse.class);
Mockito.when(myDefaultKeyVaultResp.getData()).thenReturn("{\"def-foo\":\"def-bar\"}");
Mockito.when(myDefaultKeyResp.getBody()).thenReturn(myDefaultKeyVaultResp);
Mockito.when(rest.exchange(Mockito.eq("http://127.0.0.1:8200/v1/{backend}/{key}"), Mockito.eq(HttpMethod.GET), Mockito.any(HttpEntity.class), Mockito.eq(VaultEnvironmentRepository.VaultResponse.class), Mockito.eq("secret"), Mockito.eq("mydefaultkey"))).thenReturn(myDefaultKeyResp);
VaultEnvironmentRepository repo = new VaultEnvironmentRepository(configRequest, new EnvironmentWatch.Default(), rest, new VaultEnvironmentProperties());
repo.setDefaultKey("mydefaultkey");
Environment e = repo.findOne("myapp", null, null);
assertEquals("Name should be the same as the application argument", "myapp", e.getName());
assertEquals("Properties for specified application and default application with key 'mydefaultkey' should be returned", 2, e.getPropertySources().size());
Map<String, String> firstResult = new HashMap<String, String>();
firstResult.put("foo", "bar");
assertEquals("Properties for specified application should be returned in priority position", firstResult, e.getPropertySources().get(0).getSource());
Map<String, String> secondResult = new HashMap<String, String>();
secondResult.put("def-foo", "def-bar");
assertEquals("Properties for default application with key 'mydefaultkey' should be returned in second position", secondResult, e.getPropertySources().get(1).getSource());
}
use of org.springframework.web.client.RestTemplate in project NoraUi by NoraUi.
the class MailSteps method createRestTemplate.
private static RestTemplate createRestTemplate() {
String proxyUser = "";
String proxyPassword = "";
String proxyUrl = "";
int proxyPort = 0;
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
if (!"".equals(proxyUrl)) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(proxyUrl, proxyPort), new UsernamePasswordCredentials(proxyUser, proxyPassword));
HttpHost myProxy = new HttpHost(proxyUrl, proxyPort);
clientBuilder.setProxy(myProxy).setDefaultCredentialsProvider(credsProvider).disableCookieManagement();
}
HttpClient httpClient = clientBuilder.build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setHttpClient(httpClient);
RestTemplate restTemplate = new RestTemplate(factory);
restTemplate.getMessageConverters().add(new GsonHttpMessageConverter());
return restTemplate;
}
Aggregations