Search in sources :

Example 1 with UrlConnectionBuilder

use of com.stanfy.enroscar.net.UrlConnectionBuilder in project enroscar by stanfy.

the class CacheSwitcherTest method perform.

private void perform(final String path, final SimpleFileCache cache, final String response, final boolean cached) throws IOException {
    final URLConnection connection = new UrlConnectionBuilder().setUrl(getWebServer().getUrl(path)).setCacheManagerName(cache.getName()).create();
    assertResponse(connection, response, cached);
    assertThat(cache.getWriteSuccessCount()).isEqualTo(1);
    assertThat(cache.getHitCount()).isEqualTo(cached ? 1 : 0);
    // check switcher connections stack
    assertThat(ResponseCacheSwitcher.getLastUrlConnection()).isNull();
}
Also used : UrlConnectionBuilder(com.stanfy.enroscar.net.UrlConnectionBuilder) URLConnection(java.net.URLConnection)

Example 2 with UrlConnectionBuilder

use of com.stanfy.enroscar.net.UrlConnectionBuilder in project enroscar by stanfy.

the class CacheWrapperTest method shouldAffectCoreCache.

@Test
public void shouldAffectCoreCache() throws Exception {
    final String text = "ABC";
    getWebServer().enqueue(new MockResponse().setBody(text));
    InputStream stream = new UrlConnectionBuilder().setCacheManagerName("cacheWrapper").setUrl(getWebServer().getUrl("/")).create().getInputStream();
    IoUtils.consumeStream(stream, BeansManager.get(getApplication()).getContainer().getBean(BuffersPool.class));
    // cache entry has been written to the CORE cache
    assertThat(cache.getWriteSuccessCount()).isEqualTo(1);
    assertThat(cache.getHitCount()).isZero();
    // we can read from cache
    stream = new UrlConnectionBuilder().setCacheManagerName("cacheWrapper").setUrl(getWebServer().getUrl("/")).create().getInputStream();
    final String response = IoUtils.streamToString(stream, null);
    assertThat(response).isEqualTo(text);
    assertThat(cache.getWriteSuccessCount()).isEqualTo(1);
    assertThat(cache.getHitCount()).isEqualTo(1);
}
Also used : UrlConnectionBuilder(com.stanfy.enroscar.net.UrlConnectionBuilder) MockResponse(com.squareup.okhttp.mockwebserver.MockResponse) BuffersPool(com.stanfy.enroscar.io.BuffersPool) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 3 with UrlConnectionBuilder

use of com.stanfy.enroscar.net.UrlConnectionBuilder in project enroscar by stanfy.

the class RetrofitClientTest method init.

@Before
public void init() throws Exception {
    EnroscarConnectionsEngine.config().treatFileScheme(false).setup(Robolectric.application);
    testThing = new Thing();
    testThing.name = "foo!";
    mockResponse = new Gson().toJson(testThing);
    File f = new File(Robolectric.application.getFilesDir(), "resp");
    FileOutputStream out = new FileOutputStream(f);
    out.write(mockResponse.getBytes());
    out.close();
    testResponseInput = new FileInputStream(f);
    mockResolver = mock(ContentResolver.class);
    doReturn("application/json").when(mockResolver).getType(any(Uri.class));
    AssetFileDescriptor mockDescriptor = mock(AssetFileDescriptor.class);
    doReturn(testResponseInput).when(mockDescriptor).createInputStream();
    doReturn(mockDescriptor).when(mockResolver).openAssetFileDescriptor(any(Uri.class), any(String.class));
    client = new RetrofitClient(new UrlConnectionBuilderFactory() {

        @Override
        public UrlConnectionBuilder newUrlConnectionBuilder() {
            return new UrlConnectionBuilder() {

                @Override
                public URLConnection create() throws IOException {
                    URL url = getUrl();
                    if ("content".equals(url.getProtocol())) {
                        return new ContentUriConnection(url, mockResolver);
                    }
                    return super.create();
                }
            };
        }
    });
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) UrlConnectionBuilderFactory(com.stanfy.enroscar.net.UrlConnectionBuilderFactory) ContentUriConnection(com.stanfy.enroscar.net.ContentUriConnection) Gson(com.google.gson.Gson) Uri(android.net.Uri) FileInputStream(java.io.FileInputStream) URL(java.net.URL) ContentResolver(android.content.ContentResolver) UrlConnectionBuilder(com.stanfy.enroscar.net.UrlConnectionBuilder) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Before(org.junit.Before)

Aggregations

UrlConnectionBuilder (com.stanfy.enroscar.net.UrlConnectionBuilder)3 ContentResolver (android.content.ContentResolver)1 AssetFileDescriptor (android.content.res.AssetFileDescriptor)1 Uri (android.net.Uri)1 Gson (com.google.gson.Gson)1 MockResponse (com.squareup.okhttp.mockwebserver.MockResponse)1 BuffersPool (com.stanfy.enroscar.io.BuffersPool)1 ContentUriConnection (com.stanfy.enroscar.net.ContentUriConnection)1 UrlConnectionBuilderFactory (com.stanfy.enroscar.net.UrlConnectionBuilderFactory)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 Before (org.junit.Before)1 Test (org.junit.Test)1