Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary content corrupted #61

Open
evertontavares-ifood opened this issue Apr 30, 2024 · 0 comments
Open

Binary content corrupted #61

evertontavares-ifood opened this issue Apr 30, 2024 · 0 comments

Comments

@evertontavares-ifood
Copy link

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

@evertontavares-ifood evertontavares-ifood changed the title Binary file corrupted after Binary file corrupted Apr 30, 2024
@evertontavares-ifood evertontavares-ifood changed the title Binary file corrupted Binary content corrupted Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant