Commits

Micha Kops  committed 6357aec

Authentication test examples added.

  • Participants
  • Parent commits 7d074a5

Comments (0)

Files changed (2)

File src/main/java/com/hascode/ra_samples/RestAssuredSampleService.java

 	}
 
 	@GET
-	@Produces(MediaType.APPLICATION_JSON)
+	@Produces(MediaType.TEXT_PLAIN)
 	@Path("/secure/person")
-	public Person secureGetPerson() {
-		Person person = new Person();
-		person.setEmail("test@hascode.com");
-		person.setFirstName("Tim");
-		person.setLastName("Testerman");
-		person.setId(1L);
-		return person;
+	public String secureGetPerson() {
+		return "Ok";
 	}
 
 }

File src/test/java/it/RestAssuredSampleServiceIT.java

 	@Ignore
 	@Test
 	public void testGetSingleUser() {
-		expect().statusCode(equalTo(200))
+		expect().statusCode(200)
 				.body("email", equalTo("test@hascode.com"), "firstName",
 						equalTo("Tim"), "lastName", equalTo("Testerman"), "id",
 						equalTo("1")).when().get("/ra/service/single-user");
 	@Ignore
 	@Test
 	public void testGetSingleUserAsXml() {
-		expect().statusCode(equalTo(200))
+		expect().statusCode(200)
 				.body("user.email", equalTo("test@hascode.com"),
 						"user.firstName", equalTo("Tim"), "user.lastName",
 						equalTo("Testerman"), "user.id", equalTo("1")).when()
 	@Ignore
 	@Test
 	public void testGetPersons() {
-		expect().statusCode(equalTo(200))
+		expect().statusCode(200)
 				.body(hasXPath("//person[@id='1']/email[.='test@hascode.com'] and firstName='Tim' and lastName='Testerman'"))
 				.body(hasXPath("//person[@id='20']/email[.='dev@hascode.com'] and firstName='Sara' and lastName='Stevens'"))
 				.body(hasXPath("//person[@id='1']/email[.='devnull@hascode.com'] and firstName='Mark' and lastName='Mustache'"))
 	public void testGetSingleUserAgainstSchema() {
 		InputStream xsd = getClass().getResourceAsStream("/user.xsd");
 		assertNotNull(xsd);
-		expect().statusCode(equalTo(200)).body(matchesXsd(xsd)).when()
+		expect().statusCode(200).body(matchesXsd(xsd)).when()
 				.get("/ra/service/single-user/xml");
 	}
 
 	public void testStatusNotFound() {
 		expect().statusCode(404).when().get("/ra/service/status/notfound");
 	}
+
+	@Test
+	public void testAuthenticationWorking() {
+		// we're not authenticated, service returns "401 Unauthorized"
+		expect().statusCode(401).when().get("/ra/service/secure/person");
+
+		// with authentication it is working
+		expect().statusCode(200).body(equalTo("Ok")).when().with()
+				.authentication().basic("admin", "admin")
+				.get("/ra/service/secure/person");
+	}
 }