| 1376 | } |
| 1377 | |
| 1378 | func Test_apiRun_inputFile(t *testing.T) { |
| 1379 | tests := []struct { |
| 1380 | name string |
| 1381 | inputFile string |
| 1382 | inputContents []byte |
| 1383 | |
| 1384 | contentLength int64 |
| 1385 | expectedContents []byte |
| 1386 | }{ |
| 1387 | { |
| 1388 | name: "stdin", |
| 1389 | inputFile: "-", |
| 1390 | inputContents: []byte("I WORK OUT"), |
| 1391 | contentLength: 0, |
| 1392 | }, |
| 1393 | { |
| 1394 | name: "from file", |
| 1395 | inputFile: "gh-test-file", |
| 1396 | inputContents: []byte("I WORK OUT"), |
| 1397 | contentLength: 10, |
| 1398 | }, |
| 1399 | } |
| 1400 | |
| 1401 | tempDir := t.TempDir() |
| 1402 | |
| 1403 | for _, tt := range tests { |
| 1404 | t.Run(tt.name, func(t *testing.T) { |
| 1405 | ios, stdin, _, _ := iostreams.Test() |
| 1406 | resp := &http.Response{StatusCode: 204} |
| 1407 | |
| 1408 | inputFile := tt.inputFile |
| 1409 | if tt.inputFile == "-" { |
| 1410 | _, _ = stdin.Write(tt.inputContents) |
| 1411 | } else { |
| 1412 | f, err := os.CreateTemp(tempDir, tt.inputFile) |
| 1413 | if err != nil { |
| 1414 | t.Fatal(err) |
| 1415 | } |
| 1416 | _, _ = f.Write(tt.inputContents) |
| 1417 | defer f.Close() |
| 1418 | inputFile = f.Name() |
| 1419 | } |
| 1420 | |
| 1421 | var bodyBytes []byte |
| 1422 | options := ApiOptions{ |
| 1423 | RequestPath: "hello", |
| 1424 | RequestInputFile: inputFile, |
| 1425 | RawFields: []string{"a=b", "c=d"}, |
| 1426 | |
| 1427 | IO: ios, |
| 1428 | HttpClient: func(rootapi.HTTPClientOptions) (*http.Client, error) { |
| 1429 | var tr roundTripper = func(req *http.Request) (*http.Response, error) { |
| 1430 | var err error |
| 1431 | if bodyBytes, err = io.ReadAll(req.Body); err != nil { |
| 1432 | return nil, err |
| 1433 | } |
| 1434 | resp.Request = req |
| 1435 | return resp, nil |