| 1344 | } |
| 1345 | |
| 1346 | func Test_apiRun_inputFile(t *testing.T) { |
| 1347 | tests := []struct { |
| 1348 | name string |
| 1349 | inputFile string |
| 1350 | inputContents []byte |
| 1351 | |
| 1352 | contentLength int64 |
| 1353 | expectedContents []byte |
| 1354 | }{ |
| 1355 | { |
| 1356 | name: "stdin", |
| 1357 | inputFile: "-", |
| 1358 | inputContents: []byte("I WORK OUT"), |
| 1359 | contentLength: 0, |
| 1360 | }, |
| 1361 | { |
| 1362 | name: "from file", |
| 1363 | inputFile: "gh-test-file", |
| 1364 | inputContents: []byte("I WORK OUT"), |
| 1365 | contentLength: 10, |
| 1366 | }, |
| 1367 | } |
| 1368 | |
| 1369 | tempDir := t.TempDir() |
| 1370 | |
| 1371 | for _, tt := range tests { |
| 1372 | t.Run(tt.name, func(t *testing.T) { |
| 1373 | ios, stdin, _, _ := iostreams.Test() |
| 1374 | resp := &http.Response{StatusCode: 204} |
| 1375 | |
| 1376 | inputFile := tt.inputFile |
| 1377 | if tt.inputFile == "-" { |
| 1378 | _, _ = stdin.Write(tt.inputContents) |
| 1379 | } else { |
| 1380 | f, err := os.CreateTemp(tempDir, tt.inputFile) |
| 1381 | if err != nil { |
| 1382 | t.Fatal(err) |
| 1383 | } |
| 1384 | _, _ = f.Write(tt.inputContents) |
| 1385 | defer f.Close() |
| 1386 | inputFile = f.Name() |
| 1387 | } |
| 1388 | |
| 1389 | var bodyBytes []byte |
| 1390 | options := ApiOptions{ |
| 1391 | RequestPath: "hello", |
| 1392 | RequestInputFile: inputFile, |
| 1393 | RawFields: []string{"a=b", "c=d"}, |
| 1394 | |
| 1395 | IO: ios, |
| 1396 | HttpClient: func() (*http.Client, error) { |
| 1397 | var tr roundTripper = func(req *http.Request) (*http.Response, error) { |
| 1398 | var err error |
| 1399 | if bodyBytes, err = io.ReadAll(req.Body); err != nil { |
| 1400 | return nil, err |
| 1401 | } |
| 1402 | resp.Request = req |
| 1403 | return resp, nil |