Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
SDK
/
exoplayer
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
c8087510
authored
Jan 19, 2021
by
christosts
Committed by
Oliver Woodman
Jan 19, 2021
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Add contract tests for OkHttpDataSource
PiperOrigin-RevId: 352554949
parent
60f000c8
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
189 additions
and
100 deletions
extensions/okhttp/src/test/java/com/google/android/exoplayer2/ext/okhttp/OkHttpDataSourceContractTest.java
library/core/src/test/java/com/google/android/exoplayer2/upstream/DefaultHttpDataSourceContractTest.java
testutils/src/main/java/com/google/android/exoplayer2/testutil/HttpDataSourceTestEnv.java
extensions/okhttp/src/test/java/com/google/android/exoplayer2/ext/okhttp/OkHttpDataSourceContractTest.java
0 → 100644
View file @
c8087510
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
com
.
google
.
android
.
exoplayer2
.
ext
.
okhttp
;
import
android.net.Uri
;
import
androidx.test.ext.junit.runners.AndroidJUnit4
;
import
com.google.android.exoplayer2.testutil.DataSourceContractTest
;
import
com.google.android.exoplayer2.testutil.HttpDataSourceTestEnv
;
import
com.google.android.exoplayer2.upstream.DataSource
;
import
com.google.common.collect.ImmutableList
;
import
okhttp3.OkHttpClient
;
import
org.junit.Rule
;
import
org.junit.runner.RunWith
;
/** {@link DataSource} contract tests for {@link OkHttpDataSource}. */
@RunWith
(
AndroidJUnit4
.
class
)
public
class
OkHttpDataSourceContractTest
extends
DataSourceContractTest
{
@Rule
public
HttpDataSourceTestEnv
httpDataSourceTestEnv
=
new
HttpDataSourceTestEnv
();
@Override
protected
DataSource
createDataSource
()
{
return
new
OkHttpDataSource
.
Factory
(
new
OkHttpClient
()).
createDataSource
();
}
@Override
protected
ImmutableList
<
TestResource
>
getTestResources
()
{
return
httpDataSourceTestEnv
.
getServedResources
();
}
@Override
protected
Uri
getNotFoundUri
()
{
return
Uri
.
parse
(
httpDataSourceTestEnv
.
getNonexistentUrl
());
}
}
library/core/src/test/java/com/google/android/exoplayer2/upstream/DefaultHttpDataSourceContractTest.java
View file @
c8087510
...
@@ -18,92 +18,16 @@ package com.google.android.exoplayer2.upstream;
...
@@ -18,92 +18,16 @@ package com.google.android.exoplayer2.upstream;
import
android.net.Uri
;
import
android.net.Uri
;
import
androidx.test.ext.junit.runners.AndroidJUnit4
;
import
androidx.test.ext.junit.runners.AndroidJUnit4
;
import
com.google.android.exoplayer2.testutil.DataSourceContractTest
;
import
com.google.android.exoplayer2.testutil.DataSourceContractTest
;
import
com.google.android.exoplayer2.testutil.TestUtil
;
import
com.google.android.exoplayer2.testutil.HttpDataSourceTestEnv
;
import
com.google.android.exoplayer2.testutil.WebServerDispatcher
;
import
com.google.common.collect.ImmutableList
;
import
com.google.common.collect.ImmutableList
;
import
java.util.function.Function
;
import
org.junit.Rule
;
import
okhttp3.HttpUrl
;
import
okhttp3.mockwebserver.Dispatcher
;
import
okhttp3.mockwebserver.MockResponse
;
import
okhttp3.mockwebserver.MockWebServer
;
import
okhttp3.mockwebserver.RecordedRequest
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.runner.RunWith
;
import
org.junit.runner.RunWith
;
/** {@link DataSource} contract tests for {@link DefaultHttpDataSource}. */
/** {@link DataSource} contract tests for {@link DefaultHttpDataSource}. */
@RunWith
(
AndroidJUnit4
.
class
)
@RunWith
(
AndroidJUnit4
.
class
)
public
class
DefaultHttpDataSourceContractTest
extends
DataSourceContractTest
{
public
class
DefaultHttpDataSourceContractTest
extends
DataSourceContractTest
{
private
static
int
seed
=
0
;
@Rule
public
HttpDataSourceTestEnv
httpDataSourceTestEnv
=
new
HttpDataSourceTestEnv
();
private
static
final
WebServerDispatcher
.
Resource
RANGE_SUPPORTED
=
new
WebServerDispatcher
.
Resource
.
Builder
()
.
setPath
(
"/supports/range-requests"
)
.
setData
(
TestUtil
.
buildTestData
(
/* length= */
20
,
seed
++))
.
supportsRangeRequests
(
true
)
.
build
();
private
static
final
WebServerDispatcher
.
Resource
RANGE_SUPPORTED_LENGTH_UNKNOWN
=
new
WebServerDispatcher
.
Resource
.
Builder
()
.
setPath
(
"/supports/range-requests-length-unknown"
)
.
setData
(
TestUtil
.
buildTestData
(
/* length= */
20
,
seed
++))
.
supportsRangeRequests
(
true
)
.
resolvesToUnknownLength
(
true
)
.
build
();
private
static
final
WebServerDispatcher
.
Resource
RANGE_NOT_SUPPORTED
=
new
WebServerDispatcher
.
Resource
.
Builder
()
.
setPath
(
"/doesnt/support/range-requests"
)
.
setData
(
TestUtil
.
buildTestData
(
/* length= */
20
,
seed
++))
.
supportsRangeRequests
(
false
)
.
build
();
private
static
final
WebServerDispatcher
.
Resource
RANGE_NOT_SUPPORTED_LENGTH_UNKNOWN
=
new
WebServerDispatcher
.
Resource
.
Builder
()
.
setPath
(
"/doesnt/support/range-requests-length-unknown"
)
.
setData
(
TestUtil
.
buildTestData
(
/* length= */
20
,
seed
++))
.
supportsRangeRequests
(
false
)
.
resolvesToUnknownLength
(
true
)
.
build
();
private
static
final
WebServerDispatcher
.
Resource
REDIRECTS_TO_RANGE_SUPPORTED
=
RANGE_SUPPORTED
.
buildUpon
().
setPath
(
"/redirects/to/range/supported"
).
build
();
private
final
MockWebServer
originServer
=
new
MockWebServer
();
private
final
MockWebServer
redirectionServer
=
new
MockWebServer
();
@Before
public
void
startServers
()
throws
Exception
{
originServer
.
start
();
originServer
.
setDispatcher
(
WebServerDispatcher
.
forResources
(
ImmutableList
.
of
(
RANGE_SUPPORTED
,
RANGE_SUPPORTED_LENGTH_UNKNOWN
,
RANGE_NOT_SUPPORTED
,
RANGE_NOT_SUPPORTED_LENGTH_UNKNOWN
)));
redirectionServer
.
start
();
redirectionServer
.
setDispatcher
(
new
Dispatcher
()
{
@Override
public
MockResponse
dispatch
(
RecordedRequest
request
)
{
if
(
request
.
getPath
().
equals
(
REDIRECTS_TO_RANGE_SUPPORTED
.
getPath
()))
{
return
new
MockResponse
()
.
setResponseCode
(
302
)
.
setHeader
(
"Location"
,
originServer
.
url
(
RANGE_SUPPORTED
.
getPath
()).
toString
());
}
else
{
return
new
MockResponse
().
setResponseCode
(
404
);
}
}
});
}
@After
public
void
shutdownServers
()
throws
Exception
{
originServer
.
shutdown
();
redirectionServer
.
shutdown
();
}
@Override
@Override
protected
DataSource
createDataSource
()
{
protected
DataSource
createDataSource
()
{
...
@@ -112,30 +36,11 @@ public class DefaultHttpDataSourceContractTest extends DataSourceContractTest {
...
@@ -112,30 +36,11 @@ public class DefaultHttpDataSourceContractTest extends DataSourceContractTest {
@Override
@Override
protected
ImmutableList
<
TestResource
>
getTestResources
()
{
protected
ImmutableList
<
TestResource
>
getTestResources
()
{
return
ImmutableList
.
of
(
return
httpDataSourceTestEnv
.
getServedResources
();
toTestResource
(
"range supported"
,
RANGE_SUPPORTED
,
originServer:
:
url
),
toTestResource
(
"range supported, length unknown"
,
RANGE_SUPPORTED_LENGTH_UNKNOWN
,
originServer:
:
url
),
toTestResource
(
"range not supported"
,
RANGE_NOT_SUPPORTED
,
originServer:
:
url
),
toTestResource
(
"range not supported, length unknown"
,
RANGE_NOT_SUPPORTED_LENGTH_UNKNOWN
,
originServer:
:
url
),
toTestResource
(
"302 redirect"
,
REDIRECTS_TO_RANGE_SUPPORTED
,
redirectionServer:
:
url
));
}
}
@Override
@Override
protected
Uri
getNotFoundUri
()
{
protected
Uri
getNotFoundUri
()
{
return
Uri
.
parse
(
originServer
.
url
(
"/not/a/real/path"
).
toString
());
return
Uri
.
parse
(
httpDataSourceTestEnv
.
getNonexistentUrl
());
}
private
static
TestResource
toTestResource
(
String
name
,
WebServerDispatcher
.
Resource
resource
,
Function
<
String
,
HttpUrl
>
urlResolver
)
{
return
new
TestResource
.
Builder
()
.
setName
(
name
)
.
setUri
(
Uri
.
parse
(
urlResolver
.
apply
(
resource
.
getPath
()).
toString
()))
.
setExpectedBytes
(
resource
.
getData
())
.
setEndOfInputExpected
(!
resource
.
resolvesToUnknownLength
())
.
build
();
}
}
}
}
testutils/src/main/java/com/google/android/exoplayer2/testutil/HttpDataSourceTestEnv.java
0 → 100644
View file @
c8087510
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
com
.
google
.
android
.
exoplayer2
.
testutil
;
import
android.net.Uri
;
import
com.google.android.exoplayer2.upstream.HttpDataSource
;
import
com.google.common.collect.ImmutableList
;
import
java.io.IOException
;
import
okhttp3.mockwebserver.Dispatcher
;
import
okhttp3.mockwebserver.MockResponse
;
import
okhttp3.mockwebserver.MockWebServer
;
import
okhttp3.mockwebserver.RecordedRequest
;
import
org.junit.Rule
;
import
org.junit.rules.ExternalResource
;
/** A JUnit {@link Rule} that creates test resources for {@link HttpDataSource} contract tests. */
public
class
HttpDataSourceTestEnv
extends
ExternalResource
{
private
static
int
seed
=
0
;
private
static
final
WebServerDispatcher
.
Resource
RANGE_SUPPORTED
=
new
WebServerDispatcher
.
Resource
.
Builder
()
.
setPath
(
"/supports/range-requests"
)
.
setData
(
TestUtil
.
buildTestData
(
/* length= */
20
,
seed
++))
.
supportsRangeRequests
(
true
)
.
build
();
private
static
final
WebServerDispatcher
.
Resource
RANGE_SUPPORTED_LENGTH_UNKNOWN
=
new
WebServerDispatcher
.
Resource
.
Builder
()
.
setPath
(
"/supports/range-requests-length-unknown"
)
.
setData
(
TestUtil
.
buildTestData
(
/* length= */
20
,
seed
++))
.
supportsRangeRequests
(
true
)
.
resolvesToUnknownLength
(
true
)
.
build
();
private
static
final
WebServerDispatcher
.
Resource
RANGE_NOT_SUPPORTED
=
new
WebServerDispatcher
.
Resource
.
Builder
()
.
setPath
(
"/doesnt/support/range-requests"
)
.
setData
(
TestUtil
.
buildTestData
(
/* length= */
20
,
seed
++))
.
supportsRangeRequests
(
false
)
.
build
();
private
static
final
WebServerDispatcher
.
Resource
RANGE_NOT_SUPPORTED_LENGTH_UNKNOWN
=
new
WebServerDispatcher
.
Resource
.
Builder
()
.
setPath
(
"/doesnt/support/range-requests-length-unknown"
)
.
setData
(
TestUtil
.
buildTestData
(
/* length= */
20
,
seed
++))
.
supportsRangeRequests
(
false
)
.
resolvesToUnknownLength
(
true
)
.
build
();
private
static
final
WebServerDispatcher
.
Resource
REDIRECTS_TO_RANGE_SUPPORTED
=
RANGE_SUPPORTED
.
buildUpon
().
setPath
(
"/redirects/to/range/supported"
).
build
();
private
final
MockWebServer
originServer
=
new
MockWebServer
();
private
final
MockWebServer
redirectionServer
=
new
MockWebServer
();
public
ImmutableList
<
DataSourceContractTest
.
TestResource
>
getServedResources
()
{
return
ImmutableList
.
of
(
createTestResource
(
"range supported"
,
RANGE_SUPPORTED
),
createTestResource
(
"range supported, length unknown"
,
RANGE_SUPPORTED_LENGTH_UNKNOWN
),
createTestResource
(
"range not supported"
,
RANGE_NOT_SUPPORTED
),
createTestResource
(
"range not supported"
,
RANGE_NOT_SUPPORTED
),
createTestResource
(
"range not supported, length unknown"
,
RANGE_NOT_SUPPORTED_LENGTH_UNKNOWN
),
createTestResource
(
"302 redirect"
,
REDIRECTS_TO_RANGE_SUPPORTED
,
/* server= */
redirectionServer
));
}
public
String
getNonexistentUrl
()
{
return
originServer
.
url
(
"/not/a/real/path"
).
toString
();
}
@Override
protected
void
before
()
throws
Throwable
{
originServer
.
start
();
originServer
.
setDispatcher
(
WebServerDispatcher
.
forResources
(
ImmutableList
.
of
(
RANGE_SUPPORTED
,
RANGE_SUPPORTED_LENGTH_UNKNOWN
,
RANGE_NOT_SUPPORTED
,
RANGE_NOT_SUPPORTED_LENGTH_UNKNOWN
)));
redirectionServer
.
start
();
redirectionServer
.
setDispatcher
(
new
Dispatcher
()
{
@Override
public
MockResponse
dispatch
(
RecordedRequest
request
)
{
if
(
request
.
getPath
().
equals
(
REDIRECTS_TO_RANGE_SUPPORTED
.
getPath
()))
{
return
new
MockResponse
()
.
setResponseCode
(
302
)
.
setHeader
(
"Location"
,
originServer
.
url
(
RANGE_SUPPORTED
.
getPath
()).
toString
());
}
else
{
return
new
MockResponse
().
setResponseCode
(
404
);
}
}
});
}
@Override
protected
void
after
()
{
try
{
originServer
.
shutdown
();
redirectionServer
.
shutdown
();
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
private
DataSourceContractTest
.
TestResource
createTestResource
(
String
name
,
WebServerDispatcher
.
Resource
resource
)
{
return
createTestResource
(
name
,
resource
,
originServer
);
}
private
static
DataSourceContractTest
.
TestResource
createTestResource
(
String
name
,
WebServerDispatcher
.
Resource
resource
,
MockWebServer
server
)
{
return
new
DataSourceContractTest
.
TestResource
.
Builder
()
.
setName
(
name
)
.
setUri
(
Uri
.
parse
(
server
.
url
(
resource
.
getPath
()).
toString
()))
.
setExpectedBytes
(
resource
.
getData
())
.
setEndOfInputExpected
(!
resource
.
resolvesToUnknownLength
())
.
build
();
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment