Skip to content

Commit ae6b9c5

Browse files
committed
Add token test
1 parent a0d581c commit ae6b9c5

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/test/kotlin/com/coder/gateway/sdk/CoderRestClientTest.kt

+36
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,42 @@ class CoderRestClientTest {
158158
}
159159
}
160160

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+
161197
@Test
162198
fun testGetsWorkspaces() {
163199
val tests =

0 commit comments

Comments
 (0)