Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
沉迷打码的小凳子
glide
提交
4679c6e1
G
glide
项目概览
沉迷打码的小凳子
/
glide
与 Fork 源项目一致
从无法访问的项目Fork
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
glide
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
4679c6e1
编写于
9月 09, 2014
作者:
S
Sam Judd
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Handle relative redirects in HttpUrlFetcher.
Fixes #119
上级
526ae886
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
40 addition
and
6 deletion
+40
-6
library/src/androidTest/java/com/bumptech/glide/load/data/HttpUrlFetcherServerTest.java
...om/bumptech/glide/load/data/HttpUrlFetcherServerTest.java
+35
-5
library/src/main/java/com/bumptech/glide/load/data/HttpUrlFetcher.java
...ain/java/com/bumptech/glide/load/data/HttpUrlFetcher.java
+5
-1
未找到文件。
library/src/androidTest/java/com/bumptech/glide/load/data/HttpUrlFetcherServerTest.java
浏览文件 @
4679c6e1
...
...
@@ -4,6 +4,7 @@ import com.bumptech.glide.Priority;
import
com.bumptech.glide.load.model.GlideUrl
;
import
com.squareup.okhttp.mockwebserver.MockResponse
;
import
com.squareup.okhttp.mockwebserver.MockWebServer
;
import
com.squareup.okhttp.mockwebserver.RecordedRequest
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
...
...
@@ -14,13 +15,13 @@ import java.io.ByteArrayOutputStream;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.net.HttpURLConnection
;
import
java.net.MalformedURLException
;
import
java.net.SocketTimeoutException
;
import
java.net.URL
;
import
java.util.concurrent.TimeUnit
;
import
static
org
.
hamcrest
.
MatcherAssert
.
assertThat
;
import
static
org
.
hamcrest
.
Matchers
.
containsString
;
import
static
org
.
hamcrest
.
Matchers
.
endsWith
;
import
static
org
.
hamcrest
.
Matchers
.
equalTo
;
import
static
org
.
junit
.
Assert
.
fail
;
...
...
@@ -87,6 +88,23 @@ public class HttpUrlFetcherServerTest {
assertThat
(
isToString
(
is
),
equalTo
(
expected
));
}
@Test
public
void
testHandlesRelativeRedirects
()
throws
Exception
{
String
expected
=
"fakedata"
;
mockWebServer
.
enqueue
(
new
MockResponse
()
.
setResponseCode
(
301
)
.
setHeader
(
"Location"
,
"/redirect"
));
mockWebServer
.
enqueue
(
new
MockResponse
()
.
setResponseCode
(
200
)
.
setBody
(
expected
));
InputStream
is
=
getFetcher
().
loadData
(
Priority
.
NORMAL
);
assertThat
(
isToString
(
is
),
equalTo
(
expected
));
mockWebServer
.
takeRequest
();
RecordedRequest
second
=
mockWebServer
.
takeRequest
();
assertThat
(
second
.
getPath
(),
endsWith
(
"/redirect"
));
}
@Test
public
void
testHandlesUpToFiveRedirects
()
throws
Exception
{
int
numRedirects
=
4
;
...
...
@@ -127,14 +145,27 @@ public class HttpUrlFetcherServerTest {
}
@Test
public
void
testThrowsIfRedirectLocationIs
Empty
()
throws
Exception
{
public
void
testThrowsIfRedirectLocationIs
NotPresent
()
throws
Exception
{
mockWebServer
.
enqueue
(
new
MockResponse
().
setResponseCode
(
301
));
try
{
getFetcher
().
loadData
(
Priority
.
NORMAL
);
fail
(
"Didn't get expected IOException"
);
}
catch
(
MalformedURLException
e
)
{
// Expected
}
catch
(
IOException
e
)
{
// Expected.
}
}
@Test
public
void
testThrowsIfRedirectLocationIsPresentAndEmpty
()
throws
Exception
{
mockWebServer
.
enqueue
(
new
MockResponse
()
.
setResponseCode
(
301
).
setHeader
(
"Location"
,
""
));
try
{
getFetcher
().
loadData
(
Priority
.
NORMAL
);
fail
(
"Didn't get expected IOException"
);
}
catch
(
IOException
e
)
{
// Expected.
}
}
...
...
@@ -154,7 +185,6 @@ public class HttpUrlFetcherServerTest {
getFetcher
().
loadData
(
Priority
.
NORMAL
);
}
@Test
(
expected
=
IOException
.
class
)
public
void
testThrowsIfStatusCodeIs500
()
throws
Exception
{
mockWebServer
.
enqueue
(
new
MockResponse
().
setResponseCode
(
500
));
...
...
library/src/main/java/com/bumptech/glide/load/data/HttpUrlFetcher.java
浏览文件 @
4679c6e1
package
com.bumptech.glide.load.data
;
import
android.text.TextUtils
;
import
com.bumptech.glide.Priority
;
import
com.bumptech.glide.load.model.GlideUrl
;
...
...
@@ -60,7 +61,10 @@ public class HttpUrlFetcher implements DataFetcher<InputStream> {
return
stream
;
}
else
if
(
statusCode
/
100
==
3
)
{
String
redirectUrlString
=
urlConnection
.
getHeaderField
(
"Location"
);
URL
redirectUrl
=
new
URL
(
redirectUrlString
);
if
(
TextUtils
.
isEmpty
(
redirectUrlString
))
{
throw
new
IOException
(
"Received empty or null redirect url"
);
}
URL
redirectUrl
=
new
URL
(
url
,
redirectUrlString
);
return
loadDataWithRedirects
(
redirectUrl
,
redirects
+
1
,
url
);
}
else
{
if
(
statusCode
==
-
1
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录