-
Notifications
You must be signed in to change notification settings - Fork 0
Introduce test for the StreamEndpointErrorInterceptor and some parsing tests
#21
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
394b31c
Add tests for missing fields check and error interceptor
aleksandar-apostolov 6401dfa
Spotless
aleksandar-apostolov 1ea0736
change test name
aleksandar-apostolov 082a5cf
Update tests
aleksandar-apostolov 18b978d
Spotless
aleksandar-apostolov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
136 changes: 136 additions & 0 deletions
136
...io/getstream/android/core/internal/http/interceptor/StreamEndpointErrorInterceptorTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| /* | ||
| * 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-core-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.android.core.internal.http.interceptor | ||
|
|
||
| import io.getstream.android.core.api.model.exceptions.StreamEndpointErrorData | ||
| import io.getstream.android.core.api.model.exceptions.StreamEndpointException | ||
| import io.getstream.android.core.api.serialization.StreamJsonSerialization | ||
| import java.io.IOException | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertFailsWith | ||
| import okhttp3.Connection | ||
| import okhttp3.Interceptor | ||
| import okhttp3.MediaType.Companion.toMediaType | ||
| import okhttp3.Protocol | ||
| import okhttp3.Request | ||
| import okhttp3.Response | ||
| import okhttp3.ResponseBody.Companion.toResponseBody | ||
| import org.junit.Test | ||
|
|
||
| internal class StreamEndpointErrorInterceptorTest { | ||
|
|
||
| private val request = Request.Builder().url("https://example.test/path").build() | ||
|
|
||
| private fun chainReturning(response: Response) = | ||
| object : Interceptor.Chain { | ||
| override fun call() = throw UnsupportedOperationException() | ||
|
|
||
| override fun connectTimeoutMillis() = 0 | ||
|
|
||
| override fun proceed(request: Request): Response = response | ||
|
|
||
| override fun connection(): Connection? = null | ||
|
|
||
| override fun readTimeoutMillis() = 0 | ||
|
|
||
| override fun request(): Request = request | ||
|
|
||
| override fun withConnectTimeout(timeout: Int, unit: java.util.concurrent.TimeUnit) = | ||
| this | ||
|
|
||
| override fun withReadTimeout(timeout: Int, unit: java.util.concurrent.TimeUnit) = this | ||
|
|
||
| override fun withWriteTimeout(timeout: Int, unit: java.util.concurrent.TimeUnit) = this | ||
|
|
||
| override fun writeTimeoutMillis() = 0 | ||
| } | ||
|
|
||
| @Test | ||
| fun `successful response bypasses error handling`() { | ||
| val response = | ||
| Response.Builder() | ||
| .request(request) | ||
| .protocol(Protocol.HTTP_1_1) | ||
| .code(200) | ||
| .message("OK") | ||
| .body("body".toResponseBody("text/plain".toMediaType())) | ||
| .build() | ||
| val interceptor = StreamEndpointErrorInterceptor(MockJsonSerialization()) | ||
|
|
||
| val result = interceptor.intercept(chainReturning(response)) | ||
|
|
||
| assertEquals(response, result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `unsuccessful response with parseable error returns api error`() { | ||
| val error = StreamEndpointErrorData(code = 40, message = "Invalid token") | ||
| val response = | ||
| Response.Builder() | ||
| .request(request) | ||
| .protocol(Protocol.HTTP_1_1) | ||
| .code(401) | ||
| .message("Unauthorized") | ||
| .body("{\"code\":40}".toResponseBody("application/json".toMediaType())) | ||
| .build() | ||
| val parser = MockJsonSerialization(result = Result.success(error)) | ||
| val interceptor = StreamEndpointErrorInterceptor(parser) | ||
|
|
||
| val exception = | ||
| assertFailsWith<StreamEndpointException> { | ||
| interceptor.intercept(chainReturning(response)) | ||
| } | ||
|
|
||
| assertEquals("Failed request: https://example.test/path", exception.message) | ||
| assertEquals(error, exception.apiError) | ||
| } | ||
|
|
||
| @Test | ||
| fun `unsuccessful response with parse failure propagates cause`() { | ||
| val failure = IOException("boom") | ||
| val response = | ||
| Response.Builder() | ||
| .request(request) | ||
| .protocol(Protocol.HTTP_1_1) | ||
| .code(500) | ||
| .message("Server Error") | ||
| .body("oops".toResponseBody("text/plain".toMediaType())) | ||
| .build() | ||
| val parser = MockJsonSerialization(result = Result.failure(failure)) | ||
| val interceptor = StreamEndpointErrorInterceptor(parser) | ||
|
|
||
| val exception = | ||
| assertFailsWith<StreamEndpointException> { | ||
| interceptor.intercept(chainReturning(response)) | ||
| } | ||
|
|
||
| assertEquals("Failed request: https://example.test/path", exception.message) | ||
| assertEquals(failure, exception.cause) | ||
| } | ||
|
|
||
| private class MockJsonSerialization( | ||
| private val result: Result<StreamEndpointErrorData> = | ||
| Result.failure(UnsupportedOperationException()) | ||
| ) : StreamJsonSerialization { | ||
| override fun toJson(any: Any): Result<String> = | ||
| Result.failure(UnsupportedOperationException()) | ||
|
|
||
| override fun <T : Any> fromJson(raw: String, clazz: Class<T>): Result<T> { | ||
| @Suppress("UNCHECKED_CAST") | ||
| return result as Result<T> | ||
| } | ||
| } | ||
| } | ||
187 changes: 187 additions & 0 deletions
187
...est/java/io/getstream/android/core/internal/model/StreamInternalModelSerializationTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| /* | ||
| * 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-core-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.android.core.internal.model | ||
|
|
||
| import com.squareup.moshi.JsonDataException | ||
| import io.getstream.android.core.api.model.connection.StreamConnectedUser | ||
| import io.getstream.android.core.internal.model.authentication.StreamWSAuthMessageRequest | ||
| import io.getstream.android.core.internal.model.events.EVENT_TYPE_CONNECTION_OK | ||
| import io.getstream.android.core.internal.model.events.StreamClientConnectedEvent | ||
| import io.getstream.android.core.internal.model.events.StreamClientConnectionErrorEvent | ||
| import io.getstream.android.core.internal.model.events.StreamHealthCheckEvent | ||
| import io.getstream.android.core.internal.serialization.moshi.StreamCoreMoshiProvider | ||
| import java.util.Date | ||
| import kotlin.test.assertEquals | ||
| import org.junit.Test | ||
|
|
||
| internal class StreamInternalModelSerializationTest { | ||
|
|
||
| private val moshi = StreamCoreMoshiProvider().builder {}.build() | ||
|
|
||
| @Test(expected = JsonDataException::class) | ||
| fun `StreamWSAuthMessageRequest missing required fields throws`() { | ||
| // Given | ||
| val adapter = moshi.adapter(StreamWSAuthMessageRequest::class.java) | ||
| val invalidJson = "{\"products\":null,\"token\":null}" | ||
|
|
||
| // When | ||
| adapter.fromJson(invalidJson) | ||
|
|
||
| // Then expect exception | ||
| } | ||
|
|
||
| @Test(expected = JsonDataException::class) | ||
| fun `StreamClientConnectedEvent missing connectionId throws`() { | ||
| // Given | ||
| val adapter = moshi.adapter(StreamClientConnectedEvent::class.java) | ||
| val user = | ||
| StreamConnectedUser( | ||
| createdAt = Date(0), | ||
| id = "u", | ||
| language = "en", | ||
| role = "user", | ||
| updatedAt = Date(0), | ||
| teams = emptyList(), | ||
| ) | ||
| val jsonWithNullId = | ||
| "{" + | ||
| "\"me\":${moshi.adapter(StreamConnectedUser::class.java).toJson(user)}," + | ||
| "\"type\":\"${EVENT_TYPE_CONNECTION_OK}\"" + | ||
| "}" | ||
|
|
||
| // When | ||
| adapter.fromJson(jsonWithNullId) | ||
|
|
||
| // Then expect exception | ||
| } | ||
|
|
||
| @Test | ||
| fun `StreamWSAuthMessageRequest parses canonical json`() { | ||
| val adapter = moshi.adapter(StreamWSAuthMessageRequest::class.java) | ||
| val json = | ||
| """ | ||
| { | ||
| "products": ["chat", "video"], | ||
| "token": "jwt-123", | ||
| "user_details": { | ||
| "id": "user-7", | ||
| "image": "https://example.test/u7.png", | ||
| "invisible": false, | ||
| "language": "en", | ||
| "name": "Seven", | ||
| "custom": {"role": "admin"} | ||
| } | ||
| } | ||
| """ | ||
| .trimIndent() | ||
|
|
||
| val request = adapter.fromJson(json)!! | ||
|
|
||
| assertEquals(listOf("chat", "video"), request.products) | ||
| assertEquals("jwt-123", request.token) | ||
| assertEquals("user-7", request.userDetails.id) | ||
| assertEquals("https://example.test/u7.png", request.userDetails.image) | ||
| assertEquals(false, request.userDetails.invisible) | ||
| assertEquals("admin", request.userDetails.custom?.get("role")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `StreamClientConnectedEvent parses canonical json`() { | ||
| val adapter = moshi.adapter(StreamClientConnectedEvent::class.java) | ||
| val json = | ||
| """ | ||
| { | ||
| "connection_id": "conn-xyz", | ||
| "me": { | ||
| "created_at": 12312412312, | ||
| "id": "user-42", | ||
| "language": "en", | ||
| "role": "admin", | ||
| "updated_at": 12312412312, | ||
| "blocked_user_ids": ["u-1"], | ||
| "teams": ["team-a"], | ||
| "custom": {"source": "json"}, | ||
| "deactivated_at": null, | ||
| "deleted_at": null, | ||
| "image": "https://example.test/avatar.png", | ||
| "last_active": null, | ||
| "name": "User 42" | ||
| }, | ||
| "type": "connection.ok" | ||
| } | ||
| """ | ||
| .trimIndent() | ||
|
|
||
| val event = adapter.fromJson(json)!! | ||
|
|
||
| assertEquals("conn-xyz", event.connectionId) | ||
| assertEquals("connection.ok", event.type) | ||
| assertEquals("user-42", event.me.id) | ||
| assertEquals(listOf("team-a"), event.me.teams) | ||
| assertEquals(mapOf("source" to "json"), event.me.custom) | ||
| } | ||
|
|
||
| @Test | ||
| fun `StreamClientConnectionErrorEvent parses canonical json`() { | ||
| val adapter = moshi.adapter(StreamClientConnectionErrorEvent::class.java) | ||
| val json = | ||
| """ | ||
| { | ||
| "connection_id": "conn-bad", | ||
| "created_at": 12312412312, | ||
| "error": { | ||
| "code": 32, | ||
| "duration": "5ms", | ||
| "message": "Failure", | ||
| "more_info": "https://example.test/help", | ||
| "StatusCode": 503, | ||
| "details": [10, 20], | ||
| "unrecoverable": false, | ||
| "exception_fields": {"reason": "maintenance"} | ||
| }, | ||
| "type": "connection.error" | ||
| } | ||
| """ | ||
| .trimIndent() | ||
|
|
||
| val event = adapter.fromJson(json)!! | ||
|
|
||
| assertEquals("conn-bad", event.connectionId) | ||
| assertEquals("connection.error", event.type) | ||
| assertEquals(32, event.error.code) | ||
| assertEquals("maintenance", event.error.exceptionFields?.get("reason")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `StreamHealthCheckEvent parses canonical json`() { | ||
| val adapter = moshi.adapter(StreamHealthCheckEvent::class.java) | ||
| val json = | ||
| """ | ||
| { | ||
| "connection_id": "conn-health", | ||
| "created_at": 12312412312, | ||
| "custom": {"ping": "pong"}, | ||
| "type": "health.check" | ||
| } | ||
| """ | ||
| .trimIndent() | ||
|
|
||
| val event = adapter.fromJson(json)!! | ||
|
|
||
| assertEquals("conn-health", event.connectionId) | ||
| assertEquals(mapOf("ping" to "pong"), event.custom) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.