Skip to content

Commit

Permalink
TFJ-161 adding a test case for AccessToken
Browse files Browse the repository at this point in the history
git-svn-id: http://yusuke.homeip.net/svn/twitter4j/trunk@318 117b7e0d-5933-0410-9d29-ab41bb01d86b
  • Loading branch information
yusuke committed Jun 3, 2009
1 parent cb55237 commit 26ffa80
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/main/java/twitter4j/http/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,8 @@ public static String encodeParameters(PostParameter[] postParams) {
buf.append("&");
}
try {
buf.append(postParams[j].name).append("=").append(
URLEncoder.encode(postParams[j].value, "UTF-8"));
buf.append(URLEncoder.encode(postParams[j].name, "UTF-8"))
.append("=").append(URLEncoder.encode(postParams[j].value, "UTF-8"));
} catch (java.io.UnsupportedEncodingException neverHappen) {
}
}
Expand Down
35 changes: 30 additions & 5 deletions src/test/java/twitter4j/http/OAuthTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,36 @@ public void testGetToken() throws Exception{
} catch (TwitterException te) {
assertEquals(401, te.getStatusCode());
}
AccessToken token = new AccessToken("oauth_token=6377362-kW0YV1ymaqEUCSHP29ux169mDeA4kQfhEuqkdvHk&oauth_token_secret=ghoTpd7LuMLHtJDyHkhYo40Uq5bWSxeCyOUAkbsOoOY&user_id=6377362&screen_name=twit4j2");
assertEquals("6377362-kW0YV1ymaqEUCSHP29ux169mDeA4kQfhEuqkdvHk", token.getToken());
assertEquals("ghoTpd7LuMLHtJDyHkhYo40Uq5bWSxeCyOUAkbsOoOY", token.getTokenSecret());
assertEquals("twit4j2", token.getScreenName());
assertEquals(6377362, token.getUserId());

HttpClient http = new HttpClient();
Response response = http.get(rt.getAuthorizationURL());
String cookie = response.getResponseHeader("Set-Cookie");
http.setRequestHeader("Cookie", cookie);
String resStr = response.asString();
String authorizeURL = catchPattern(resStr, "<form action=\"","\" id=\"login_form\"");
PostParameter[] params = new PostParameter[4];
params[0] = new PostParameter("authenticity_token"
, catchPattern(resStr, "\"authenticity_token\" type=\"hidden\" value=\"", "\" />"));
params[1] = new PostParameter("oauth_token",
catchPattern(resStr,"name=\"oauth_token\" type=\"hidden\" value=\"","\" />"));
params[2] = new PostParameter("session[username_or_email]",id1);
params[3] = new PostParameter("session[password]",pass1);
response = http.post(authorizeURL, params);
AccessToken at;
at = twitter.getOAuthAccessToken(rt.getToken(),rt.getTokenSecret());
assertEquals(at.getScreenName(),id1);
assertEquals(at.getUserId(),6358482);

at = new AccessToken("oauth_token=6377362-kW0YV1ymaqEUCSHP29ux169mDeA4kQfhEuqkdvHk&oauth_token_secret=ghoTpd7LuMLHtJDyHkhYo40Uq5bWSxeCyOUAkbsOoOY&user_id=6377362&screen_name=twit4j2");
assertEquals("6377362-kW0YV1ymaqEUCSHP29ux169mDeA4kQfhEuqkdvHk", at.getToken());
assertEquals("ghoTpd7LuMLHtJDyHkhYo40Uq5bWSxeCyOUAkbsOoOY", at.getTokenSecret());
assertEquals("twit4j2", at.getScreenName());
assertEquals(6377362, at.getUserId());
}
private static String catchPattern(String body, String before, String after){
int beforeIndex = body.indexOf(before);
int afterIndex = body.indexOf(after, beforeIndex);
return body.substring(beforeIndex + before.length(), afterIndex);
}

public void testSign() throws Exception {
Expand Down

0 comments on commit 26ffa80

Please sign in to comment.