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
a6f79901
authored
Jun 22, 2020
by
ibaker
Committed by
Andrew Lewis
Jun 23, 2020
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Migrate OkHttpDataSourceTest from Mockito to MockWebServer
PiperOrigin-RevId: 317637058
parent
8cccbcf4
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
98 deletions
extensions/okhttp/build.gradle
extensions/okhttp/src/test/java/com/google/android/exoplayer2/ext/okhttp/OkHttpDataSourceTest.java
extensions/okhttp/build.gradle
View file @
a6f79901
...
...
@@ -37,6 +37,7 @@ dependencies {
compileOnly
'org.checkerframework:checker-qual:'
+
checkerframeworkVersion
compileOnly
'org.jetbrains.kotlin:kotlin-annotations-jvm:'
+
kotlinAnnotationsVersion
testImplementation
project
(
modulePrefix
+
'testutils'
)
testImplementation
'com.squareup.okhttp3:mockwebserver:'
+
mockWebServerVersion
testImplementation
'org.robolectric:robolectric:'
+
robolectricVersion
// Do not update to 3.13.X or later until minSdkVersion is increased to 21:
// https://cashapp.github.io/2019-02-05/okhttp-3-13-requires-android-5
...
...
extensions/okhttp/src/test/java/com/google/android/exoplayer2/ext/okhttp/OkHttpDataSourceTest.java
View file @
a6f79901
...
...
@@ -17,28 +17,21 @@
package
com
.
google
.
android
.
exoplayer2
.
ext
.
okhttp
;
import
static
com
.
google
.
common
.
truth
.
Truth
.
assertThat
;
import
static
java
.
util
.
concurrent
.
TimeUnit
.
SECONDS
;
import
static
org
.
junit
.
Assert
.
assertThrows
;
import
static
org
.
mockito
.
Mockito
.
doReturn
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
androidx.test.ext.junit.runners.AndroidJUnit4
;
import
com.google.android.exoplayer2.C
;
import
com.google.android.exoplayer2.upstream.DataSpec
;
import
com.google.android.exoplayer2.upstream.HttpDataSource
;
import
java.io.IOException
;
import
com.google.common.base.Charsets
;
import
java.util.HashMap
;
import
java.util.Map
;
import
okhttp3.Call
;
import
okhttp3.MediaType
;
import
okhttp3.Protocol
;
import
okhttp3.Request
;
import
okhttp3.Response
;
import
okhttp3.ResponseBody
;
import
okhttp3.Headers
;
import
okhttp3.OkHttpClient
;
import
okhttp3.mockwebserver.MockResponse
;
import
okhttp3.mockwebserver.MockWebServer
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.ArgumentMatchers
;
import
org.mockito.Mockito
;
/** Unit tests for {@link OkHttpDataSource}. */
@RunWith
(
AndroidJUnit4
.
class
)
...
...
@@ -50,114 +43,76 @@ public class OkHttpDataSourceTest {
* below. Values wrapped in '*' are the ones that should be set in the connection request.
*
* <pre>{@code
* +---------------
--------+
---+-----+-----+-----+-----+-----+
* |
| Header Key
|
* +---------------
--------+
---+-----+-----+-----+-----+-----+
* |
Location | 0 | 1 | 2 | 3 | 4 | 5
|
* +---------------
--------+
---+-----+-----+-----+-----+-----+
* |
Default |*Y*| Y | Y |
| | |
* |
OkHttpDataSource | | *Y* | Y | Y
| *Y* | |
* | DataSpec
| |
| *Y* | *Y* | | *Y* |
* +---------------
--------+
---+-----+-----+-----+-----+-----+
* +---------------
+-----+--
---+-----+-----+-----+-----+-----+
* |
| Header Key
|
* +---------------
+-----+--
---+-----+-----+-----+-----+-----+
* |
Location | 0 | 1 | 2 | 3 | 4 | 5 | 6
|
* +---------------
+-----+--
---+-----+-----+-----+-----+-----+
* |
Constructor | *Y* | Y | Y | | Y
| | |
* |
Setter | | *Y* | Y | Y |
| *Y* | |
* | DataSpec
| | | *Y*
| *Y* | *Y* | | *Y* |
* +---------------
+-----+--
---+-----+-----+-----+-----+-----+
* }</pre>
*/
@Test
public
void
open_setsCorrectHeaders
()
throws
HttpDataSource
.
HttpDataSourceException
{
String
defaultValue
=
"Default"
;
String
okHttpDataSourceValue
=
"OkHttpDataSource"
;
String
dataSpecValue
=
"DataSpec"
;
// 1. Default properties on OkHttpDataSource
HttpDataSource
.
RequestProperties
defaultRequestProperties
=
new
HttpDataSource
.
RequestProperties
();
defaultRequestProperties
.
set
(
"0"
,
defaultValue
);
defaultRequestProperties
.
set
(
"1"
,
defaultValue
);
defaultRequestProperties
.
set
(
"2"
,
defaultValue
);
Call
.
Factory
mockCallFactory
=
mock
(
Call
.
Factory
.
class
);
OkHttpDataSource
okHttpDataSource
=
public
void
open_setsCorrectHeaders
()
throws
Exception
{
MockWebServer
mockWebServer
=
new
MockWebServer
();
mockWebServer
.
enqueue
(
new
MockResponse
());
String
propertyFromConstructor
=
"fromConstructor"
;
HttpDataSource
.
RequestProperties
constructorProperties
=
new
HttpDataSource
.
RequestProperties
();
constructorProperties
.
set
(
"0"
,
propertyFromConstructor
);
constructorProperties
.
set
(
"1"
,
propertyFromConstructor
);
constructorProperties
.
set
(
"2"
,
propertyFromConstructor
);
constructorProperties
.
set
(
"4"
,
propertyFromConstructor
);
OkHttpDataSource
dataSource
=
new
OkHttpDataSource
(
mockCallFactory
,
"testAgent"
,
/* cacheControl= */
null
,
defaultRequest
Properties
);
new
OkHttpClient
(),
"testAgent"
,
/* cacheControl= */
null
,
constructor
Properties
);
// 2. Additional properties set with setRequestProperty().
okHttpDataSource
.
setRequestProperty
(
"1"
,
okHttpDataSourceValue
);
okHttpDataSource
.
setRequestProperty
(
"2"
,
okHttpDataSourceValue
);
okHttpDataSource
.
setRequestProperty
(
"3"
,
okHttpDataSourceValue
);
okHttpDataSource
.
setRequestProperty
(
"4"
,
okHttpDataSourceValue
);
String
propertyFromSetter
=
"fromSetter"
;
dataSource
.
setRequestProperty
(
"1"
,
propertyFromSetter
);
dataSource
.
setRequestProperty
(
"2"
,
propertyFromSetter
);
dataSource
.
setRequestProperty
(
"3"
,
propertyFromSetter
);
dataSource
.
setRequestProperty
(
"5"
,
propertyFromSetter
);
// 3. DataSpec properties
String
propertyFromDataSpec
=
"fromDataSpec"
;
Map
<
String
,
String
>
dataSpecRequestProperties
=
new
HashMap
<>();
dataSpecRequestProperties
.
put
(
"2"
,
dataSpecValue
);
dataSpecRequestProperties
.
put
(
"3"
,
dataSpecValue
);
dataSpecRequestProperties
.
put
(
"5"
,
dataSpecValue
);
dataSpecRequestProperties
.
put
(
"2"
,
propertyFromDataSpec
);
dataSpecRequestProperties
.
put
(
"3"
,
propertyFromDataSpec
);
dataSpecRequestProperties
.
put
(
"4"
,
propertyFromDataSpec
);
dataSpecRequestProperties
.
put
(
"6"
,
propertyFromDataSpec
);
DataSpec
dataSpec
=
new
DataSpec
.
Builder
()
.
setUri
(
"http://www.google.com"
)
.
setPosition
(
1000
)
.
setLength
(
5000
)
.
setUri
(
mockWebServer
.
url
(
"/test-path"
).
toString
())
.
setHttpRequestHeaders
(
dataSpecRequestProperties
)
.
build
();
Mockito
.
doAnswer
(
invocation
->
{
Request
request
=
invocation
.
getArgument
(
0
);
assertThat
(
request
.
header
(
"0"
)).
isEqualTo
(
defaultValue
);
assertThat
(
request
.
header
(
"1"
)).
isEqualTo
(
okHttpDataSourceValue
);
assertThat
(
request
.
header
(
"2"
)).
isEqualTo
(
dataSpecValue
);
assertThat
(
request
.
header
(
"3"
)).
isEqualTo
(
dataSpecValue
);
assertThat
(
request
.
header
(
"4"
)).
isEqualTo
(
okHttpDataSourceValue
);
assertThat
(
request
.
header
(
"5"
)).
isEqualTo
(
dataSpecValue
);
dataSource
.
open
(
dataSpec
);
// return a Call whose .execute() will return a mock Response
Call
returnValue
=
mock
(
Call
.
class
);
doReturn
(
new
Response
.
Builder
()
.
request
(
request
)
.
protocol
(
Protocol
.
HTTP_1_1
)
.
code
(
200
)
.
message
(
"OK"
)
.
body
(
ResponseBody
.
create
(
MediaType
.
parse
(
"text/plain"
),
""
))
.
build
())
.
when
(
returnValue
)
.
execute
();
return
returnValue
;
})
.
when
(
mockCallFactory
)
.
newCall
(
ArgumentMatchers
.
any
());
okHttpDataSource
.
open
(
dataSpec
);
Headers
headers
=
mockWebServer
.
takeRequest
(
10
,
SECONDS
).
getHeaders
();
assertThat
(
headers
.
get
(
"0"
)).
isEqualTo
(
propertyFromConstructor
);
assertThat
(
headers
.
get
(
"1"
)).
isEqualTo
(
propertyFromSetter
);
assertThat
(
headers
.
get
(
"2"
)).
isEqualTo
(
propertyFromDataSpec
);
assertThat
(
headers
.
get
(
"3"
)).
isEqualTo
(
propertyFromDataSpec
);
assertThat
(
headers
.
get
(
"4"
)).
isEqualTo
(
propertyFromDataSpec
);
assertThat
(
headers
.
get
(
"5"
)).
isEqualTo
(
propertyFromSetter
);
assertThat
(
headers
.
get
(
"6"
)).
isEqualTo
(
propertyFromDataSpec
);
}
@Test
public
void
open_invalidResponseCode
()
throws
Exception
{
Call
.
Factory
callFactory
=
request
->
{
Call
mockCall
=
mock
(
Call
.
class
);
try
{
when
(
mockCall
.
execute
())
.
thenReturn
(
new
Response
.
Builder
()
.
request
(
request
)
.
protocol
(
Protocol
.
HTTP_1_1
)
.
code
(
404
)
.
message
(
"NOT FOUND"
)
.
body
(
ResponseBody
.
create
(
MediaType
.
parse
(
"text/plain"
),
"failure msg"
))
.
build
());
}
catch
(
IOException
e
)
{
throw
new
AssertionError
(
e
);
}
return
mockCall
;
};
MockWebServer
mockWebServer
=
new
MockWebServer
();
mockWebServer
.
enqueue
(
new
MockResponse
().
setResponseCode
(
404
).
setBody
(
"failure msg"
));
OkHttpDataSource
okHttpDataSource
=
new
OkHttpDataSource
(
callFactory
,
new
OkHttpClient
()
,
"testAgent"
,
/* cacheControl= */
null
,
/* defaultRequestProperties= */
null
);
DataSpec
dataSpec
=
new
DataSpec
.
Builder
().
setUri
(
"http://www.google.com"
).
build
();
DataSpec
dataSpec
=
new
DataSpec
.
Builder
().
setUri
(
mockWebServer
.
url
(
"/test-path"
).
toString
()
).
build
();
HttpDataSource
.
InvalidResponseCodeException
exception
=
assertThrows
(
...
...
@@ -165,6 +120,6 @@ public class OkHttpDataSourceTest {
()
->
okHttpDataSource
.
open
(
dataSpec
));
assertThat
(
exception
.
responseCode
).
isEqualTo
(
404
);
assertThat
(
exception
.
responseBody
).
isEqualTo
(
"failure msg"
.
getBytes
(
C
.
UTF8_NAME
));
assertThat
(
exception
.
responseBody
).
isEqualTo
(
"failure msg"
.
getBytes
(
C
harsets
.
UTF_8
));
}
}
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