Skip to content

Instantly share code, notes, and snippets.

@swanson
Last active January 11, 2024 16:42
Show Gist options
  • Select an option

  • Save swanson/7dee3f3474e30fe8f15c to your computer and use it in GitHub Desktop.

Select an option

Save swanson/7dee3f3474e30fe8f15c to your computer and use it in GitHub Desktop.

Revisions

  1. matt swanson revised this gist Jan 14, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -57,7 +57,7 @@ public Response execute(Request request) throws IOException {
    }

    TypedInput body = new TypedInputStream(mimeType, inputStream.available(), inputStream);
    return new Response(200, "Content from res/raw/" + fileName, new ArrayList<Header>(), body);
    return new Response(request.getUrl(), 200, "Content from res/raw/" + fileName, new ArrayList<Header>(), body);
    }

    private static class TypedInputStream implements TypedInput {
  2. matt swanson revised this gist Feb 23, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions gistfile2.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    A Retrofit Client that reads JSON files from disk and returns them as the response (to be handled by GSON). Make sure the context you pass is from the instrumentation app and put your JSON files in `res/raw` in the instrumentation project. Inspired by Ruby's `vcr` gem.

    A `GET` request to `/articles` will look for the JSON file `res/raw/get_articles.json`, a `POST` request to `/articles/123/` will look for the JSON file `res/raw/post_articles_123.json`, etc.

    We use `setScenario()` if we want to test alternate responses so that a failed login `POST` to `/login` will read from `res/raw/failed_login_post_login.json` (and the happy path can use `post_login.json`).
  3. matt swanson revised this gist Feb 23, 2014. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions gistfile2.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    A `GET` request to `/articles` will look for the JSON file `res/raw/get_articles.json`, a `POST` request to `/articles/123/` will look for the JSON file `res/raw/post_articles_123.json`, etc.

    We use `setScenario()` if we want to test alternate responses so that a failed login `POST` to `/login` will read from `res/raw/failed_login_post_login.json` (and the happy path can use `post_login.json`).
  4. matt swanson revised this gist Feb 23, 2014. No changes.
  5. matt swanson revised this gist Feb 23, 2014. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -45,15 +45,16 @@ public Response execute(Request request) throws IOException {
    context.getPackageName());

    if (resourceId == 0) {
    Log.wtf("FORUM", "Could not find res/raw/" + fileName + ".json");
    Log.wtf("YourTag", "Could not find res/raw/" + fileName + ".json");
    throw new IOException("Could not find res/raw/" + fileName + ".json");
    }

    InputStream inputStream = context.getResources().openRawResource(resourceId);

    String mimeType = URLConnection.guessContentTypeFromStream(inputStream);
    if (mimeType == null)
    if (mimeType == null) {
    mimeType = "application/json";
    }

    TypedInput body = new TypedInputStream(mimeType, inputStream.available(), inputStream);
    return new Response(200, "Content from res/raw/" + fileName, new ArrayList<Header>(), body);
  6. matt swanson created this gist Feb 23, 2014.
    88 changes: 88 additions & 0 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,88 @@
    import android.annotation.SuppressLint;
    import android.content.Context;
    import android.util.Log;

    import retrofit.client.Client;
    import retrofit.client.Header;
    import retrofit.client.Request;
    import retrofit.client.Response;
    import retrofit.mime.TypedInput;

    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.ArrayList;

    @SuppressLint("DefaultLocale")
    public class LocalJsonClient implements Client {
    private Context context;

    private String scenario = null;

    public LocalJsonClient(Context ctx) {
    this.context = ctx;
    }

    public void setScenario(String scenario) {
    this.scenario = scenario;
    }

    @Override
    public Response execute(Request request) throws IOException {
    URL requestedUrl = new URL(request.getUrl());
    String requestedMethod = request.getMethod();

    String prefix = "";
    if (this.scenario != null) {
    prefix = scenario + "_";
    }

    String fileName = (prefix + requestedMethod + requestedUrl.getPath()).replace("/", "_");
    fileName = fileName.toLowerCase();

    int resourceId = context.getResources().getIdentifier(fileName, "raw",
    context.getPackageName());

    if (resourceId == 0) {
    Log.wtf("FORUM", "Could not find res/raw/" + fileName + ".json");
    throw new IOException("Could not find res/raw/" + fileName + ".json");
    }

    InputStream inputStream = context.getResources().openRawResource(resourceId);

    String mimeType = URLConnection.guessContentTypeFromStream(inputStream);
    if (mimeType == null)
    mimeType = "application/json";

    TypedInput body = new TypedInputStream(mimeType, inputStream.available(), inputStream);
    return new Response(200, "Content from res/raw/" + fileName, new ArrayList<Header>(), body);
    }

    private static class TypedInputStream implements TypedInput {
    private final String mimeType;
    private final long length;
    private final InputStream stream;

    private TypedInputStream(String mimeType, long length, InputStream stream) {
    this.mimeType = mimeType;
    this.length = length;
    this.stream = stream;
    }

    @Override
    public String mimeType() {
    return mimeType;
    }

    @Override
    public long length() {
    return length;
    }

    @Override
    public InputStream in() throws IOException {
    return stream;
    }
    }
    }