Commits

Micha Kops  committed 9c42223

Cookie verification samples added.

  • Participants
  • Parent commits 25ea140

Comments (0)

Files changed (2)

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

 import java.util.ArrayList;
 import java.util.List;
 
+import javax.ws.rs.Consumes;
+import javax.ws.rs.CookieParam;
 import javax.ws.rs.GET;
 import javax.ws.rs.HeaderParam;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
 import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.NewCookie;
 import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
 
 @Path("/service")
 public class RestAssuredSampleService {
 				.header("anotherHeader", "bar").build();
 	}
 
+	@GET
+	@Produces(MediaType.APPLICATION_XML)
+	@Consumes(MediaType.APPLICATION_XML)
+	@Path("/contentype/accept")
+	public Response restrictToSingleContentType() {
+		return Response.noContent().build();
+	}
+
+	@GET
+	@Produces(MediaType.TEXT_PLAIN)
+	@Path("/access/cookie-token-secured")
+	public Response accessSecuredByCookie(
+			@CookieParam("authtoken") final String authToken) {
+		if ("abcdef".equals(authToken)) {
+			return Response.ok().build();
+		}
+
+		return Response.status(Status.FORBIDDEN).build();
+
+	}
+
+	@GET
+	@Produces(MediaType.TEXT_PLAIN)
+	@Path("/cookie/modify")
+	public Response modifyCookie(@Context final HttpHeaders httpHeaders,
+			@QueryParam("name") final String name) {
+		NewCookie cookie = new NewCookie("userName", name);
+		return Response.ok().cookie(cookie).build();
+	}
+
 	// TODO:
 	// Setting the Content Type
 	// Verifying the Content Type
-	// Setting Cookies
-	// Verifying Cookies
+
 	// Specifying Path Parameters
 	// File Uploads
 	// Registering custom parsers for MIME-types

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

 import static org.hamcrest.Matchers.hasXPath;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import groovyx.net.http.ContentType;
 
 import java.io.InputStream;
 
+import org.junit.Ignore;
 import org.junit.Test;
 
 import com.jayway.restassured.path.json.JsonPath;
 		expect().headers("customHeader1", "foo", "anotherHeader", "bar").when()
 				.get("/ra/service/header/multiple");
 	}
+
+	@Ignore("tbd .. @Consumes is set but status 200 returned")
+	@Test
+	public void testRestrictToSingleContentType() {
+		expect().contentType(ContentType.XML).statusCode(415).when().with()
+				.contentType(ContentType.JSON)
+				.get("/ra/service/contentype/accept");
+	}
+
+	@Test
+	public void testAccessSecuredByCookie() {
+		expect().statusCode(403).when()
+				.get("/ra/service/access/cookie-token-secured");
+		given().cookie("authtoken", "abcdef").expect().statusCode(200).when()
+				.get("/ra/service/access/cookie-token-secured");
+	}
+
+	@Test
+	public void testModifyCookie() {
+		expect().cookie("userName", equalTo("Ted")).when().with()
+				.param("name", "Ted").get("/ra/service/cookie/modify");
+		expect().cookie("userName", equalTo("Bill")).when().with()
+				.param("name", "Bill").get("/ra/service/cookie/modify");
+	}
 }