|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "google.golang.org/api/drive/v3" |
| 14 | + "google.golang.org/api/option" |
| 15 | +) |
| 16 | + |
| 17 | +func TestDownloadDriveFile_NonGoogleDoc(t *testing.T) { |
| 18 | + body := "hello" |
| 19 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 20 | + // Files.Get(...).Download hits /drive/v3/files/{id}?alt=media |
| 21 | + if !(strings.Contains(r.URL.Path, "/files/") && r.URL.Query().Get("alt") == "media") { |
| 22 | + http.NotFound(w, r) |
| 23 | + return |
| 24 | + } |
| 25 | + w.WriteHeader(http.StatusOK) |
| 26 | + _, _ = io.WriteString(w, body) |
| 27 | + })) |
| 28 | + defer srv.Close() |
| 29 | + |
| 30 | + svc, err := drive.NewService(context.Background(), |
| 31 | + option.WithoutAuthentication(), |
| 32 | + option.WithHTTPClient(srv.Client()), |
| 33 | + option.WithEndpoint(srv.URL+"/"), |
| 34 | + ) |
| 35 | + if err != nil { |
| 36 | + t.Fatalf("NewService: %v", err) |
| 37 | + } |
| 38 | + |
| 39 | + tmp := t.TempDir() |
| 40 | + dest := filepath.Join(tmp, "file.bin") |
| 41 | + outPath, n, err := downloadDriveFile(context.Background(), svc, &drive.File{Id: "id1", MimeType: "application/pdf"}, dest) |
| 42 | + if err != nil { |
| 43 | + t.Fatalf("downloadDriveFile: %v", err) |
| 44 | + } |
| 45 | + if outPath != dest { |
| 46 | + t.Fatalf("unexpected outPath: %q", outPath) |
| 47 | + } |
| 48 | + if n != int64(len(body)) { |
| 49 | + t.Fatalf("unexpected n: %d", n) |
| 50 | + } |
| 51 | + b, err := os.ReadFile(dest) |
| 52 | + if err != nil { |
| 53 | + t.Fatalf("read: %v", err) |
| 54 | + } |
| 55 | + if string(b) != body { |
| 56 | + t.Fatalf("unexpected body: %q", string(b)) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestDownloadDriveFile_GoogleDocExport(t *testing.T) { |
| 61 | + body := "exported" |
| 62 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 63 | + // Files.Export(...).Download hits /drive/v3/files/{id}/export?mimeType=... |
| 64 | + if !(strings.Contains(r.URL.Path, "/export") && strings.Contains(r.URL.Path, "/files/")) { |
| 65 | + http.NotFound(w, r) |
| 66 | + return |
| 67 | + } |
| 68 | + w.WriteHeader(http.StatusOK) |
| 69 | + _, _ = io.WriteString(w, body) |
| 70 | + })) |
| 71 | + defer srv.Close() |
| 72 | + |
| 73 | + svc, err := drive.NewService(context.Background(), |
| 74 | + option.WithoutAuthentication(), |
| 75 | + option.WithHTTPClient(srv.Client()), |
| 76 | + option.WithEndpoint(srv.URL+"/"), |
| 77 | + ) |
| 78 | + if err != nil { |
| 79 | + t.Fatalf("NewService: %v", err) |
| 80 | + } |
| 81 | + |
| 82 | + tmp := t.TempDir() |
| 83 | + dest := filepath.Join(tmp, "doc.txt") |
| 84 | + outPath, n, err := downloadDriveFile(context.Background(), svc, &drive.File{Id: "id1", MimeType: "application/vnd.google-apps.document"}, dest) |
| 85 | + if err != nil { |
| 86 | + t.Fatalf("downloadDriveFile: %v", err) |
| 87 | + } |
| 88 | + if !strings.HasSuffix(outPath, ".pdf") { |
| 89 | + t.Fatalf("expected pdf outPath, got: %q", outPath) |
| 90 | + } |
| 91 | + if n != int64(len(body)) { |
| 92 | + t.Fatalf("unexpected n: %d", n) |
| 93 | + } |
| 94 | + b, err := os.ReadFile(outPath) |
| 95 | + if err != nil { |
| 96 | + t.Fatalf("read: %v", err) |
| 97 | + } |
| 98 | + if string(b) != body { |
| 99 | + t.Fatalf("unexpected body: %q", string(b)) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func TestDownloadDriveFile_HTTPError(t *testing.T) { |
| 104 | + orig := driveDownload |
| 105 | + t.Cleanup(func() { driveDownload = orig }) |
| 106 | + driveDownload = func(context.Context, *drive.Service, string) (*http.Response, error) { |
| 107 | + return &http.Response{ |
| 108 | + Status: "403 Forbidden", |
| 109 | + StatusCode: http.StatusForbidden, |
| 110 | + Body: io.NopCloser(strings.NewReader("nope\n")), |
| 111 | + }, nil |
| 112 | + } |
| 113 | + |
| 114 | + tmp := t.TempDir() |
| 115 | + dest := filepath.Join(tmp, "file.bin") |
| 116 | + _, _, err := downloadDriveFile(context.Background(), &drive.Service{}, &drive.File{Id: "id1", MimeType: "application/pdf"}, dest) |
| 117 | + if err == nil { |
| 118 | + t.Fatalf("expected error") |
| 119 | + } |
| 120 | + if !strings.Contains(err.Error(), "download failed") || !strings.Contains(err.Error(), "nope") { |
| 121 | + t.Fatalf("unexpected error: %v", err) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func TestDownloadDriveFile_CreateError(t *testing.T) { |
| 126 | + orig := driveDownload |
| 127 | + t.Cleanup(func() { driveDownload = orig }) |
| 128 | + driveDownload = func(context.Context, *drive.Service, string) (*http.Response, error) { |
| 129 | + return &http.Response{ |
| 130 | + Status: "200 OK", |
| 131 | + StatusCode: http.StatusOK, |
| 132 | + Body: io.NopCloser(strings.NewReader("x")), |
| 133 | + }, nil |
| 134 | + } |
| 135 | + |
| 136 | + tmp := t.TempDir() |
| 137 | + dest := filepath.Join(tmp, "no-such-dir", "file.bin") |
| 138 | + _, _, err := downloadDriveFile(context.Background(), &drive.Service{}, &drive.File{Id: "id1", MimeType: "application/pdf"}, dest) |
| 139 | + if err == nil { |
| 140 | + t.Fatalf("expected error") |
| 141 | + } |
| 142 | +} |
0 commit comments