first step towards ParameterList refactoring

This commit is contained in:
Pablo Fernandez committed 2011-12-14 13:31:12 -03:00
1 parent 56561acdac
commit ce531cdc38
11 files changed
+318 -123

No files matched your search

@@ -9,7 +9,7 @@ import org.scribe.oauth.*;
public class TwitterExample
{
private static final String PROTECTED_RESOURCE_URL = "http://api.twitter.com/1/account/verify_credentials.xml";
private static final String PROTECTED_RESOURCE_URL = "https://api.twitter.com/1/statuses/update.json";
public static void main(String[] args)
{
@@ -45,7 +45,8 @@ public class TwitterExample
// Now let's go and ask for a protected resource!
System.out.println("Now we're going to access a protected resource...");
OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
OAuthRequest request = new OAuthRequest(Verb.POST, PROTECTED_RESOURCE_URL);
request.addBodyParameter("status", "this is sparta! *");
service.signRequest(accessToken, request);
Response response = request.send();
System.out.println("Got it! Lets see what we found...");
@@ -0,0 +1,62 @@
package org.scribe.model;
import java.util.HashMap;
import java.util.Map;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.scribe.utils.URLUtils;
/**
* @author: Pablo Fernandez
*/
public class ParameterListTest
{
private ParameterList params;
@Before
public void setup()
{
this.params = new ParameterList();
}
@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionWhenAppendingNullMapToQuerystring()
{
String url = null;
params.appendTo(url);
}
@Test
public void shouldAppendNothingToQuerystringIfGivenEmptyMap()
{
String url = "http://www.example.com";
Assert.assertEquals(url, params.appendTo(url));
}
@Test
public void shouldAppendParametersToSimpleUrl()
{
String url = "http://www.example.com";
String expectedUrl = "http://www.example.com?param1=value1&param2=value%20with%20spaces";
params.add("param1", "value1");
params.add("param2", "value with spaces");
url = params.appendTo(url);
Assert.assertEquals(url, expectedUrl);
}
@Test
public void shouldAppendParametersToUrlWithQuerystring()
{
String url = "http://www.example.com?already=present";
String expectedUrl = "http://www.example.com?already=present&param1=value1&param2=value%20with%20spaces";
params.add("param1", "value1");
params.add("param2", "value with spaces");
url = params.appendTo(url);
Assert.assertEquals(url, expectedUrl);
}
}
@@ -34,8 +34,7 @@ public class RequestTest
{
assertEquals(2, getRequest.getQueryStringParams().size());
assertEquals(0, postRequest.getQueryStringParams().size());
assertTrue(getRequest.getQueryStringParams().containsKey("qsparam"));
assertTrue(getRequest.getQueryStringParams().get("qsparam").equals("value"));
assertTrue(getRequest.getQueryStringParams().contains(new Parameter("qsparam", "value")));
}
@Test
@@ -51,7 +50,7 @@ public class RequestTest
@Test
public void shouldSetBodyParamsAndAddContentLength()
{
assertEquals("param=value&param+with+spaces=value+with+spaces", postRequest.getBodyContents());
assertEquals("param=value&param%20with%20spaces=value%20with%20spaces", postRequest.getBodyContents());
postRequest.send();
assertTrue(connection.getHeaders().containsKey("Content-Length"));
}
@@ -77,7 +76,7 @@ public class RequestTest
@Test
public void shouldHandleQueryStringSpaceEncodingProperly()
{
assertTrue(getRequest.getQueryStringParams().get("other param").equals("value with spaces"));
assertTrue(getRequest.getQueryStringParams().contains(new Parameter("other param","value with spaces")));
}
@Test
@@ -101,51 +101,6 @@ public class URLUtilsTest
URLUtils.formURLDecode(toDecode);
}
@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionWhenAppendingNullMapToQuerystring()
{
String url = "http://www.example.com";
Map<String, String> nullMap = null;
URLUtils.appendParametersToQueryString(url, nullMap);
}
@Test
public void shouldAppendNothingToQuerystringIfGivenEmptyMap()
{
String url = "http://www.example.com";
Map<String, String> emptyMap = new HashMap<String, String>();
String newUrl = URLUtils.appendParametersToQueryString(url, emptyMap);
Assert.assertEquals(url, newUrl);
}
@Test
public void shouldAppendParametersToSimpleUrl()
{
String url = "http://www.example.com";
String expectedUrl = "http://www.example.com?param1=value1&param2=value+with+spaces";
Map<String, String> params = new HashMap<String, String>();
params.put("param1", "value1");
params.put("param2", "value with spaces");
url = URLUtils.appendParametersToQueryString(url, params);
Assert.assertEquals(url, expectedUrl);
}
@Test
public void shouldAppendParametersToUrlWithQuerystring()
{
String url = "http://www.example.com?already=present";
String expectedUrl = "http://www.example.com?already=present&param1=value1&param2=value+with+spaces";
Map<String, String> params = new HashMap<String, String>();
params.put("param1", "value1");
params.put("param2", "value with spaces");
url = URLUtils.appendParametersToQueryString(url, params);
Assert.assertEquals(url, expectedUrl);
}
@Test
public void shouldPercentEncodePlusSymbol()
{