| 1446 | } |
| 1447 | |
| 1448 | func Test_apiRun_inputFile(t *testing.T) { |
| 1449 | tests := []struct { |
| 1450 | name string |
| 1451 | inputFile string |
| 1452 | inputContents []byte |
| 1453 | |
| 1454 | contentLength int64 |
| 1455 | expectedContents []byte |
| 1456 | }{ |
| 1457 | { |
| 1458 | name: "stdin", |
| 1459 | inputFile: "-", |
| 1460 | inputContents: []byte("I WORK OUT"), |
| 1461 | contentLength: 0, |
| 1462 | }, |
| 1463 | { |
| 1464 | name: "from file", |
| 1465 | inputFile: "gh-test-file", |
| 1466 | inputContents: []byte("I WORK OUT"), |
| 1467 | contentLength: 10, |
| 1468 | }, |
| 1469 | } |
| 1470 | |
| 1471 | tempDir := t.TempDir() |
| 1472 | |
| 1473 | for _, tt := range tests { |
| 1474 | t.Run(tt.name, func(t *testing.T) { |
| 1475 | ios, stdin, _, _ := iostreams.Test() |
| 1476 | resp := &http.Response{StatusCode: 204} |
| 1477 | |
| 1478 | inputFile := tt.inputFile |
| 1479 | if tt.inputFile == "-" { |
| 1480 | _, _ = stdin.Write(tt.inputContents) |
| 1481 | } else { |
| 1482 | f, err := os.CreateTemp(tempDir, tt.inputFile) |
| 1483 | if err != nil { |
| 1484 | t.Fatal(err) |
| 1485 | } |
| 1486 | _, _ = f.Write(tt.inputContents) |
| 1487 | defer f.Close() |
| 1488 | inputFile = f.Name() |
| 1489 | } |
| 1490 | |
| 1491 | var bodyBytes []byte |
| 1492 | options := ApiOptions{ |
| 1493 | RequestPath: "hello", |
| 1494 | RequestInputFile: inputFile, |
| 1495 | RawFields: []string{"a=b", "c=d"}, |
| 1496 | |
| 1497 | IO: ios, |
| 1498 | HttpClient: func(rootapi.HTTPClientOptions) (*http.Client, error) { |
| 1499 | var tr roundTripper = func(req *http.Request) (*http.Response, error) { |
| 1500 | var err error |
| 1501 | if bodyBytes, err = io.ReadAll(req.Body); err != nil { |
| 1502 | return nil, err |
| 1503 | } |
| 1504 | resp.Request = req |
| 1505 | return resp, nil |