Skip to content

Commit 7f654e4

Browse files
committed
Test download failure
1 parent 0c47258 commit 7f654e4

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

src/main/kotlin/com/coder/gateway/sdk/CoderCLIManager.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class CoderCLIManager @JvmOverloads constructor(deployment: URL, destinationDir:
117117
} finally {
118118
conn.disconnect()
119119
}
120-
throw Exception("Unable to download $remoteBinaryUrl (got response code `${conn.responseCode}`)")
120+
throw ResponseException("Unexpected response from $remoteBinaryUrl", conn.responseCode)
121121
}
122122

123123
/**
@@ -251,3 +251,5 @@ class Environment(private val env: Map<String, String> = emptyMap()) {
251251
return System.getenv(name)
252252
}
253253
}
254+
255+
class ResponseException(message: String, val code: Int) : Exception(message)

src/main/kotlin/com/coder/gateway/views/steps/CoderWorkspacesStepView.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import com.coder.gateway.sdk.CoderSemVer
1616
import com.coder.gateway.sdk.IncompatibleVersionException
1717
import com.coder.gateway.sdk.InvalidVersionException
1818
import com.coder.gateway.sdk.OS
19+
import com.coder.gateway.sdk.ResponseException
1920
import com.coder.gateway.sdk.TemplateIconDownloader
2021
import com.coder.gateway.sdk.ex.AuthenticationResponseException
2122
import com.coder.gateway.sdk.ex.TemplateResponseException
@@ -444,6 +445,9 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
444445
}
445446
try {
446447
cliManager.downloadCLI()
448+
} catch (e: ResponseException) {
449+
logger.error("Download failed with response code ${e.code}", e)
450+
return@launchUnderBackgroundProgress
447451
} catch (e: Exception) {
448452
logger.error("Failed to download Coder CLI", e)
449453
return@launchUnderBackgroundProgress

src/test/groovy/CoderCLIManagerTest.groovy

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ class CoderCLIManagerTest extends spock.lang.Specification {
2121
@Shared
2222
private def servers = []
2323

24-
String startMockServer() {
24+
/**
25+
* Create, start, and return a server that mocks Coder.
26+
*/
27+
HttpServer startMockServer(errorCode = 0) {
2528
HttpServer srv = HttpServer.create(new InetSocketAddress(0), 0)
2629
srv.createContext("/", new HttpHandler() {
2730
void handle(HttpExchange exchange) {
@@ -39,6 +42,11 @@ class CoderCLIManagerTest extends spock.lang.Specification {
3942
response = "not found"
4043
}
4144

45+
if (errorCode != 0) {
46+
code = errorCode
47+
response = "error code ${code}"
48+
}
49+
4250
byte[] body = response.getBytes()
4351
exchange.sendResponseHeaders(code, code == HttpURLConnection.HTTP_OK ? body.length : -1)
4452
exchange.responseBody.write(body)
@@ -102,6 +110,20 @@ class CoderCLIManagerTest extends spock.lang.Specification {
102110
ccm.localBinaryPath.getParent() == CoderCLIManager.getDataDir().resolve("test.xn--n28h.invalid")
103111
}
104112

113+
def "fails to download"() {
114+
given:
115+
HttpServer server = startMockServer(HttpURLConnection.HTTP_INTERNAL_ERROR)
116+
String url = "http://localhost:" + server.address.port
117+
def ccm = new CoderCLIManager(new URL(url), tmpdir)
118+
119+
when:
120+
ccm.downloadCLI()
121+
122+
then:
123+
def e = thrown(ResponseException)
124+
e.code == HttpURLConnection.HTTP_INTERNAL_ERROR
125+
}
126+
105127
def "downloads a working cli"() {
106128
given:
107129
def ccm = createCLIManager()

0 commit comments

Comments
 (0)