MCPcopy Index your code
hub / github.com/larksuite/cli / TestBuildFormdata

Function TestBuildFormdata

internal/cmdutil/fileupload_test.go:265–392  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

263}
264
265func TestBuildFormdata(t *testing.T) {
266 fio := &localfileio.LocalFileIO{}
267
268 t.Run("stdin success", func(t *testing.T) {
269 stdin := bytes.NewReader([]byte("file-content-here"))
270 fd, err := BuildFormdata(fio, "file", "", true, stdin, nil)
271 if err != nil {
272 t.Fatalf("unexpected error: %v", err)
273 }
274 if fd == nil {
275 t.Fatal("expected non-nil Formdata")
276 }
277 })
278
279 t.Run("stdin nil reader", func(t *testing.T) {
280 _, err := BuildFormdata(fio, "file", "", true, nil, nil)
281 if err == nil {
282 t.Fatal("expected error for nil stdin")
283 }
284 if !strings.Contains(err.Error(), "stdin is not available") {
285 t.Errorf("error = %q, want containing %q", err.Error(), "stdin is not available")
286 }
287 requireFileValidationError(t, err, errs.SubtypeFailedPrecondition)
288 })
289
290 t.Run("stdin read failure", func(t *testing.T) {
291 readErr := errors.New("pipe closed")
292 _, err := BuildFormdata(fio, "file", "", true, &failingReader{err: readErr}, nil)
293 if err == nil {
294 t.Fatal("expected error for failing stdin reader")
295 }
296 requireFileValidationError(t, err, errs.SubtypeInvalidArgument)
297 if !errors.Is(err, readErr) {
298 t.Error("underlying read error not reachable via errors.Is; WithCause missing")
299 }
300 })
301
302 t.Run("stdin empty", func(t *testing.T) {
303 stdin := bytes.NewReader([]byte{})
304 _, err := BuildFormdata(fio, "file", "", true, stdin, nil)
305 if err == nil {
306 t.Fatal("expected error for empty stdin")
307 }
308 if !strings.Contains(err.Error(), "stdin is empty") {
309 t.Errorf("error = %q, want containing %q", err.Error(), "stdin is empty")
310 }
311 requireFileValidationError(t, err, errs.SubtypeInvalidArgument)
312 })
313
314 t.Run("file open success", func(t *testing.T) {
315 dir := t.TempDir()
316 TestChdir(t, dir)
317
318 if err := os.WriteFile(filepath.Join(dir, "test.txt"), []byte("hello"), 0600); err != nil {
319 t.Fatalf("failed to create test file: %v", err)
320 }
321
322 fd, err := BuildFormdata(fio, "photo", "test.txt", false, nil, nil)

Callers

nothing calls this directly

Calls 8

BuildFormdataFunction · 0.85
TestChdirFunction · 0.85
IsMethod · 0.80
RunMethod · 0.65
WriteFileMethod · 0.65
ContainsMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected