Commits

Micha Kops committed f29dbda

Jersey Dependencies updated, Jersey Multipart Dependencies added, Examples for file uploads and custom parsers added

  • Participants
  • Parent commits 9c42223

Comments (0)

Files changed (4)

 	<packaging>war</packaging>
 	<version>0.0.1-SNAPSHOT</version>
 	<name>rest-assured-examples-webapp Maven Webapp</name>
-	<url>http://maven.apache.org</url>
+	<url>http://www.hascode.com</url>
+	
+	<properties>
+		<jersey-version>1.9.1</jersey-version>
+	</properties>
+	
 	<dependencies>
 		<dependency>
 			<groupId>com.sun.jersey</groupId>
 			<artifactId>jersey-server</artifactId>
-			<version>1.9</version>
+			<version>${jersey-version}</version>
 		</dependency>
 		<dependency>
 			<groupId>javax.xml.bind</groupId>
 		<dependency>
 			<groupId>com.sun.jersey</groupId>
 			<artifactId>jersey-core</artifactId>
-			<version>1.9</version>
+			<version>${jersey-version}</version>
 		</dependency>
 		<dependency>
 			<groupId>com.sun.jersey</groupId>
 			<artifactId>jersey-json</artifactId>
-			<version>1.9</version>
+			<version>${jersey-version}</version>
 		</dependency>
 		<dependency>
 			<groupId>junit</groupId>
 			<artifactId>junit</artifactId>
 			<version>4.10</version>
+			<scope>test</scope>
 		</dependency>
 		<dependency>
 			<groupId>com.jayway.restassured</groupId>
 			<artifactId>rest-assured</artifactId>
-			<version>1.2.3</version>
+			<version>1.4</version>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.commons</groupId>
+			<artifactId>commons-io</artifactId>
+			<version>1.3.2</version>
+		</dependency>
+		<dependency>
+			<groupId>com.sun.jersey.contribs</groupId>
+			<artifactId>jersey-multipart</artifactId>
+			<version>${jersey-version}</version>
 		</dependency>
 	</dependencies>
 	<repositories>
 			</plugin>
 		</plugins>
 	</build>
+	<description>hasCode.com REST Service and REST-assured integration tests.</description>
 </project>

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

 package com.hascode.ra_samples;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.StringWriter;
 import java.util.ArrayList;
 import java.util.List;
 
 import javax.ws.rs.CookieParam;
 import javax.ws.rs.GET;
 import javax.ws.rs.HeaderParam;
+import javax.ws.rs.POST;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
 import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.Status;
 
+import org.apache.commons.io.IOUtils;
+import org.codehaus.jettison.json.JSONException;
+import org.codehaus.jettison.json.JSONObject;
+
+import com.sun.jersey.core.header.FormDataContentDisposition;
+import com.sun.jersey.multipart.FormDataParam;
+
 @Path("/service")
 public class RestAssuredSampleService {
 	@GET
 		return Response.ok().cookie(cookie).build();
 	}
 
+	@POST
+	@Consumes(MediaType.MULTIPART_FORM_DATA)
+	@Path("/file/upload")
+	public String contentFromFileupload(
+			@FormDataParam("file") final InputStream file,
+			@FormDataParam("file") final FormDataContentDisposition fileDisposition) {
+		try {
+			StringWriter writer = new StringWriter();
+			IOUtils.copy(file, writer);
+			return writer.toString();
+		} catch (IOException e) {
+		}
+		return "";
+	}
+
+	@GET
+	@Produces("text/json")
+	@Path("/detail/json")
+	public String getDetailsAsJson() {
+		try {
+			JSONObject obj;
+			obj = new JSONObject("{\"test\":true}");
+			return obj.toString();
+		} catch (JSONException e) {
+		}
+		return "";
+	}
 	// TODO:
 	// Setting the Content Type
 	// Verifying the Content Type
 
-	// Specifying Path Parameters
-	// File Uploads
-	// Registering custom parsers for MIME-types
 	// Setting default values
 	// Specification reuse
 

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 static org.junit.Assert.assertTrue;
 import groovyx.net.http.ContentType;
 
+import java.io.File;
 import java.io.InputStream;
 
 import org.junit.Ignore;
 import org.junit.Test;
 
+import com.jayway.restassured.RestAssured;
+import com.jayway.restassured.parsing.Parser;
 import com.jayway.restassured.path.json.JsonPath;
 import com.jayway.restassured.response.Response;
 
 		expect().cookie("userName", equalTo("Bill")).when().with()
 				.param("name", "Bill").get("/ra/service/cookie/modify");
 	}
+
+	@Test
+	public void testFileUpload() {
+		final File file = new File(getClass().getClassLoader()
+				.getResource("test.txt").getFile());
+		assertNotNull(file);
+		assertTrue(file.canRead());
+		given().multiPart(file).expect()
+				.body(equalTo("This is an uploaded test file.")).when()
+				.post("/ra/service/file/upload");
+	}
+
+	@Test
+	public void testRegisterParserForUnknownContentType() {
+		RestAssured.registerParser("text/json", Parser.JSON);
+		expect().body("test", equalTo(true)).when()
+				.get("/ra/service/detail/json");
+	}
 }

src/test/resources/test.txt

+This is an uploaded test file.