MCPcopy
hub / github.com/grafana/k6 / TestRequestWithMultipleBinaryFiles

Function TestRequestWithMultipleBinaryFiles

internal/js/initcontext_test.go:374–513  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

372}
373
374func TestRequestWithMultipleBinaryFiles(t *testing.T) {
375 t.Parallel()
376
377 ch := make(chan bool, 1)
378
379 h := func(_ http.ResponseWriter, r *http.Request) {
380 defer func() {
381 ch <- true
382 }()
383
384 require.NoError(t, r.ParseMultipartForm(32<<20))
385 require.Len(t, r.MultipartForm.File["files"], 2)
386 for i, fh := range r.MultipartForm.File["files"] {
387 f, _ := fh.Open()
388 defer func() { require.NoError(t, f.Close()) }()
389 bytes := make([]byte, 5)
390 _, err := f.Read(bytes)
391 require.NoError(t, err)
392 switch i {
393 case 0:
394 assert.Equal(t, []byte("file1"), bytes)
395 case 1:
396 assert.Equal(t, []byte("file2"), bytes)
397 }
398 }
399 }
400
401 srv := httptest.NewServer(http.HandlerFunc(h))
402 defer srv.Close()
403
404 fs := fsext.NewMemMapFs()
405 require.NoError(t, fs.MkdirAll("/path/to", 0o755))
406 require.NoError(t, fsext.WriteFile(fs, "/path/to/file1.bin", []byte("file1"), 0o644))
407 require.NoError(t, fsext.WriteFile(fs, "/path/to/file2.bin", []byte("file2"), 0o644))
408
409 b, err := getSimpleBundle(t, "/path/to/script.js",
410 fmt.Sprintf(`
411 import http from 'k6/http';
412
413 function toByteArray(obj) {
414 let arr = [];
415 if (typeof obj === 'string') {
416 for (let i=0; i < obj.length; i++) {
417 arr.push(obj.charCodeAt(i) & 0xff);
418 }
419 } else {
420 obj = new Uint8Array(obj);
421 for (let i=0; i < obj.byteLength; i++) {
422 arr.push(obj[i] & 0xff);
423 }
424 }
425 return arr;
426 }
427
428 // A more robust version of this polyfill is available here:
429 // https://jslib.k6.io/formdata/0.0.1/index.js
430 function FormData() {
431 this.boundary = '----boundary';

Callers

nothing calls this directly

Calls 15

RootTagSetMethod · 0.95
NewMemMapFsFunction · 0.92
WriteFileFunction · 0.92
NewRegistryFunction · 0.92
RegisterBuiltinMetricsFunction · 0.92
NewDialerFunction · 0.92
NewResolverFunction · 0.92
NewBufferPoolFunction · 0.92
NewVUStateTagsFunction · 0.92
getSimpleBundleFunction · 0.85
EqualMethod · 0.80
MkdirAllMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…