Files
httpx/request_form_test.go
Aleksey Shakhmatov b40a373675 Add NewFormRequest for form-encoded HTTP requests
Creates requests with application/x-www-form-urlencoded body from
url.Values. Supports GetBody for retry compatibility, following the
same pattern as NewJSONRequest.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-22 21:47:19 +03:00

81 lines
2.0 KiB
Go

package httpx_test
import (
"context"
"io"
"net/http"
"net/url"
"testing"
"git.codelab.vc/pkg/httpx"
)
func TestNewFormRequest(t *testing.T) {
t.Run("body is form-encoded", func(t *testing.T) {
values := url.Values{"username": {"alice"}, "scope": {"read"}}
req, err := httpx.NewFormRequest(context.Background(), http.MethodPost, "http://example.com/token", values)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
body, err := io.ReadAll(req.Body)
if err != nil {
t.Fatalf("reading body: %v", err)
}
parsed, err := url.ParseQuery(string(body))
if err != nil {
t.Fatalf("parsing form: %v", err)
}
if parsed.Get("username") != "alice" {
t.Errorf("username = %q, want %q", parsed.Get("username"), "alice")
}
if parsed.Get("scope") != "read" {
t.Errorf("scope = %q, want %q", parsed.Get("scope"), "read")
}
})
t.Run("content type is set", func(t *testing.T) {
values := url.Values{"key": {"value"}}
req, err := httpx.NewFormRequest(context.Background(), http.MethodPost, "http://example.com", values)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
ct := req.Header.Get("Content-Type")
if ct != "application/x-www-form-urlencoded" {
t.Errorf("Content-Type = %q, want %q", ct, "application/x-www-form-urlencoded")
}
})
t.Run("GetBody works for retry", func(t *testing.T) {
values := url.Values{"key": {"value"}}
req, err := httpx.NewFormRequest(context.Background(), http.MethodPost, "http://example.com", values)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if req.GetBody == nil {
t.Fatal("GetBody is nil")
}
b1, err := io.ReadAll(req.Body)
if err != nil {
t.Fatalf("reading body: %v", err)
}
body2, err := req.GetBody()
if err != nil {
t.Fatalf("GetBody(): %v", err)
}
b2, err := io.ReadAll(body2)
if err != nil {
t.Fatalf("reading body2: %v", err)
}
if string(b1) != string(b2) {
t.Errorf("GetBody returned different data: %q vs %q", b1, b2)
}
})
}