11package com.coder.toolbox.util
22
3+ import com.coder.toolbox.CoderToolboxContext
34import com.coder.toolbox.sdk.DataGen
4- import com.sun.net.httpserver.HttpHandler
5- import com.sun.net.httpserver.HttpServer
6- import java.net.HttpURLConnection
7- import java.net.InetSocketAddress
5+ import com.coder.toolbox.settings.Environment
6+ import com.coder.toolbox.store.CoderSecretsStore
7+ import com.coder.toolbox.store.CoderSettingsStore
8+ import com.jetbrains.toolbox.api.core.diagnostics.Logger
9+ import com.jetbrains.toolbox.api.core.os.LocalDesktopManager
10+ import com.jetbrains.toolbox.api.localization.LocalizableStringFactory
11+ import com.jetbrains.toolbox.api.remoteDev.connection.ClientHelper
12+ import com.jetbrains.toolbox.api.remoteDev.connection.RemoteToolsHelper
13+ import com.jetbrains.toolbox.api.remoteDev.connection.ToolboxProxySettings
14+ import com.jetbrains.toolbox.api.remoteDev.states.EnvironmentStateColorPalette
15+ import com.jetbrains.toolbox.api.remoteDev.ui.EnvironmentUiPageManager
16+ import com.jetbrains.toolbox.api.ui.ToolboxUi
17+ import io.mockk.mockk
18+ import kotlinx.coroutines.CoroutineScope
19+ import kotlinx.coroutines.flow.MutableStateFlow
20+ import kotlinx.coroutines.runBlocking
821import java.util.UUID
922import kotlin.test.Test
10- import kotlin.test.assertContains
1123import kotlin.test.assertEquals
12- import kotlin.test.assertFailsWith
24+ import kotlin.test.assertNull
1325
1426internal class LinkHandlerTest {
15- /* *
16- * Create, start, and return a server that uses the provided handler.
17- */
18- private fun mockServer (handler : HttpHandler ): Pair <HttpServer , String > {
19- val srv = HttpServer .create(InetSocketAddress (0 ), 0 )
20- srv.createContext(" /" , handler)
21- srv.start()
22- return Pair (srv, " http://localhost:" + srv.address.port)
23- }
24-
25- /* *
26- * Create, start, and return a server that mocks redirects.
27- */
28- private fun mockRedirectServer (
29- location : String ,
30- temp : Boolean ,
31- ): Pair <HttpServer , String > = mockServer { exchange ->
32- exchange.responseHeaders.set(" Location" , location)
33- exchange.sendResponseHeaders(
34- if (temp) HttpURLConnection .HTTP_MOVED_TEMP else HttpURLConnection .HTTP_MOVED_PERM ,
35- - 1 ,
36- )
37- exchange.close()
38- }
27+ private val context = CoderToolboxContext (
28+ mockk<ToolboxUi >(relaxed = true ),
29+ mockk<EnvironmentUiPageManager >(),
30+ mockk<EnvironmentStateColorPalette >(),
31+ mockk<RemoteToolsHelper >(),
32+ mockk<ClientHelper >(),
33+ mockk<LocalDesktopManager >(),
34+ mockk<CoroutineScope >(),
35+ mockk<Logger >(relaxed = true ),
36+ mockk<LocalizableStringFactory >(relaxed = true ),
37+ CoderSettingsStore (pluginTestSettingsStore(), Environment (), mockk<Logger >(relaxed = true )),
38+ mockk<CoderSecretsStore >(),
39+ mockk<ToolboxProxySettings >()
40+ )
41+
42+ private val protocolHandler = CoderProtocolHandler (
43+ context,
44+ DialogUi (context),
45+ MutableStateFlow (false )
46+ )
3947
4048 private val agents =
4149 mapOf (
@@ -49,7 +57,7 @@ internal class LinkHandlerTest {
4957 )
5058
5159 @Test
52- fun getMatchingAgent () {
60+ fun tstgetMatchingAgent () {
5361 val ws = DataGen .workspace(" ws" , agents = agents)
5462
5563 val tests =
@@ -74,9 +82,10 @@ internal class LinkHandlerTest {
7482 " b0e4c54d-9ba9-4413-8512-11ca1e826a24" ,
7583 ),
7684 )
77-
78- tests.forEach {
79- assertEquals(UUID .fromString(it.second), getMatchingAgent(it.first, ws).id)
85+ runBlocking {
86+ tests.forEach {
87+ assertEquals(UUID .fromString(it.second), protocolHandler.getMatchingAgent(it.first, ws)?.id)
88+ }
8089 }
8190 }
8291
@@ -104,14 +113,10 @@ internal class LinkHandlerTest {
104113 " agent with ID" ,
105114 ),
106115 )
107-
108- tests.forEach {
109- val ex =
110- assertFailsWith(
111- exceptionClass = it.second,
112- block = { getMatchingAgent(it.first, ws).id },
113- )
114- assertContains(ex.message.toString(), it.third)
116+ runBlocking {
117+ tests.forEach {
118+ assertNull(protocolHandler.getMatchingAgent(it.first, ws)?.id)
119+ }
115120 }
116121 }
117122
@@ -126,15 +131,16 @@ internal class LinkHandlerTest {
126131 mapOf (" agent" to null ),
127132 mapOf (" agent_id" to null ),
128133 )
129-
130- tests.forEach {
131- assertEquals(
132- UUID .fromString(" b0e4c54d-9ba9-4413-8512-11ca1e826a24" ),
133- getMatchingAgent(
134- it,
135- ws,
136- ).id,
137- )
134+ runBlocking {
135+ tests.forEach {
136+ assertEquals(
137+ UUID .fromString(" b0e4c54d-9ba9-4413-8512-11ca1e826a24" ),
138+ protocolHandler.getMatchingAgent(
139+ it,
140+ ws,
141+ )?.id,
142+ )
143+ }
138144 }
139145 }
140146
@@ -149,14 +155,10 @@ internal class LinkHandlerTest {
149155 " agent with ID"
150156 ),
151157 )
152-
153- tests.forEach {
154- val ex =
155- assertFailsWith(
156- exceptionClass = it.second,
157- block = { getMatchingAgent(it.first, ws).id },
158- )
159- assertContains(ex.message.toString(), it.third)
158+ runBlocking {
159+ tests.forEach {
160+ assertNull(protocolHandler.getMatchingAgent(it.first, ws)?.id)
161+ }
160162 }
161163 }
162164
@@ -177,43 +179,10 @@ internal class LinkHandlerTest {
177179 " has no agents"
178180 ),
179181 )
180-
181- tests.forEach {
182- val ex =
183- assertFailsWith(
184- exceptionClass = it.second,
185- block = { getMatchingAgent(it.first, ws).id },
186- )
187- assertContains(ex.message.toString(), it.third)
188- }
189- }
190-
191- @Test
192- fun followsRedirects () {
193- val (srv1, url1) =
194- mockServer { exchange ->
195- exchange.sendResponseHeaders(HttpURLConnection .HTTP_OK , - 1 )
196- exchange.close()
182+ runBlocking {
183+ tests.forEach {
184+ assertNull(protocolHandler.getMatchingAgent(it.first, ws)?.id)
197185 }
198- val (srv2, url2) = mockRedirectServer(url1, false )
199- val (srv3, url3) = mockRedirectServer(url2, true )
200-
201- assertEquals(url1.toURL(), resolveRedirects(java.net.URL (url3)))
202-
203- srv1.stop(0 )
204- srv2.stop(0 )
205- srv3.stop(0 )
206- }
207-
208- @Test
209- fun followsMaximumRedirects () {
210- val (srv, url) = mockRedirectServer(" ." , true )
211-
212- assertFailsWith(
213- exceptionClass = Exception ::class ,
214- block = { resolveRedirects(java.net.URL (url)) },
215- )
216-
217- srv.stop(0 )
186+ }
218187 }
219188}
0 commit comments