1
1
package com.coder.toolbox.util
2
2
3
+ import com.coder.toolbox.CoderToolboxContext
3
4
import 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
8
21
import java.util.UUID
9
22
import kotlin.test.Test
10
- import kotlin.test.assertContains
11
23
import kotlin.test.assertEquals
12
- import kotlin.test.assertFailsWith
24
+ import kotlin.test.assertNull
13
25
14
26
internal 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
+ )
39
47
40
48
private val agents =
41
49
mapOf (
@@ -49,7 +57,7 @@ internal class LinkHandlerTest {
49
57
)
50
58
51
59
@Test
52
- fun getMatchingAgent () {
60
+ fun tstgetMatchingAgent () {
53
61
val ws = DataGen .workspace(" ws" , agents = agents)
54
62
55
63
val tests =
@@ -74,9 +82,10 @@ internal class LinkHandlerTest {
74
82
" b0e4c54d-9ba9-4413-8512-11ca1e826a24" ,
75
83
),
76
84
)
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
+ }
80
89
}
81
90
}
82
91
@@ -104,14 +113,10 @@ internal class LinkHandlerTest {
104
113
" agent with ID" ,
105
114
),
106
115
)
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
+ }
115
120
}
116
121
}
117
122
@@ -126,15 +131,16 @@ internal class LinkHandlerTest {
126
131
mapOf (" agent" to null ),
127
132
mapOf (" agent_id" to null ),
128
133
)
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
+ }
138
144
}
139
145
}
140
146
@@ -149,14 +155,10 @@ internal class LinkHandlerTest {
149
155
" agent with ID"
150
156
),
151
157
)
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
+ }
160
162
}
161
163
}
162
164
@@ -177,43 +179,10 @@ internal class LinkHandlerTest {
177
179
" has no agents"
178
180
),
179
181
)
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)
197
185
}
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
+ }
218
187
}
219
188
}
0 commit comments