Search in sources :

Example 1 with StringContentHandler

use of com.stanfy.enroscar.rest.response.handler.StringContentHandler in project enroscar by stanfy.

the class ContentHandlerTest method gotErrorShourldReadErrorStream.

@Test
public void gotErrorShourldReadErrorStream() throws Exception {
    assertThat(ResponseCache.getDefault()).isNull();
    scheduleBadMethodResponse();
    URLConnection connection = makeConnection(new MyRequestBuilder<String>(Robolectric.application) {
    }.setUrl(getWebServer().getUrl("/error").toString()).setOperationType(OperationType.SIMPLE_GET));
    boolean failed = false;
    try {
        read(connection);
    } catch (final IOException e) {
        failed = true;
    }
    getWebServer().takeRequest();
    assertThat(failed).isTrue();
    final String expected = scheduleBadMethodResponse();
    // send request once more
    connection = makeConnection(new MyRequestBuilder<String>(Robolectric.application) {

        {
            setRequestContentHandler(StringContentHandler.BEAN_NAME);
        }
    }.setUrl(getWebServer().getUrl("/error").toString()).setOperationType(OperationType.SIMPLE_GET));
    final StringContentHandler contentHandler = BeansManager.get(getApplication()).getContainer().getBean(StringContentHandler.BEAN_NAME, StringContentHandler.class);
    // we should test base behavior
    assertThat(contentHandler).isInstanceOf(BaseContentHandler.class);
    final String response = (String) contentHandler.getContent(connection);
    getWebServer().takeRequest();
    final HttpURLConnection http = (HttpURLConnection) UrlConnectionWrapper.unwrap(connection);
    assertThat(http.getResponseCode()).isEqualTo(HttpURLConnection.HTTP_BAD_METHOD);
    assertThat(response).isEqualTo(expected);
}
Also used : StringContentHandler(com.stanfy.enroscar.rest.response.handler.StringContentHandler) HttpURLConnection(java.net.HttpURLConnection) IOException(java.io.IOException) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) Test(org.junit.Test) AbstractMockServerTest(com.stanfy.enroscar.net.test.AbstractMockServerTest)

Example 2 with StringContentHandler

use of com.stanfy.enroscar.rest.response.handler.StringContentHandler in project enroscar by stanfy.

the class RemoteServerApiConfigurationTest method configureBean.

@Before
public void configureBean() {
    BeansManager manager = BeansManager.get(Robolectric.application);
    manager.edit().put("defaultCacheBeanName", new DummyResponseCache()).put("defaultContentHandlerName", new StringContentHandler(Robolectric.application)).put(RemoteServerApiConfiguration.class).commit();
    RemoteServerApiConfiguration config = manager.getContainer().getBean(RemoteServerApiConfiguration.class);
    config.setDefaultCacheBeanName("defaultCacheBeanName");
    config.setDefaultContentHandlerName("defaultContentHandlerName");
}
Also used : BeansManager(com.stanfy.enroscar.beans.BeansManager) StringContentHandler(com.stanfy.enroscar.rest.response.handler.StringContentHandler) RemoteServerApiConfiguration(com.stanfy.enroscar.rest.RemoteServerApiConfiguration) DummyResponseCache(com.stanfy.enroscar.net.test.cache.DummyResponseCache) Before(org.junit.Before)

Aggregations

StringContentHandler (com.stanfy.enroscar.rest.response.handler.StringContentHandler)2 BeansManager (com.stanfy.enroscar.beans.BeansManager)1 AbstractMockServerTest (com.stanfy.enroscar.net.test.AbstractMockServerTest)1 DummyResponseCache (com.stanfy.enroscar.net.test.cache.DummyResponseCache)1 RemoteServerApiConfiguration (com.stanfy.enroscar.rest.RemoteServerApiConfiguration)1 IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1 URLConnection (java.net.URLConnection)1 Before (org.junit.Before)1 Test (org.junit.Test)1