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

WithQueryObject encode empty values to empty string #438

Closed
sanikoyes opened this issue Dec 21, 2023 · 1 comment
Closed

WithQueryObject encode empty values to empty string #438

sanikoyes opened this issue Dec 21, 2023 · 1 comment
Assignees
Labels
feature New feature or request
Milestone

Comments

@sanikoyes
Copy link

sanikoyes commented Dec 21, 2023

The form.EncodeToValues interface used in the WithQueryObject function will escape the empty value in the query object into an empty string "", such as the number 0, or a length 0 of array/slice/map etc.

Please provide an option to turn off this feature when create httpexpect.Expect, (the form library can create an Encoder and then call KeepZeros(false) to disable this feature)

@gavv gavv changed the title WithQueryObject encode empty values to empty string '' WithQueryObject encode empty values to empty string Mar 2, 2025
@gavv
Copy link
Owner

gavv commented Mar 3, 2025

Hi, thanks for report.

Pushed to master: 7844b86

You can now use WithQueryEncoder() with three encoders available:

	// QueryEncoderGoogle encodes query using google/go-querystring package.
	// Query object should be a struct annotated with `url` tags.
	// See https://github.com/google/go-querystring.
	QueryEncoderGoogle

	// QueryEncoderForm encodes query using ajg/form package.
	// Query object can be arbitrary type including maps and structs
	// annotated with `form` tags.
	// See https://github.com/ajg/form.
	QueryEncoderForm

	// QueryEncoderFormKeepZeros is same as QueryEncoderForm, but with KeepZeros
	// flag enabled. With this flag, encoder keeps zero (default) values in their
	// literal form when encoding, and returns the former; by default zero values
	// are not kept, but are rather encoded as the empty string.
	QueryEncoderFormKeepZeros

Please provide an option to turn off this feature when create httpexpect.Expect

Such options are usually per-request, because otherwise httpexpect.Config would quickly become a mess. But you can easily make it global by using a request builder:

e := httpexpect.Default(t, "http://example.com")

e2 := e.Builder(func (req *httpexpect.Request) {
	req.WithQueryEncoder(httpexpect.QueryEncoderFormKeepZeros)
})

// now use e2 everywhere

I'm closing the issue, but feel free to re-open if you have any troubles.

@gavv gavv closed this as completed Mar 3, 2025
@gavv gavv added the feature New feature or request label Mar 3, 2025
@gavv gavv added this to the v2 milestone Mar 3, 2025
@gavv gavv self-assigned this Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants