Skip to content

Binary content corrupted  #61

@evertontavares-ifood

Description

@evertontavares-ifood

Describe the bug

I'm trying to use mocha to mock a .tar.gz artifact download, but the binary response body is corrupted.

The error occur because the Scanner used to write the mocked body, because "Successive calls to the Scanner.Scan method will step through the 'tokens' of a file, skipping the bytes between the tokens.", and it causes an error when I try to read the file.

The error occurs because of a behavior of the bufio.Scanner used to write the mocked body into the response. The library doc says that "successive calls to the Scanner.Scan method will step through the 'tokens' of a file, skipping the bytes between the tokens" and, as the default token is a \n character, causes the corruption

I'm using github.com/vitorsalgado/mocha/v3 v3.0.2

To Reproduce

package whatever

var (
	//go:embed test/*.tar.gz
	testFiles embed.FS
)

func TestErrorSample(t *testing.T) {
	m := mocha.New(t)
	m.Start()

	content, _ := testFiles.ReadFile("test/any_file.tar.gz")
	m.AddMocks(mocha.Get(expect.URLPath("/test.tar.gz")).
		Reply(reply.OK().Body(content)))

	client := resty.New().SetBaseURL(m.URL())
	resp, err := client.R().Get("/test.tar.gz")
	if err != nil {
		t.Fatal(err)
	}

	assert.Equal(t, content, resp.Body()) // Not equal
}

Expected behavior
I expect that the response body be equal to the configured body.

Screenshots
If applicable, add screenshots to help explain your problem.

Environment (please complete the following information):

  • OS: MacOS Monterey 12.6
  • Version go 1.22.2 darwin/amd64

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions