Skip to content

Commit

Permalink
migrate ReactCookieJarContainerTest to Kotlin (#37809)
Browse files Browse the repository at this point in the history
Summary:
This PR migrates ReactCookieJarContainerTest.java to Kotlin as a part of the issue #37708

## Changelog:
[INTERNAL] [CHANGED] - Migrate ReactCookieJarContainerTest.java to Kotlin

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

[ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests

Pull Request resolved: #37809

Test Plan:
1. Run ./gradlew :packages:react-native:ReactAndroid:test.
2. All tests should pass.

Reviewed By: javache

Differential Revision: D46779169

Pulled By: rshest

fbshipit-source-id: b5caa9397ac1b852fc73d9c58afc4a21caeb8727
  • Loading branch information
pk-218 authored and facebook-github-bot committed Jun 16, 2023
1 parent d097408 commit f1901d4
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 71 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.modules.network

import okhttp3.Cookie
import okhttp3.CookieJar
import okhttp3.HttpUrl
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.any
import org.mockito.Mockito.mock
import org.mockito.Mockito.`when` as whenever
import org.powermock.core.classloader.annotations.PowerMockIgnore
import org.powermock.core.classloader.annotations.PrepareForTest
import org.robolectric.RobolectricTestRunner

/** Tests for {@link NetworkingModule}. */
@RunWith(RobolectricTestRunner::class)
@PrepareForTest(ReactCookieJarContainer::class)
@PowerMockIgnore("org.mockito.*", "org.robolectric.*", "androidx.*", "android.*")
class ReactCookieJarContainerTest {
private val httpUrl: HttpUrl = HttpUrl.parse("http://example.com")

@Test
fun testMissingJar() {
val jarContainer: ReactCookieJarContainer = mock(ReactCookieJarContainer::class.java)
assertThat(jarContainer.loadForRequest(httpUrl).size).isEqualTo(0)
}

@Test
fun testEmptyCookies() {
val jarContainer: ReactCookieJarContainer = mock(ReactCookieJarContainer::class.java)
val cookies: List<Cookie> = emptyList()
whenever(jarContainer.loadForRequest(any(HttpUrl::class.java))).thenReturn(cookies)
assertThat(jarContainer.loadForRequest(httpUrl).size).isEqualTo(0)
}

@Test
fun testValidCookies() {
val jarContainer = ReactCookieJarContainer()
val cookieJar: CookieJar = mock(CookieJar::class.java)
jarContainer.setCookieJar(cookieJar)
val cookies: MutableList<Cookie> = mutableListOf()
cookies.add(Cookie.Builder().name("valid").value("valid value").domain("domain").build())
whenever(cookieJar.loadForRequest(httpUrl)).thenReturn(cookies)
assertThat(jarContainer.loadForRequest(httpUrl).size).isEqualTo(1)
}

@Test
fun testInvalidCookies() {
val jarContainer = ReactCookieJarContainer()
val cookieJar: CookieJar = mock(CookieJar::class.java)
jarContainer.setCookieJar(cookieJar)
val cookies: MutableList<Cookie> = mutableListOf()
cookies.add(Cookie.Builder().name("valid").value("înválíd välūė").domain("domain").build())
whenever(cookieJar.loadForRequest(httpUrl)).thenReturn(cookies)
assertThat(jarContainer.loadForRequest(httpUrl).size).isEqualTo(0)
}
}

0 comments on commit f1901d4

Please sign in to comment.