Skip to content

Instantly share code, notes, and snippets.

@geapi
Last active November 18, 2016 19:14
Show Gist options
  • Save geapi/31c75d845bc309011be9abea3f13602a to your computer and use it in GitHub Desktop.
Save geapi/31c75d845bc309011be9abea3f13602a to your computer and use it in GitHub Desktop.
example of using basic auth in RestTemplate call
package tips.cloudnative.todotest;
// imports
import static tips.cloudnative.testauthentication.TestAuthentication.basicAuthHeaders;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class TodoTest {
@Test
public void testListTodos() {
RestTemplate restTemplate = new RestTemplate();
HttpEntity<String> requestEntity = new HttpEntity<>(basicAuthHeaders());
ResponseEntity<List<Todo>> response = restTemplate.exchange(
"http://localhost:" + port + "/todos",
HttpMethod.GET,
requestEntity,
Todo);
Todo returnedTodo = response.getBody();
assertThat(returnedTodo).isEqualTo(new Todo(1L, "renew New York Times subscription", false)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment