Java Code Examples for org.springframework.web.client.HttpClientErrorException#create()

The following examples show how to use org.springframework.web.client.HttpClientErrorException#create() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DockerConfigJsonSecretToRegistryConfigurationConverterTest.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
@Test
public void testConvertDockerHubRegistry() throws URISyntaxException {

	HttpHeaders authenticateHeader = new HttpHeaders();
	authenticateHeader.add("Www-Authenticate", "Bearer realm=\"https://demo.repository.io/service/token\",service=\"demo-registry\",scope=\"registry:category:pull\"");
	HttpClientErrorException httpClientErrorException =
			HttpClientErrorException.create(HttpStatus.UNAUTHORIZED, "", authenticateHeader, new byte[0], null);

	when(mockRestTemplate.exchange(eq(new URI("https://demo.repository.io/v2/_catalog")),
			eq(HttpMethod.GET), any(), eq(Map.class))).thenThrow(httpClientErrorException);

	String b = "{\"auths\":{\"demo.repository.io\":{\"username\":\"testuser\",\"password\":\"testpassword\",\"auth\":\"YWRtaW46SGFyYm9yMTIzNDU=\"}}}";
	Map<String, RegistryConfiguration> result = converter.convert(b);

	assertThat(result.size(), is(1));
	assertTrue(result.containsKey("demo.repository.io"));

	RegistryConfiguration registryConfiguration = result.get("demo.repository.io");

	assertThat(registryConfiguration.getRegistryHost(), is("demo.repository.io"));
	assertThat(registryConfiguration.getUser(), is("testuser"));
	assertThat(registryConfiguration.getSecret(), is("testpassword"));
	assertThat(registryConfiguration.getAuthorizationType(), is(RegistryConfiguration.AuthorizationType.dockeroauth2));
	assertThat(registryConfiguration.getExtra().get("registryAuthUri"),
			is("https://demo.repository.io/service/token?service=demo-registry&scope=repository:{repository}:pull"));

}
 
Example 2
Source File: ApplicationConfigurationMetadataResolverAutoConfigurationTest.java    From spring-cloud-dataflow with Apache License 2.0 4 votes vote down vote up
@Bean(name = "noSslVerificationContainerRestTemplate")
RestTemplate noSslVerificationContainerRestTemplate() throws URISyntaxException {
	RestTemplate restTemplate = Mockito.mock(RestTemplate.class);

	// demo.repository.io
	HttpHeaders authenticateHeader = new HttpHeaders();
	authenticateHeader.add("Www-Authenticate", "Bearer realm=\"https://demo.repository.io/service/token\",service=\"demo-registry\",scope=\"registry:category:pull\"");
	HttpClientErrorException httpClientErrorException =
			HttpClientErrorException.create(HttpStatus.UNAUTHORIZED, "", authenticateHeader, new byte[0], null);

	when(restTemplate.exchange(eq(new URI("https://demo.repository.io/v2/_catalog")),
			eq(HttpMethod.GET), any(), eq(Map.class))).thenThrow(httpClientErrorException);


	when(restTemplate
			.exchange(
					eq(new URI("https://demo.repository.io/service/token?service=demo-registry&scope=repository:disabledssl/image:pull")),
					eq(HttpMethod.GET), any(), eq(Map.class)))
			.thenReturn(new ResponseEntity<>(Collections.singletonMap("token", "my_token_999"), HttpStatus.OK));

	when(restTemplate
			.exchange(
					eq(new URI("https://demo.repository.io/v2/disabledssl/image/manifests/1.0.0")),
					eq(HttpMethod.GET), any(), eq(Map.class)))
			.thenReturn(new ResponseEntity<>(Collections.singletonMap("config", Collections.singletonMap("digest", "test_digest")), HttpStatus.OK));

	when(restTemplate
			.exchange(
					eq(new URI("https://demo.repository.io/v2/disabledssl/image/blobs/test_digest")),
					eq(HttpMethod.GET), any(), eq(String.class)))
			.thenReturn(new ResponseEntity<>("{\"config\": {\"Labels\": {\"foo\": \"bar\"} } }", HttpStatus.OK));

	// demo.goharbor.io
	HttpHeaders authenticateHeader2 = new HttpHeaders();
	authenticateHeader2.add("Www-Authenticate", "Bearer realm=\"https://demo.goharbor.io/service/token\",service=\"demo-registry2\",scope=\"registry:category:pull\"");
	HttpClientErrorException httpClientErrorException2 =
			HttpClientErrorException.create(HttpStatus.UNAUTHORIZED, "", authenticateHeader2, new byte[0], null);

	when(restTemplate.exchange(eq(new URI("https://demo.goharbor.io/v2/_catalog")),
			eq(HttpMethod.GET), any(), eq(Map.class))).thenThrow(httpClientErrorException2);

	return restTemplate;
}