-
Notifications
You must be signed in to change notification settings - Fork 282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(Part 1) stream-chat-android-client
unit tests - api
package
#5660
base: develop
Are you sure you want to change the base?
Changes from 3 commits
20f3051
f3a7d50
2116f90
5215984
a70a09a
aa726c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved. | ||
* | ||
* Licensed under the Stream License; | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://github.com/GetStream/stream-chat-android/blob/main/LICENSE | ||
* | ||
* 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 io.getstream.chat.android.client.api | ||
|
||
import io.getstream.chat.android.test.TestCoroutineRule | ||
import io.getstream.result.Error | ||
import io.getstream.result.Result | ||
import kotlinx.coroutines.test.TestScope | ||
import kotlinx.coroutines.test.runTest | ||
import org.amshove.kluent.`should be instance of` | ||
import org.amshove.kluent.shouldBeEqualTo | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
internal class ErrorCallTest { | ||
|
||
@get:Rule | ||
val coroutinesTestRule = TestCoroutineRule() | ||
|
||
@Test | ||
fun testErrorCallExecute() { | ||
// given | ||
val error = Error.GenericError("Generic error") | ||
val errorCall = ErrorCall<Unit>(TestScope(), error) | ||
// when | ||
val result = errorCall.execute() | ||
// then | ||
result `should be instance of` Result.Failure::class | ||
result as Result.Failure | ||
result.value shouldBeEqualTo error | ||
} | ||
|
||
@Test | ||
fun testErrorCallEnqueue() = runTest { | ||
// given | ||
val error = Error.GenericError("Generic error") | ||
val errorCall = ErrorCall<Unit>(backgroundScope, error) | ||
// when | ||
errorCall.enqueue { result -> | ||
// then | ||
result `should be instance of` Result.Failure::class | ||
result as Result.Failure | ||
result.value shouldBeEqualTo error | ||
} | ||
} | ||
|
||
@Test | ||
fun testErrorCallAwait() = runTest { | ||
// given | ||
val error = Error.GenericError("Generic error") | ||
val errorCall = ErrorCall<Unit>(TestScope(), error) | ||
// when | ||
val result = errorCall.await() | ||
// then | ||
result `should be instance of` Result.Failure::class | ||
result as Result.Failure | ||
result.value shouldBeEqualTo error | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved. | ||
* | ||
* Licensed under the Stream License; | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://github.com/GetStream/stream-chat-android/blob/main/LICENSE | ||
* | ||
* 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 io.getstream.chat.android.client.api.interceptor | ||
|
||
import io.getstream.chat.android.client.api.FakeChain | ||
import io.getstream.chat.android.client.api.FakeResponse | ||
import org.amshove.kluent.shouldBeEqualTo | ||
import org.junit.Test | ||
|
||
internal class ApiKeyInterceptorTest { | ||
|
||
@Test | ||
fun testApiKeyIsAddedAsHeader() { | ||
// given | ||
val apiKey = "apiKeyValue" | ||
val interceptor = ApiKeyInterceptor(apiKey) | ||
// when | ||
val response = interceptor.intercept(FakeChain(FakeResponse(200))) | ||
// then | ||
val apiKeyQueryParam = response.request.url.queryParameter("api_key") | ||
apiKeyQueryParam shouldBeEqualTo apiKey | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved. | ||
* | ||
* Licensed under the Stream License; | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://github.com/GetStream/stream-chat-android/blob/main/LICENSE | ||
* | ||
* 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 io.getstream.chat.android.client.api.interceptor | ||
|
||
import io.getstream.chat.android.client.api.FakeChain | ||
import io.getstream.chat.android.client.api.FakeResponse | ||
import io.getstream.chat.android.client.plugins.requests.ApiRequestsAnalyser | ||
import org.junit.Test | ||
import org.mockito.kotlin.any | ||
import org.mockito.kotlin.doNothing | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.times | ||
import org.mockito.kotlin.verify | ||
import org.mockito.kotlin.whenever | ||
|
||
internal class ApiRequestAnalyserInterceptorTest { | ||
|
||
@Test | ||
fun testApiRequestAnalyserInterceptorRegistersTheRequestInTheAnalyser() { | ||
// given | ||
val analyser = mock<ApiRequestsAnalyser>() | ||
doNothing().whenever(analyser).registerRequest(any(), any()) | ||
val interceptor = ApiRequestAnalyserInterceptor(analyser) | ||
// when | ||
val chain = FakeChain(FakeResponse(200)) | ||
interceptor.intercept(chain) | ||
// then | ||
verify(analyser, times(1)).registerRequest( | ||
requestName = chain.request().url.toString(), | ||
data = mapOf("body" to "no_body"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we only testing when the response is empty? |
||
) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved. | ||
* | ||
* Licensed under the Stream License; | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://github.com/GetStream/stream-chat-android/blob/main/LICENSE | ||
* | ||
* 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 io.getstream.chat.android.client.api.interceptor | ||
|
||
import io.getstream.chat.android.client.api.FakeChain | ||
import io.getstream.chat.android.client.api.FakeResponse | ||
import io.getstream.chat.android.client.utils.HeadersUtil | ||
import org.amshove.kluent.`should be equal to` | ||
import org.junit.Test | ||
import org.mockito.kotlin.doReturn | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.whenever | ||
|
||
internal class HeadersInterceptorTest { | ||
|
||
@Test | ||
fun testAnonymousUserHeaders() { | ||
// given | ||
val isAnonymous = { true } | ||
val headersUtil = mock<HeadersUtil>() | ||
whenever(headersUtil.buildSdkTrackingHeaders()).doReturn("sdkTrackingHeaders") | ||
whenever(headersUtil.buildUserAgent()).doReturn("userAgent") | ||
val interceptor = HeadersInterceptor(isAnonymous, headersUtil) | ||
// when | ||
val response = interceptor.intercept(FakeChain(FakeResponse(200))) | ||
// then | ||
response.request.header("User-Agent") `should be equal to` "userAgent" | ||
response.request.header("Content-Type") `should be equal to` "application/json" | ||
response.request.header("stream-auth-type") `should be equal to` "anonymous" | ||
response.request.header("X-Stream-Client") `should be equal to` "sdkTrackingHeaders" | ||
response.request.header("Cache-Control") `should be equal to` "no-cache" | ||
} | ||
|
||
@Test | ||
fun testAuthenticatedUserHeaders() { | ||
// given | ||
val isAnonymous = { false } | ||
val headersUtil = mock<HeadersUtil>() | ||
whenever(headersUtil.buildSdkTrackingHeaders()).doReturn("sdkTrackingHeaders") | ||
whenever(headersUtil.buildUserAgent()).doReturn("userAgent") | ||
val interceptor = HeadersInterceptor(isAnonymous, headersUtil) | ||
// when | ||
val response = interceptor.intercept(FakeChain(FakeResponse(200))) | ||
// then | ||
response.request.header("User-Agent") `should be equal to` "userAgent" | ||
response.request.header("Content-Type") `should be equal to` "application/json" | ||
response.request.header("stream-auth-type") `should be equal to` "jwt" | ||
response.request.header("X-Stream-Client") `should be equal to` "sdkTrackingHeaders" | ||
response.request.header("Cache-Control") `should be equal to` "no-cache" | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of writting the same test multiple times only because a single parameter is different, we can use On the other hand, consider to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't want to write a |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved. | ||
* | ||
* Licensed under the Stream License; | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://github.com/GetStream/stream-chat-android/blob/main/LICENSE | ||
* | ||
* 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 io.getstream.chat.android.client.api.interceptor | ||
|
||
import io.getstream.chat.android.client.api.FakeChain | ||
import io.getstream.chat.android.client.api.FakeResponse | ||
import io.getstream.chat.android.client.api.models.ProgressRequestBody | ||
import io.getstream.chat.android.client.utils.ProgressCallback | ||
import io.getstream.result.Error | ||
import okhttp3.Request | ||
import okhttp3.RequestBody.Companion.toRequestBody | ||
import org.amshove.kluent.`should be instance of` | ||
import org.junit.Test | ||
|
||
internal class ProgressInterceptorTest { | ||
|
||
@Test | ||
fun testProgressInterceptor() { | ||
// given | ||
val interceptor = ProgressInterceptor() | ||
val progressCallback = object : ProgressCallback { | ||
override fun onSuccess(url: String?) { /* No-Op */ } | ||
override fun onError(error: Error) { /* No-Op */ } | ||
override fun onProgress(bytesUploaded: Long, totalBytes: Long) { /* No-Op */ } | ||
} | ||
val request = Request.Builder() | ||
.url("https://hello.url") | ||
.post("body".toRequestBody()) | ||
.tag(ProgressCallback::class.java, progressCallback) | ||
.build() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about creating a |
||
val chain = FakeChain(FakeResponse(200), request = request) | ||
// when | ||
val response = interceptor.intercept(chain) | ||
// then | ||
response.request.body `should be instance of` ProgressRequestBody::class | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.