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