Created
July 28, 2010 21:47
-
-
Save harbulot/496447 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.http.HttpResponse; | |
import org.apache.http.client.HttpClient; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.junit.After; | |
import static org.junit.Assert.*; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.restlet.Component; | |
import org.restlet.data.MediaType; | |
import org.restlet.data.Protocol; | |
import org.restlet.representation.Representation; | |
import org.restlet.representation.StringRepresentation; | |
import org.restlet.resource.Get; | |
import org.restlet.resource.ServerResource; | |
public class GetWithContentTypeTest { | |
public static class SampleResource extends ServerResource { | |
@Override | |
public void doInit() { | |
setNegotiated(true); | |
} | |
@Get("json") | |
public Representation toJson() { | |
return new StringRepresentation("{ 'message': 'hello' }", | |
MediaType.APPLICATION_JSON); | |
} | |
@Get("html") | |
public Representation toHtml() { | |
return new StringRepresentation("<html><body>Hello</body></html>", | |
MediaType.TEXT_HTML); | |
} | |
} | |
private Component component; | |
@Before | |
public void startUp() throws Exception { | |
component = new Component(); | |
component.getServers().add(Protocol.HTTP, 8182); | |
component.getDefaultHost().attachDefault(SampleResource.class); | |
component.start(); | |
} | |
@Test | |
public void testDefaultGet() throws Exception { | |
HttpClient httpClient = new DefaultHttpClient(); | |
HttpGet httpGet = new HttpGet("http://localhost:8182/"); | |
HttpResponse response = httpClient.execute(httpGet); | |
String respMediaType = response.getFirstHeader("Content-Type") | |
.getValue().split(";")[0]; | |
System.out.println("MediaType (default): " + respMediaType); | |
} | |
@Test | |
public void testGetJson() throws Exception { | |
HttpClient httpClient = new DefaultHttpClient(); | |
HttpGet httpGet = new HttpGet("http://localhost:8182/"); | |
httpGet.setHeader("Accept", "application/json"); | |
HttpResponse response = httpClient.execute(httpGet); | |
String respMediaType = response.getFirstHeader("Content-Type") | |
.getValue().split(";")[0]; | |
System.out.println("MediaType (A: JSON): " + respMediaType); | |
assertEquals("application/json", respMediaType); | |
} | |
@Test | |
public void testGetJsonWithContentType() throws Exception { | |
HttpClient httpClient = new DefaultHttpClient(); | |
HttpGet httpGet = new HttpGet("http://localhost:8182/"); | |
httpGet.setHeader("Accept", "application/json"); | |
httpGet.setHeader("Content-Type", "application/x-www-form-urlencoded"); | |
HttpResponse response = httpClient.execute(httpGet); | |
String respMediaType = response.getFirstHeader("Content-Type") | |
.getValue().split(";")[0]; | |
System.out.println("MediaTypes (A: JSON, CT: FORM): " + respMediaType); | |
assertEquals("application/json", respMediaType); | |
} | |
@Test | |
public void testGetHtml() throws Exception { | |
HttpClient httpClient = new DefaultHttpClient(); | |
HttpGet httpGet = new HttpGet("http://localhost:8182/"); | |
httpGet.setHeader("Accept", "text/html"); | |
HttpResponse response = httpClient.execute(httpGet); | |
String respMediaType = response.getFirstHeader("Content-Type") | |
.getValue().split(";")[0]; | |
System.out.println("MediaType (A: HTML): " + respMediaType); | |
assertEquals("text/html", respMediaType); | |
} | |
@Test | |
public void testGetHtmlWithContentType() throws Exception { | |
HttpClient httpClient = new DefaultHttpClient(); | |
HttpGet httpGet = new HttpGet("http://localhost:8182/"); | |
httpGet.setHeader("Accept", "text/html"); | |
httpGet.setHeader("Content-Type", "application/x-www-form-urlencoded"); | |
HttpResponse response = httpClient.execute(httpGet); | |
String respMediaType = response.getFirstHeader("Content-Type") | |
.getValue().split(";")[0]; | |
System.out.println("MediaTypes (A: HTML, CT: FORM): " + respMediaType); | |
assertEquals("text/html", respMediaType); | |
} | |
@After | |
public void tearDown() throws Exception { | |
component.stop(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>test</groupId> | |
<artifactId>test</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>2.3.1</version> | |
<configuration> | |
<source>1.6</source> | |
<target>1.6</target> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
<repositories> | |
<repository> | |
<id>maven-restlet</id> | |
<name>Public online Restlet repository</name> | |
<url>http://maven.restlet.org</url> | |
</repository> | |
</repositories> | |
<dependencies> | |
<dependency> | |
<groupId>org.restlet.jse</groupId> | |
<artifactId>org.restlet</artifactId> | |
<version>2.0-RC4</version> | |
</dependency> | |
<dependency> | |
<groupId>org.restlet.jse</groupId> | |
<artifactId>org.restlet.ext.httpclient</artifactId> | |
<version>2.0-RC4</version> | |
</dependency> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.4</version> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment