Created
February 16, 2015 09:28
-
-
Save thuytrinh/214d2ed1ca0a4d4aff23 to your computer and use it in GitHub Desktop.
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
public class GistFetcherTest extends InstrumentationTestCase { | |
public void testFetch() throws IOException { | |
final InputStream mockResponseStream = getInstrumentation().getContext().getAssets().open("mock_response.json"); | |
OkHttpClient httpClient = new OkHttpClient(); | |
httpClient.interceptors().add(new Interceptor() { | |
@Override | |
public Response intercept(Chain chain) throws IOException { | |
return new Response.Builder() | |
.protocol(Protocol.HTTP_2) | |
.code(200) // To make response.isSuccessful() return true. | |
.request(chain.request()) | |
.body(new ResponseBody() { | |
@Override | |
public MediaType contentType() { | |
return null; | |
} | |
@Override | |
public long contentLength() { | |
// Means we don't know the length beforehand. | |
return -1; | |
} | |
@Override | |
public BufferedSource source() { | |
try { | |
return new Buffer().readFrom(mockResponseStream); | |
} catch (IOException e) { | |
// Fail early! | |
Assertions.fail(e.getMessage(), e.getCause()); | |
return null; | |
} | |
} | |
}) | |
.build(); | |
} | |
}); | |
// This will be ignored. We won't execute any network request for it. | |
String url = "https://api.github.com/gists/AWESOME_GIST_ID"; | |
GistFetcher fetcher = new GistFetcher(httpClient, new Gson()); | |
GistFetcher.Gist gist = fetcher.fetch(url); | |
assertThat(gist).isNotNull(); | |
assertThat(gist.files()).hasSize(2).containsKeys("ObservableProperty.java", "usage.md"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment