|
16 | 16 | package update_test
|
17 | 17 |
|
18 | 18 | import (
|
| 19 | + "fmt" |
| 20 | + "net/http" |
| 21 | + "net/url" |
19 | 22 | "strings"
|
20 | 23 | "testing"
|
21 | 24 |
|
@@ -68,3 +71,44 @@ func TestUpdateShowingOutdated(t *testing.T) {
|
68 | 71 | require.True(t, strings.HasPrefix(lines[3], "Arduino AVR Boards"))
|
69 | 72 | require.True(t, strings.HasPrefix(lines[6], "USBHost"))
|
70 | 73 | }
|
| 74 | + |
| 75 | +func serveFileWithCode(port uint16, path string, code int, env *integrationtest.Environment) *url.URL { |
| 76 | + mux := http.NewServeMux() |
| 77 | + mux.HandleFunc("/"+path, func(w http.ResponseWriter, r *http.Request) { |
| 78 | + w.WriteHeader(code) |
| 79 | + }) |
| 80 | + server := &http.Server{ |
| 81 | + Addr: fmt.Sprintf(":%d", port), |
| 82 | + Handler: mux, |
| 83 | + } |
| 84 | + |
| 85 | + t := env.T() |
| 86 | + fileURL, err := url.Parse(fmt.Sprintf("http://127.0.0.1:%d/%s", port, path)) |
| 87 | + require.NoError(t, err) |
| 88 | + |
| 89 | + go func() { |
| 90 | + err := server.ListenAndServe() |
| 91 | + require.Equal(t, err, http.ErrServerClosed) |
| 92 | + }() |
| 93 | + |
| 94 | + env.RegisterCleanUpCallback(func() { |
| 95 | + server.Close() |
| 96 | + }) |
| 97 | + |
| 98 | + return fileURL |
| 99 | +} |
| 100 | + |
| 101 | +func TestUpdateWithUrlNotFound(t *testing.T) { |
| 102 | + env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t) |
| 103 | + defer env.CleanUp() |
| 104 | + |
| 105 | + _, _, err := cli.Run("update") |
| 106 | + require.NoError(t, err) |
| 107 | + |
| 108 | + // Brings up a local server to fake a failure |
| 109 | + url := serveFileWithCode(8080, "test_index.json", 404, env) |
| 110 | + |
| 111 | + stdout, _, err := cli.Run("update", "--additional-urls="+url.String()) |
| 112 | + require.Error(t, err) |
| 113 | + require.Contains(t, string(stdout), "Downloading index: test_index.json Server responded with: 404 Not Found") |
| 114 | +} |
0 commit comments