forked from square/retrofit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request square#342 from square/jw/mock-adapter
Add adapter for wrapping mock service implementations to mimic real behavior.
- Loading branch information
Showing
11 changed files
with
1,176 additions
and
47 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<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> | ||
|
||
<parent> | ||
<groupId>com.squareup.retrofit</groupId> | ||
<artifactId>parent</artifactId> | ||
<version>1.2.3-SNAPSHOT</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>retrofit-mock</artifactId> | ||
<name>Retrofit Mock Adapter</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.squareup.retrofit</groupId> | ||
<artifactId>retrofit</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.google.android</groupId> | ||
<artifactId>android</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.easytesting</groupId> | ||
<artifactId>fest-assert-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.guava</groupId> | ||
<artifactId>guava</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
37 changes: 37 additions & 0 deletions
37
retrofit-mock/src/main/java/retrofit/MockHttpException.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2013 Square, Inc. | ||
package retrofit; | ||
|
||
/** | ||
* An exception used to trigger the simulation of an HTTP error for mock services. | ||
* | ||
* @see MockRestAdapter | ||
*/ | ||
public class MockHttpException extends RuntimeException { | ||
public final int code; | ||
public final String reason; | ||
public final Object responseBody; | ||
|
||
/** | ||
* Create a new HTTP exception. | ||
* | ||
* @param code Corresponding HTTP status code to trigger. | ||
* @param responseBody Object to use as the contents of the response body. | ||
*/ | ||
public MockHttpException(int code, Object responseBody) { | ||
this(code, "", responseBody); | ||
} | ||
|
||
/** | ||
* Create a new HTTP exception. | ||
* | ||
* @param code HTTP status code to trigger. | ||
* @param reason HTTP status reason message. | ||
* @param responseBody Object to use as the contents of the response body. | ||
*/ | ||
public MockHttpException(int code, String reason, Object responseBody) { | ||
super("HTTP " + code + " " + reason); | ||
this.code = code; | ||
this.reason = reason; | ||
this.responseBody = responseBody; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
retrofit-mock/src/main/java/retrofit/MockHttpRetrofitError.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright 2013 Square, Inc. | ||
package retrofit; | ||
|
||
import java.lang.reflect.Type; | ||
import retrofit.client.Response; | ||
|
||
class MockHttpRetrofitError extends RetrofitError { | ||
private final Object body; | ||
|
||
MockHttpRetrofitError(String url, Response response, Object body) { | ||
super(url, response, null, null, false, null); | ||
this.body = body; | ||
} | ||
|
||
@Override public Object getBody() { | ||
return body; | ||
} | ||
|
||
@Override public Object getBodyAs(Type type) { | ||
return body; | ||
} | ||
} |
Oops, something went wrong.