Initial commit
This commit is contained in:
commit
bfbe536886
59 files changed
+2540
No files matched your search
@@ -0,0 +1,80 @@
|
||||
package org.scribe.model;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
|
||||
public class ConnectionStub extends HttpURLConnection
|
||||
{
|
||||
|
||||
private Map<String, String> headers = new HashMap<String, String>();
|
||||
private Map<String, List<String>> responseHeaders = new HashMap<String, List<String>>();
|
||||
private int inputStreamCalled = 0;
|
||||
|
||||
public ConnectionStub() throws Exception
|
||||
{
|
||||
super(new URL("http://example.com"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRequestProperty(String key, String value)
|
||||
{
|
||||
headers.put(key, value);
|
||||
}
|
||||
|
||||
public Map<String, String> getHeaders()
|
||||
{
|
||||
return headers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getResponseCode() throws IOException
|
||||
{
|
||||
return 200;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getInputStream() throws IOException
|
||||
{
|
||||
inputStreamCalled++;
|
||||
return new ByteArrayInputStream("contents".getBytes());
|
||||
}
|
||||
|
||||
public int getTimesCalledInpuStream()
|
||||
{
|
||||
return inputStreamCalled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public OutputStream getOutputStream() throws IOException
|
||||
{
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
baos.write("contents".getBytes());
|
||||
return baos;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, List<String>> getHeaderFields()
|
||||
{
|
||||
return responseHeaders;
|
||||
}
|
||||
|
||||
public void addResponseHeader(String key, String value)
|
||||
{
|
||||
responseHeaders.put(key, Arrays.asList(value));
|
||||
}
|
||||
|
||||
public void connect() throws IOException
|
||||
{
|
||||
}
|
||||
|
||||
public void disconnect()
|
||||
{
|
||||
}
|
||||
|
||||
public boolean usingProxy()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.scribe.model;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.*;
|
||||
import org.scribe.model.Request.*;
|
||||
|
||||
public class OAuthConfigTest
|
||||
{
|
||||
|
||||
@Test
|
||||
public void shouldReturnDefaultValuesIfNotSet()
|
||||
{
|
||||
OAuthConfig config = new OAuthConfig();
|
||||
assertEquals(Verb.GET, config.getAccessTokenVerb());
|
||||
assertEquals(Verb.GET, config.getRequestTokenVerb());
|
||||
assertEquals(OAuthConstants.OUT_OF_BAND, config.getCallback());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldOverrideDefaultsIfSet()
|
||||
{
|
||||
OAuthConfig config = new OAuthConfig();
|
||||
config.setCallback("http://callback");
|
||||
config.setAccessTokenVerb(Verb.POST);
|
||||
config.setRequestTokenVerb(Verb.POST);
|
||||
assertEquals("http://callback", config.getCallback());
|
||||
assertEquals(Verb.POST, config.getAccessTokenVerb());
|
||||
assertEquals(Verb.POST, config.getRequestTokenVerb());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package org.scribe.model;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.*;
|
||||
import org.scribe.model.Request.*;
|
||||
|
||||
public class OAuthRequestTest
|
||||
{
|
||||
|
||||
private OAuthRequest request;
|
||||
|
||||
@Before
|
||||
public void setup()
|
||||
{
|
||||
request = new OAuthRequest(Verb.GET, "http://example.com");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldAddOAuthParamters()
|
||||
{
|
||||
request.addOAuthParameter(OAuthConstants.TOKEN, "token");
|
||||
request.addOAuthParameter(OAuthConstants.NONCE, "nonce");
|
||||
request.addOAuthParameter(OAuthConstants.TIMESTAMP, "ts");
|
||||
|
||||
assertEquals(3, request.getOauthParameters().size());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void shouldThrowExceptionIfParameterIsNotOAuth()
|
||||
{
|
||||
request.addOAuthParameter("otherParam", "value");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package org.scribe.model;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.*;
|
||||
import org.scribe.model.Request.*;
|
||||
|
||||
public class RequestTest
|
||||
{
|
||||
|
||||
private Request getRequest;
|
||||
private Request postRequest;
|
||||
private ConnectionStub connection;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception
|
||||
{
|
||||
connection = new ConnectionStub();
|
||||
postRequest = new Request(Verb.POST, "http://example.com");
|
||||
postRequest.setConnection(connection);
|
||||
getRequest = new Request(Verb.GET, "http://example.com?qsparam=value&other=value+with+spaces");
|
||||
getRequest.setConnection(connection);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSetRequestVerb()
|
||||
{
|
||||
getRequest.send();
|
||||
assertEquals("GET", connection.getRequestMethod());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetQueryStringParameters()
|
||||
{
|
||||
assertEquals(2, getRequest.getQueryStringParams().size());
|
||||
assertEquals(0, postRequest.getQueryStringParams().size());
|
||||
assertTrue(getRequest.getQueryStringParams().containsKey("qsparam"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldAddRequestHeaders()
|
||||
{
|
||||
getRequest.addHeader("Header", "1");
|
||||
getRequest.addHeader("Header2", "2");
|
||||
getRequest.send();
|
||||
assertEquals(2, getRequest.getHeaders().size());
|
||||
assertEquals(2, connection.getHeaders().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSetBodyParamsAndHeaders()
|
||||
{
|
||||
postRequest.addBodyParameter("param", "value");
|
||||
postRequest.addBodyParameter("param two", "value with spaces");
|
||||
postRequest.send();
|
||||
assertEquals("param+two=value+with+spaces¶m=value", postRequest.getBodyContents());
|
||||
assertTrue(connection.getHeaders().containsKey("Content-Length"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSetPayloadAndHeaders()
|
||||
{
|
||||
postRequest.addPayload("PAYLOAD");
|
||||
postRequest.send();
|
||||
assertEquals("PAYLOAD", postRequest.getBodyContents());
|
||||
assertTrue(connection.getHeaders().containsKey("Content-Length"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package org.scribe.model;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
import org.junit.*;
|
||||
|
||||
public class ResponseTest
|
||||
{
|
||||
|
||||
private Response response;
|
||||
private ConnectionStub connection;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception
|
||||
{
|
||||
connection = new ConnectionStub();
|
||||
connection.addResponseHeader("one", "one");
|
||||
connection.addResponseHeader("two", "two");
|
||||
response = new Response(connection);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPopulateResponseHeaders()
|
||||
{
|
||||
assertEquals(2, response.getHeaders().size());
|
||||
assertEquals("one", response.getHeader("one"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldParseBodyContents()
|
||||
{
|
||||
assertEquals("contents", response.getBody());
|
||||
assertEquals(1, connection.getTimesCalledInpuStream());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldParseBodyContentsOnlyOnce()
|
||||
{
|
||||
assertEquals("contents", response.getBody());
|
||||
assertEquals("contents", response.getBody());
|
||||
assertEquals("contents", response.getBody());
|
||||
assertEquals(1, connection.getTimesCalledInpuStream());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleAConnectionWithErrors() throws Exception
|
||||
{
|
||||
Response errResponse = new Response(new FaultyConnection());
|
||||
assertEquals(400, errResponse.getCode());
|
||||
assertEquals("errors", errResponse.getBody());
|
||||
}
|
||||
|
||||
private static class FaultyConnection extends ConnectionStub
|
||||
{
|
||||
|
||||
public FaultyConnection() throws Exception
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getErrorStream()
|
||||
{
|
||||
return new ByteArrayInputStream("errors".getBytes());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getResponseCode() throws IOException
|
||||
{
|
||||
return 400;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user