Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull request to fix issue #9 #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/java/com/fourspaces/couchdb/CouchResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public class CouchResponse {

private String error_id;
private String error_reason;

private static final String EXCEPTION_ID = "exception";

/**
* C-tor parses the method results to build the CouchResponse object.
Expand Down Expand Up @@ -112,6 +114,23 @@ public class CouchResponse {
log.debug(toString());
}

/**
* C-tor that handles exceptions from the HTTP client
*
* @param req Original HTTP request to couchdb server
* @param exception Exception thrown during request to server
* @throws IOException
*/
CouchResponse(HttpRequestBase req, Exception exception) {
headers = new Header[0];
body = "";
path = req.getURI().getPath();

this.error_id = EXCEPTION_ID;
this.error_reason = exception.getMessage();
ok=false;
}

@Override
/**
* A better toString for this object... can be very verbose though.
Expand Down
6 changes: 6 additions & 0 deletions src/java/com/fourspaces/couchdb/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ protected CouchResponse http(HttpRequestBase req) {
entity = httpResponse.getEntity();
lastResponse = new CouchResponse(req, httpResponse);
} catch (IOException e) {
lastResponse = new CouchResponse(req, e);
log.error(ExceptionUtils.getStackTrace(e));
} finally {
if (entity != null) {
Expand All @@ -503,6 +504,11 @@ public void setUserAgent(String ua)
{
httpParams.setParameter(AllClientPNames.USER_AGENT, ua);
}

public void setHttpClient(HttpClient httpClient)
{
this.httpClient = httpClient;
}

public void setConnectionTimeout(int milliseconds)
{
Expand Down
41 changes: 41 additions & 0 deletions src/test/com/fourspaces/couchdb/SessionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.fourspaces.couchdb;

import com.fourspaces.couchdb.test.TestHttpClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.net.SocketTimeoutException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

public class SessionTest
{

@Before public void setup() {
}

@After public void tearDown() {
}


@Test
public void testTimeout() {
Session session = new Session("localhost", 5984);
String reason = "test timeout";
SocketTimeoutException exception = new SocketTimeoutException(reason);
TestHttpClient httpClient = new TestHttpClient(exception);
session.setHttpClient(httpClient);

//url doesn't matter since we'll mock a timeout exception
CouchResponse response = session.get("http://nowhere.example.com:5984/nosuchdb");

assertFalse(response.isOk());
assertEquals(response.getErrorId(), "exception");
assertEquals(response.getErrorReason(), reason);
}



}
98 changes: 98 additions & 0 deletions src/test/com/fourspaces/couchdb/test/TestHttpClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.fourspaces.couchdb.test;

import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;

import java.io.IOException;
import java.net.SocketTimeoutException;

/**
* Date: 11/10/12
*
* @author lreeder
*/
public class TestHttpClient implements HttpClient
{

private IOException exceptionToThrow = null;

public TestHttpClient()
{
}

public TestHttpClient(IOException exceptionToThrow)
{
this.exceptionToThrow = exceptionToThrow;
}

public HttpParams getParams()
{
throw new UnsupportedOperationException("Method getParams not implemented.");
}

public ClientConnectionManager getConnectionManager()
{
throw new UnsupportedOperationException("Method getConnectionManager not implemented.");
}

public HttpResponse execute(HttpUriRequest httpUriRequestIn) throws IOException, ClientProtocolException
{

if(exceptionToThrow != null)
{
throw exceptionToThrow;
}

throw new UnsupportedOperationException("Method execute not implemented.");
}

public HttpResponse execute(HttpUriRequest httpUriRequestIn,
HttpContext httpContextIn) throws IOException, ClientProtocolException
{
throw new UnsupportedOperationException("Method execute not implemented.");
}

public HttpResponse execute(HttpHost httpHostIn,
HttpRequest httpRequestIn) throws IOException, ClientProtocolException
{
throw new UnsupportedOperationException("Method execute not implemented.");
}

public HttpResponse execute(HttpHost httpHostIn, HttpRequest httpRequestIn,
HttpContext httpContextIn) throws IOException, ClientProtocolException
{
throw new UnsupportedOperationException("Method execute not implemented.");
}

public <T> T execute(HttpUriRequest httpUriRequestIn,
ResponseHandler<? extends T> responseHandlerIn) throws IOException, ClientProtocolException
{
throw new UnsupportedOperationException("Method execute not implemented.");
}

public <T> T execute(HttpUriRequest httpUriRequestIn, ResponseHandler<? extends T> responseHandlerIn,
HttpContext httpContextIn) throws IOException, ClientProtocolException
{
throw new UnsupportedOperationException("Method execute not implemented.");
}

public <T> T execute(HttpHost httpHostIn, HttpRequest httpRequestIn,
ResponseHandler<? extends T> responseHandlerIn) throws IOException, ClientProtocolException
{
throw new UnsupportedOperationException("Method execute not implemented.");
}

public <T> T execute(HttpHost httpHostIn, HttpRequest httpRequestIn, ResponseHandler<? extends T> responseHandlerIn,
HttpContext httpContextIn) throws IOException, ClientProtocolException
{
throw new UnsupportedOperationException("Method execute not implemented.");
}
}