(t *testing.T)
| 24 | } |
| 25 | |
| 26 | func TestPget(t *testing.T) { |
| 27 | // listening file server |
| 28 | mux := http.NewServeMux() |
| 29 | mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 30 | http.Redirect(w, r, "/moo", http.StatusFound) |
| 31 | }) |
| 32 | |
| 33 | mux.HandleFunc("/moo", func(w http.ResponseWriter, r *http.Request) { |
| 34 | http.Redirect(w, r, "/mooo", http.StatusFound) |
| 35 | }) |
| 36 | |
| 37 | mux.HandleFunc("/mooo", func(w http.ResponseWriter, r *http.Request) { |
| 38 | http.Redirect(w, r, "/test.tar.gz", http.StatusFound) |
| 39 | }) |
| 40 | |
| 41 | mux.HandleFunc("/test.tar.gz", func(w http.ResponseWriter, r *http.Request) { |
| 42 | fp := "_testdata/test.tar.gz" |
| 43 | data, err := ioutil.ReadFile(fp) |
| 44 | if err != nil { |
| 45 | t.Errorf("failed to readfile: %s", err) |
| 46 | } |
| 47 | http.ServeContent(w, r, fp, time.Now(), bytes.NewReader(data)) |
| 48 | }) |
| 49 | |
| 50 | ts := httptest.NewServer(mux) |
| 51 | defer ts.Close() |
| 52 | |
| 53 | // begin tests |
| 54 | url := ts.URL |
| 55 | |
| 56 | tmpdir := t.TempDir() |
| 57 | |
| 58 | cfg := &DownloadConfig{ |
| 59 | Filename: "test.tar.gz", |
| 60 | ContentLength: 1719652, |
| 61 | Dirname: tmpdir, |
| 62 | Procs: 4, |
| 63 | URLs: []string{ts.URL}, |
| 64 | Client: newDownloadClient(1), |
| 65 | } |
| 66 | |
| 67 | t.Run("check", func(t *testing.T) { |
| 68 | target, err := Check(context.Background(), &CheckConfig{ |
| 69 | URLs: []string{url}, |
| 70 | Timeout: 10 * time.Second, |
| 71 | }) |
| 72 | |
| 73 | if err != nil { |
| 74 | t.Fatalf("failed to check header: %s", err) |
| 75 | } |
| 76 | |
| 77 | if len(target.URLs) == 0 { |
| 78 | t.Fatalf("invalid URL length %d", len(target.URLs)) |
| 79 | } |
| 80 | |
| 81 | // could redirect? |
| 82 | assert.NotEqual(t, target.URLs[0], url, "failed to get of the last url in the redirect") |
| 83 | }) |
nothing calls this directly
no test coverage detected