Skip to content

Commit

Permalink
chore: update HttpSettingsHelperTest (#442)
Browse files Browse the repository at this point in the history
  • Loading branch information
tangcent authored Apr 20, 2021
1 parent 43495e5 commit 7ae5f58
Showing 1 changed file with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.itangcent.idea.plugin.settings.helper

import com.google.inject.Inject
import com.itangcent.idea.plugin.settings.SettingBinder
import com.itangcent.idea.plugin.settings.Settings
import com.intellij.openapi.ui.Messages
import com.itangcent.idea.swing.MessagesHelper
import com.itangcent.intellij.context.ActionContext
import com.itangcent.mock.AdvancedContextTest
import com.itangcent.mock.SettingBinderAdaptor
import org.junit.Assert.assertArrayEquals
import org.junit.jupiter.api.Test
import org.mockito.Mockito
import java.util.concurrent.TimeUnit
import kotlin.test.assertEquals
import kotlin.test.assertFalse
Expand All @@ -20,15 +19,37 @@ internal class HttpSettingsHelperTest : SettingsHelperTest() {

@Inject
private lateinit var httpSettingsHelper: HttpSettingsHelper
override fun bind(builder: ActionContext.ActionContextBuilder) {
super.bind(builder)

val messagesHelper = Mockito.mock(MessagesHelper::class.java)
Mockito.`when`(messagesHelper.showYesNoDialog(
Mockito.eq("Do you trust [http://itangcent.com]?"),
Mockito.anyString(),
Mockito.any()))
.thenReturn(Messages.YES)
Mockito.`when`(messagesHelper.showYesNoDialog(
Mockito.eq("Do you trust [http://tangcent.com]?"),
Mockito.anyString(),
Mockito.any()))
.thenReturn(Messages.NO)
builder.bindInstance(MessagesHelper::class, messagesHelper)
}

@Test
fun testCheckTrustUrl() {
settings.yapiServer = "http://127.0.0.1:3000"
settings.trustHosts = arrayOf(
"https://raw.githubusercontent.com",
"!https://raw.githubusercontent.com/itangcent",
"!http://192.168.1.1",
"!http://localhost",
"https://api.getpostman.com")

//trust yapi server
assertTrue(httpSettingsHelper.checkTrustUrl("http://127.0.0.1:3000"))
assertTrue(httpSettingsHelper.checkTrustUrl("http://127.0.0.1:3000/api"))

assertTrue(httpSettingsHelper.checkTrustUrl("https://raw.githubusercontent.com"))
assertTrue(httpSettingsHelper.checkTrustUrl("https://raw.githubusercontent.com/tangcent"))
assertTrue(httpSettingsHelper.checkTrustUrl("https://raw.githubusercontent.com/tangcent/easy-yapi/master/third/swagger.config"))
Expand All @@ -40,22 +61,36 @@ internal class HttpSettingsHelperTest : SettingsHelperTest() {
assertFalse(httpSettingsHelper.checkTrustUrl("http://localhost/a"))
assertTrue(httpSettingsHelper.checkTrustUrl("https://api.getpostman.com"))
assertTrue(httpSettingsHelper.checkTrustUrl("https://api.getpostman.com/collections"))

//check without dumb
assertFalse(httpSettingsHelper.checkTrustUrl("http://tangcent.com/index", false))
assertTrue(httpSettingsHelper.checkTrustUrl("http://itangcent.com/index", false))
}

@Test
fun testCheckTrustHost() {
settings.yapiServer = "http://127.0.0.1:3000"
settings.trustHosts = arrayOf(
"https://raw.githubusercontent.com",
"!https://raw.githubusercontent.com/itangcent",
"!http://192.168.1.1",
"!http://localhost",
"https://api.getpostman.com")

//trust yapi server
assertTrue(httpSettingsHelper.checkTrustHost("http://127.0.0.1:3000"))

assertTrue(httpSettingsHelper.checkTrustHost("https://raw.githubusercontent.com"))
assertFalse(httpSettingsHelper.checkTrustHost("https://raw.githubusercontent.com/tangcent"))
assertFalse(httpSettingsHelper.checkTrustHost("https://raw.githubusercontent.com/itangcent"))
assertFalse(httpSettingsHelper.checkTrustHost("http://192.168.1.1"))
assertFalse(httpSettingsHelper.checkTrustHost("http://localhost"))
assertTrue(httpSettingsHelper.checkTrustHost("https://api.getpostman.com"))

//check without dumb
assertFalse(httpSettingsHelper.checkTrustHost("http://tangcent.com", false))
assertTrue(httpSettingsHelper.checkTrustHost("http://itangcent.com", false))

}

@Test
Expand All @@ -76,6 +111,8 @@ internal class HttpSettingsHelperTest : SettingsHelperTest() {
httpSettingsHelper.resolveHost("https://raw.githubusercontent.com"))
assertEquals("https://raw.githubusercontent.com/tangcent",
httpSettingsHelper.resolveHost("https://raw.githubusercontent.com/tangcent"))
assertEquals("https://raw.githubusercontent.com/tangcent",
httpSettingsHelper.resolveHost("https://raw.githubusercontent.com/tangcent/"))
assertEquals("https://raw.githubusercontent.com/tangcent",
httpSettingsHelper.resolveHost("https://raw.githubusercontent.com/tangcent/easy-yapi/master/third/swagger.config"))
assertEquals("https://api.getpostman.com",
Expand All @@ -84,6 +121,8 @@ internal class HttpSettingsHelperTest : SettingsHelperTest() {
httpSettingsHelper.resolveHost("http://127.0.0.1/a/b/c"))
assertEquals("https://127.0.0.1",
httpSettingsHelper.resolveHost("https://127.0.0.1/a/b/c"))
assertEquals("unknown host",
httpSettingsHelper.resolveHost("unknown host"))
}

@Test
Expand Down

0 comments on commit 7ae5f58

Please sign in to comment.