Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
正在写bug并嘿嘿一笑
JustAuth
提交
bccf3279
J
JustAuth
项目概览
正在写bug并嘿嘿一笑
/
JustAuth
与 Fork 源项目一致
Fork自
justauth / JustAuth
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
JustAuth
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
bccf3279
编写于
3月 28, 2020
作者:
智布道
👁
浏览文件
操作
浏览文件
下载
差异文件
✅
完善单测
上级
6b4a8b78
469558bd
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
99 addition
and
16 deletion
+99
-16
src/test/java/me/zhyd/oauth/utils/GlobalAuthUtilsTest.java
src/test/java/me/zhyd/oauth/utils/GlobalAuthUtilsTest.java
+96
-14
src/test/java/me/zhyd/oauth/utils/JsonPathTest.java
src/test/java/me/zhyd/oauth/utils/JsonPathTest.java
+3
-2
未找到文件。
src/test/java/me/zhyd/oauth/utils/GlobalAuthUtilsTest.java
浏览文件 @
bccf3279
package
me.zhyd.oauth.utils
;
import
com.alibaba.fastjson.JSON
;
import
me.zhyd.oauth.config.AuthConfig
;
import
me.zhyd.oauth.model.AuthCallback
;
import
me.zhyd.oauth.model.AuthToken
;
...
...
@@ -8,35 +9,35 @@ import org.junit.Test;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.TreeMap
;
import
static
me
.
zhyd
.
oauth
.
config
.
AuthDefaultSource
.
TWITTER
;
import
static
me
.
zhyd
.
oauth
.
utils
.
GlobalAuthUtils
.
generateTwitterSignature
;
import
static
me
.
zhyd
.
oauth
.
utils
.
GlobalAuthUtils
.
urlEncode
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
public
class
GlobalAuthUtilsTest
{
@Test
public
void
testG
enerateDingTalkSignature
()
{
public
void
g
enerateDingTalkSignature
()
{
assertEquals
(
"mLTZEMqIlpAA3xtJ43KcRT0EDLwgSamFe%2FNis5lq9ik%3D"
,
GlobalAuthUtils
.
generateDingTalkSignature
(
"SHA-256"
,
"1562325753000 "
));
}
@Test
public
void
testU
rlDecode
()
{
public
void
u
rlDecode
()
{
assertEquals
(
""
,
GlobalAuthUtils
.
urlDecode
(
null
));
assertEquals
(
"https://www.foo.bar"
,
GlobalAuthUtils
.
urlDecode
(
"https://www.foo.bar"
));
assertEquals
(
"mLTZEMqIlpAA3xtJ43KcRT0EDLwgSamFe/Nis5lq9ik="
,
GlobalAuthUtils
.
urlDecode
(
"mLTZEMqIlpAA3xtJ43KcRT0EDLwgSamFe%2FNis5lq9ik%3D"
));
}
@Test
public
void
testP
arseStringToMap
()
{
public
void
p
arseStringToMap
()
{
Map
expected
=
new
HashMap
();
expected
.
put
(
"bar"
,
"baz"
);
assertEquals
(
expected
,
GlobalAuthUtils
.
parseStringToMap
(
"foo&bar=baz"
));
}
@Test
public
void
testI
sHttpProtocol
()
{
public
void
i
sHttpProtocol
()
{
Assert
.
assertFalse
(
GlobalAuthUtils
.
isHttpProtocol
(
""
));
Assert
.
assertFalse
(
GlobalAuthUtils
.
isHttpProtocol
(
"foo"
));
...
...
@@ -44,7 +45,7 @@ public class GlobalAuthUtilsTest {
}
@Test
public
void
testI
sHttpsProtocol
()
{
public
void
i
sHttpsProtocol
()
{
Assert
.
assertFalse
(
GlobalAuthUtils
.
isHttpsProtocol
(
""
));
Assert
.
assertFalse
(
GlobalAuthUtils
.
isHttpsProtocol
(
"foo"
));
...
...
@@ -52,7 +53,7 @@ public class GlobalAuthUtilsTest {
}
@Test
public
void
testI
sLocalHost
()
{
public
void
i
sLocalHost
()
{
Assert
.
assertFalse
(
GlobalAuthUtils
.
isLocalHost
(
"foo"
));
Assert
.
assertTrue
(
GlobalAuthUtils
.
isLocalHost
(
""
));
...
...
@@ -76,9 +77,9 @@ public class GlobalAuthUtilsTest {
params
.
put
(
"oauth_version"
,
"1.0"
);
String
baseUrl
=
"https://api.twitter.com/oauth/request_token"
;
params
.
put
(
"oauth_signature"
,
generateTwitterSignature
(
params
,
"POST"
,
baseUrl
,
config
.
getClientSecret
(),
null
));
params
.
put
(
"oauth_signature"
,
GlobalAuthUtils
.
generateTwitterSignature
(
params
,
"POST"
,
baseUrl
,
config
.
getClientSecret
(),
null
));
params
.
forEach
((
k
,
v
)
->
params
.
put
(
k
,
"\""
+
urlEncode
(
v
.
toString
()
)
+
"\""
));
params
.
forEach
((
k
,
v
)
->
params
.
put
(
k
,
"\""
+
GlobalAuthUtils
.
urlEncode
(
v
)
+
"\""
));
String
actual
=
"OAuth "
+
GlobalAuthUtils
.
parseMapToString
(
params
,
false
).
replaceAll
(
"&"
,
", "
);
assertEquals
(
"OAuth oauth_nonce=\"sTj7Ivg73u052eXstpoS1AWQCynuDEPN\", oauth_signature=\"%2BL5Jq%2FTaKubge04cWw%2B4yfjFlaU%3D\", oauth_callback=\"https%3A%2F%2Fcodinglife.tech\", oauth_consumer_key=\"HD0XLqzi5Wz0G08rh45Cg8mgh\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1569750981\", oauth_version=\"1.0\""
,
actual
);
...
...
@@ -103,10 +104,10 @@ public class GlobalAuthUtilsTest {
params
.
put
(
"oauth_verifier"
,
authCallback
.
getOauthVerifier
());
params
.
put
(
"oauth_version"
,
"1.0"
);
params
.
put
(
"oauth_signature"
,
generateTwitterSignature
(
params
,
"POST"
,
TWITTER
.
accessToken
(),
config
.
getClientSecret
(),
authCallback
params
.
put
(
"oauth_signature"
,
GlobalAuthUtils
.
generateTwitterSignature
(
params
,
"POST"
,
TWITTER
.
accessToken
(),
config
.
getClientSecret
(),
authCallback
.
getOauthToken
()));
params
.
forEach
((
k
,
v
)
->
params
.
put
(
k
,
"\""
+
urlEncode
(
v
.
toString
()
)
+
"\""
));
params
.
forEach
((
k
,
v
)
->
params
.
put
(
k
,
"\""
+
GlobalAuthUtils
.
urlEncode
(
v
)
+
"\""
));
String
actual
=
"OAuth "
+
GlobalAuthUtils
.
parseMapToString
(
params
,
false
).
replaceAll
(
"&"
,
", "
);
assertEquals
(
"OAuth oauth_verifier=\"lYou4gxfA6S5KioUa8VF8HCShzA2nSxp\", oauth_nonce=\"sTj7Ivg73u052eXstpoS1AWQCynuDEPN\", oauth_signature=\"9i0lmWgvphtkl2KcCO9VyZ3K2%2F0%3D\", oauth_token=\"W_KLmAAAAAAAxq5LAAABbXxJeD0\", oauth_consumer_key=\"HD0XLqzi5Wz0G08rh45Cg8mgh\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1569751082\", oauth_version=\"1.0\""
,
actual
);
...
...
@@ -139,11 +140,92 @@ public class GlobalAuthUtilsTest {
queryParams
.
put
(
"include_entities"
,
Boolean
.
toString
(
true
));
Map
<
String
,
String
>
params
=
new
HashMap
<>(
queryParams
);
oauthParams
.
put
(
"oauth_signature"
,
generateTwitterSignature
(
params
,
"GET"
,
TWITTER
.
userInfo
(),
config
.
getClientSecret
(),
authToken
oauthParams
.
put
(
"oauth_signature"
,
GlobalAuthUtils
.
generateTwitterSignature
(
params
,
"GET"
,
TWITTER
.
userInfo
(),
config
.
getClientSecret
(),
authToken
.
getOauthTokenSecret
()));
oauthParams
.
forEach
((
k
,
v
)
->
oauthParams
.
put
(
k
,
"\""
+
urlEncode
(
v
.
toString
()
)
+
"\""
));
oauthParams
.
forEach
((
k
,
v
)
->
oauthParams
.
put
(
k
,
"\""
+
GlobalAuthUtils
.
urlEncode
(
v
)
+
"\""
));
String
actual
=
"OAuth "
+
GlobalAuthUtils
.
parseMapToString
(
oauthParams
,
false
).
replaceAll
(
"&"
,
", "
);
assertEquals
(
"OAuth oauth_nonce=\"sTj7Ivg73u052eXstpoS1AWQCynuDEPN\", oauth_signature=\"yHHq2J1W5QLAO8gGipnY1V%2Bzxqk%3D\", oauth_token=\"1961977975-PcFQaCnpN9h9xqtqHwHlpGBXFrHJ9bOLy7OtGAL\", oauth_consumer_key=\"HD0XLqzi5Wz0G08rh45Cg8mgh\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1569751082\", oauth_version=\"1.0\""
,
actual
);
}
@Test
public
void
md5
()
{
String
str
=
"helloworld,iamjustauth"
;
String
md5Str
=
GlobalAuthUtils
.
md5
(
str
);
assertEquals
(
"b0d923de4289b69976448cac718528b8"
,
md5Str
);
}
@Test
public
void
treemap
()
{
Map
<
String
,
Object
>
parameters
=
new
HashMap
<>();
parameters
.
put
(
"user_id"
,
"1"
);
parameters
.
put
(
"screen_name"
,
"222"
);
parameters
.
put
(
"a"
,
"222"
);
parameters
.
put
(
"include_entities"
,
Boolean
.
toString
(
true
));
final
Map
<
String
,
Object
>
sorted
=
new
TreeMap
<>(
parameters
);
assertEquals
(
"{\"a\":\"222\",\"include_entities\":\"true\",\"screen_name\":\"222\",\"user_id\":\"1\"}"
,
JSON
.
toJSONString
(
sorted
));
}
@Test
public
void
urlEncode
()
{
assertEquals
(
""
,
GlobalAuthUtils
.
urlEncode
(
null
));
assertEquals
(
"https%3A%2F%2Fwww.foo.bar"
,
GlobalAuthUtils
.
urlEncode
(
"https://www.foo.bar"
));
assertEquals
(
"mLTZEMqIlpAA3xtJ43KcRT0EDLwgSamFe%252FNis5lq9ik%253D"
,
GlobalAuthUtils
.
urlEncode
(
"mLTZEMqIlpAA3xtJ43KcRT0EDLwgSamFe%2FNis5lq9ik%3D"
));
}
@Test
public
void
parseMapToString
()
{
Map
<
String
,
String
>
parameters
=
new
HashMap
<>();
parameters
.
put
(
"user_id"
,
"1"
);
parameters
.
put
(
"screen_name"
,
"史上最全的第三方授权登录库"
);
parameters
.
put
(
"include_entities"
,
Boolean
.
toString
(
true
));
assertEquals
(
"user_id=1&screen_name=史上最全的第三方授权登录库&include_entities=true"
,
GlobalAuthUtils
.
parseMapToString
(
parameters
,
false
));
assertEquals
(
"user_id=1&screen_name=%E5%8F%B2%E4%B8%8A%E6%9C%80%E5%85%A8%E7%9A%84%E7%AC%AC%E4%B8%89%E6%96%B9%E6%8E%88%E6%9D%83%E7%99%BB%E5%BD%95%E5%BA%93&include_entities=true"
,
GlobalAuthUtils
.
parseMapToString
(
parameters
,
true
));
assertEquals
(
""
,
GlobalAuthUtils
.
parseMapToString
(
null
,
true
));
}
@Test
public
void
generateNonce
()
{
assertEquals
(
10
,
GlobalAuthUtils
.
generateNonce
(
10
).
length
());
}
@Test
public
void
getTimestamp
()
{
assertNotNull
(
GlobalAuthUtils
.
getTimestamp
());
}
@Test
public
void
generateTwitterSignature
()
{
Map
<
String
,
String
>
queryParams
=
new
HashMap
<>();
queryParams
.
put
(
"name"
,
"你好"
);
queryParams
.
put
(
"gender"
,
"male"
);
assertEquals
(
"J6MAQH1kcgUdj2jmygN3rdfI4lo="
,
GlobalAuthUtils
.
generateTwitterSignature
(
queryParams
,
"GET"
,
TWITTER
.
userInfo
(),
"xxxxx"
,
"xxxxx"
));
}
@Test
public
void
generateElemeSignature
()
{
Map
<
String
,
Object
>
parameters
=
new
HashMap
<>();
parameters
.
put
(
"name"
,
"你好"
);
parameters
.
put
(
"gender"
,
"male"
);
String
appKey
=
"appKey"
;
String
secret
=
"appKey"
;
long
timestamp
=
1233456789
;
String
action
=
"appKey"
;
String
token
=
"appKey"
;
assertEquals
(
"26FEB8BF7E84FED2619D9C5D97F421BD"
,
GlobalAuthUtils
.
generateElemeSignature
(
appKey
,
secret
,
timestamp
,
action
,
token
,
parameters
));
}
@Test
public
void
generateJdSignature
()
{
Map
<
String
,
Object
>
parameters
=
new
HashMap
<>();
parameters
.
put
(
"name"
,
"你好"
);
parameters
.
put
(
"gender"
,
"male"
);
String
appSecret
=
"appKey"
;
assertEquals
(
"FE04EC03BA8A619802CF309959C2B43F"
,
GlobalAuthUtils
.
generateJdSignature
(
appSecret
,
parameters
));
}
}
src/test/java/me/zhyd/oauth/utils/JsonPathTest.java
浏览文件 @
bccf3279
...
...
@@ -3,6 +3,7 @@ package me.zhyd.oauth.utils;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONObject
;
import
com.alibaba.fastjson.JSONPath
;
import
org.junit.Assert
;
import
org.junit.Test
;
import
java.util.ArrayList
;
...
...
@@ -29,9 +30,9 @@ public class JsonPathTest {
Map
<
String
,
Object
>
master
=
new
HashMap
<>();
//
master.put("elements", list);
master
.
put
(
"elements"
,
list
);
JSONObject
emailObj
=
JSONObject
.
parseObject
(
JSON
.
toJSONString
(
master
));
Object
object
=
JSONPath
.
eval
(
emailObj
,
"$['elements'][0]['handle~']['emailAddress']"
);
System
.
out
.
println
(
object
);
Assert
.
assertEquals
(
"xxxx"
,
object
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录