MCPcopy
hub / github.com/valyala/fasthttp / testFSByteRange

Function testFSByteRange

fs_test.go:580–629  ·  view source on GitHub ↗
(t *testing.T, h RequestHandler, filePath string)

Source from the content-addressed store, hash-verified

578}
579
580func testFSByteRange(t *testing.T, h RequestHandler, filePath string) {
581 t.Helper()
582
583 var ctx RequestCtx
584 ctx.Init(&Request{}, nil, nil)
585
586 expectedBody, err := getFileContents(filePath)
587 if err != nil {
588 t.Fatalf("cannot read file %q: %v", filePath, err)
589 }
590
591 fileSize := len(expectedBody)
592 startPos := rand.Intn(fileSize)
593 endPos := rand.Intn(fileSize)
594 if endPos < startPos {
595 startPos, endPos = endPos, startPos
596 }
597
598 ctx.Request.SetRequestURI(filePath)
599 ctx.Request.Header.SetByteRange(startPos, endPos)
600 h(&ctx)
601
602 var resp Response
603 s := ctx.Response.String()
604 br := bufio.NewReader(bytes.NewBufferString(s))
605 if err := resp.Read(br); err != nil {
606 t.Fatalf("unexpected error: %v. filePath=%q", err, filePath)
607 }
608 if resp.StatusCode() != StatusPartialContent {
609 t.Fatalf("unexpected status code: %d. Expecting %d. filePath=%q", resp.StatusCode(), StatusPartialContent, filePath)
610 }
611 cr := resp.Header.Peek(HeaderContentRange)
612
613 expectedCR := fmt.Sprintf("bytes %d-%d/%d", startPos, endPos, fileSize)
614 if string(cr) != expectedCR {
615 t.Fatalf("unexpected content-range %q. Expecting %q. filePath=%q", cr, expectedCR, filePath)
616 }
617 body := resp.Body()
618 bodySize := endPos - startPos + 1
619 if len(body) != bodySize {
620 t.Fatalf("unexpected body size %d. Expecting %d. filePath=%q, startPos=%d, endPos=%d",
621 len(body), bodySize, filePath, startPos, endPos)
622 }
623
624 expectedBody = expectedBody[startPos : endPos+1]
625 if !bytes.Equal(body, expectedBody) {
626 t.Fatalf("unexpected body %q. Expecting %q. filePath=%q, startPos=%d, endPos=%d",
627 body, expectedBody, filePath, startPos, endPos)
628 }
629}
630
631func getFileContents(path string) ([]byte, error) {
632 path = "." + path

Calls 10

InitMethod · 0.95
ReadMethod · 0.95
StatusCodeMethod · 0.95
BodyMethod · 0.95
getFileContentsFunction · 0.85
SetByteRangeMethod · 0.80
NewReaderMethod · 0.80
PeekMethod · 0.65
SetRequestURIMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…