| 1264 | } |
| 1265 | |
| 1266 | func Test_apiRun_inputFile(t *testing.T) { |
| 1267 | tests := []struct { |
| 1268 | name string |
| 1269 | inputFile string |
| 1270 | inputContents []byte |
| 1271 | |
| 1272 | contentLength int64 |
| 1273 | expectedContents []byte |
| 1274 | }{ |
| 1275 | { |
| 1276 | name: "stdin", |
| 1277 | inputFile: "-", |
| 1278 | inputContents: []byte("I WORK OUT"), |
| 1279 | contentLength: 0, |
| 1280 | }, |
| 1281 | { |
| 1282 | name: "from file", |
| 1283 | inputFile: "gh-test-file", |
| 1284 | inputContents: []byte("I WORK OUT"), |
| 1285 | contentLength: 10, |
| 1286 | }, |
| 1287 | } |
| 1288 | |
| 1289 | tempDir := t.TempDir() |
| 1290 | |
| 1291 | for _, tt := range tests { |
| 1292 | t.Run(tt.name, func(t *testing.T) { |
| 1293 | ios, stdin, _, _ := iostreams.Test() |
| 1294 | resp := &http.Response{StatusCode: 204} |
| 1295 | |
| 1296 | inputFile := tt.inputFile |
| 1297 | if tt.inputFile == "-" { |
| 1298 | _, _ = stdin.Write(tt.inputContents) |
| 1299 | } else { |
| 1300 | f, err := os.CreateTemp(tempDir, tt.inputFile) |
| 1301 | if err != nil { |
| 1302 | t.Fatal(err) |
| 1303 | } |
| 1304 | _, _ = f.Write(tt.inputContents) |
| 1305 | defer f.Close() |
| 1306 | inputFile = f.Name() |
| 1307 | } |
| 1308 | |
| 1309 | var bodyBytes []byte |
| 1310 | options := ApiOptions{ |
| 1311 | RequestPath: "hello", |
| 1312 | RequestInputFile: inputFile, |
| 1313 | RawFields: []string{"a=b", "c=d"}, |
| 1314 | |
| 1315 | IO: ios, |
| 1316 | HttpClient: func() (*http.Client, error) { |
| 1317 | var tr roundTripper = func(req *http.Request) (*http.Response, error) { |
| 1318 | var err error |
| 1319 | if bodyBytes, err = io.ReadAll(req.Body); err != nil { |
| 1320 | return nil, err |
| 1321 | } |
| 1322 | resp.Request = req |
| 1323 | return resp, nil |