@@ -158,6 +158,42 @@ class CoderRestClientTest {
158
158
}
159
159
}
160
160
161
+ @Test
162
+ fun testToken () {
163
+ val user = DataGen .user()
164
+ val (srv, url) = mockServer()
165
+ srv.createContext(
166
+ " /api/v2/users/me" ,
167
+ BaseHttpHandler (" GET" ) { exchange ->
168
+ if (exchange.requestHeaders.getFirst(" Coder-Session-Token" ) != " token" ) {
169
+ val response = Response (" Unauthorized" , " You do not have permission to the requested resource" )
170
+ val body = moshi.adapter(Response ::class .java).toJson(response).toByteArray()
171
+ exchange.sendResponseHeaders(HttpURLConnection .HTTP_UNAUTHORIZED , body.size.toLong())
172
+ exchange.responseBody.write(body)
173
+ } else {
174
+ val body = moshi.adapter(User ::class .java).toJson(user).toByteArray()
175
+ exchange.sendResponseHeaders(HttpURLConnection .HTTP_OK , body.size.toLong())
176
+ exchange.responseBody.write(body)
177
+ }
178
+ },
179
+ )
180
+
181
+ val client = CoderRestClient (URL (url), " token" )
182
+ assertEquals(user.username, client.me().username)
183
+
184
+ val tests = listOf (" invalid" , null )
185
+ tests.forEach { token ->
186
+ val ex =
187
+ assertFailsWith(
188
+ exceptionClass = APIResponseException ::class ,
189
+ block = { CoderRestClient (URL (url), token).me() },
190
+ )
191
+ assertEquals(true , ex.isUnauthorized)
192
+ }
193
+
194
+ srv.stop(0 )
195
+ }
196
+
161
197
@Test
162
198
fun testGetsWorkspaces () {
163
199
val tests =
0 commit comments