2022-07-17 12:37:23 +08:00
|
|
|
package internal
|
2022-07-16 14:11:34 +08:00
|
|
|
|
|
|
|
|
import (
|
2022-10-19 20:39:46 +08:00
|
|
|
"errors"
|
2022-10-17 06:30:58 +08:00
|
|
|
"net/http"
|
2022-07-16 14:11:34 +08:00
|
|
|
"net/http/httptest"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2025-08-09 12:40:07 +08:00
|
|
|
"github.com/golang/protobuf/proto"
|
2022-07-16 14:11:34 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/zeromicro/go-zero/rest/pathvar"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestNewRequestParserNoVar(t *testing.T) {
|
2022-10-17 06:30:58 +08:00
|
|
|
req := httptest.NewRequest("GET", "/", http.NoBody)
|
2025-08-09 12:40:07 +08:00
|
|
|
parser, err := NewRequestParser(req, nil, false)
|
2022-07-16 14:11:34 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.NotNil(t, parser)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNewRequestParserWithVars(t *testing.T) {
|
2022-10-17 06:30:58 +08:00
|
|
|
req := httptest.NewRequest("GET", "/", http.NoBody)
|
2022-07-16 14:11:34 +08:00
|
|
|
req = pathvar.WithVars(req, map[string]string{"a": "b"})
|
2025-08-09 12:40:07 +08:00
|
|
|
parser, err := NewRequestParser(req, nil, false)
|
2022-07-16 14:11:34 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.NotNil(t, parser)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNewRequestParserNoVarWithBody(t *testing.T) {
|
|
|
|
|
req := httptest.NewRequest("GET", "/", strings.NewReader(`{"a": "b"}`))
|
2025-08-09 12:40:07 +08:00
|
|
|
parser, err := NewRequestParser(req, nil, false)
|
2022-07-16 14:11:34 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.NotNil(t, parser)
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 20:39:46 +08:00
|
|
|
func TestNewRequestParserWithNegativeContentLength(t *testing.T) {
|
|
|
|
|
req := httptest.NewRequest("GET", "/", strings.NewReader(`{"a": "b"}`))
|
|
|
|
|
req.ContentLength = -1
|
2025-08-09 12:40:07 +08:00
|
|
|
parser, err := NewRequestParser(req, nil, false)
|
2022-10-19 20:39:46 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.NotNil(t, parser)
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-16 14:11:34 +08:00
|
|
|
func TestNewRequestParserWithVarsWithBody(t *testing.T) {
|
|
|
|
|
req := httptest.NewRequest("GET", "/", strings.NewReader(`{"a": "b"}`))
|
|
|
|
|
req = pathvar.WithVars(req, map[string]string{"c": "d"})
|
2025-08-09 12:40:07 +08:00
|
|
|
parser, err := NewRequestParser(req, nil, false)
|
2022-07-16 14:11:34 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.NotNil(t, parser)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNewRequestParserWithVarsWithWrongBody(t *testing.T) {
|
|
|
|
|
req := httptest.NewRequest("GET", "/", strings.NewReader(`{"a": "b"`))
|
|
|
|
|
req = pathvar.WithVars(req, map[string]string{"c": "d"})
|
2025-08-09 12:40:07 +08:00
|
|
|
parser, err := NewRequestParser(req, nil, false)
|
2022-07-16 14:11:34 +08:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
assert.Nil(t, parser)
|
|
|
|
|
}
|
2022-07-16 23:40:53 +08:00
|
|
|
|
|
|
|
|
func TestNewRequestParserWithForm(t *testing.T) {
|
2022-10-19 20:39:46 +08:00
|
|
|
req := httptest.NewRequest("GET", "/val?a=b", nil)
|
2025-08-09 12:40:07 +08:00
|
|
|
parser, err := NewRequestParser(req, nil, false)
|
2022-10-19 20:39:46 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.NotNil(t, parser)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNewRequestParserWithNilBody(t *testing.T) {
|
2022-10-17 06:30:58 +08:00
|
|
|
req := httptest.NewRequest("GET", "/val?a=b", http.NoBody)
|
2022-10-19 20:39:46 +08:00
|
|
|
req.Body = nil
|
2025-08-09 12:40:07 +08:00
|
|
|
parser, err := NewRequestParser(req, nil, false)
|
2022-10-19 20:39:46 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.NotNil(t, parser)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNewRequestParserWithBadBody(t *testing.T) {
|
|
|
|
|
req := httptest.NewRequest("GET", "/val?a=b", badBody{})
|
|
|
|
|
req.Body = badBody{}
|
2025-08-09 12:40:07 +08:00
|
|
|
parser, err := NewRequestParser(req, nil, false)
|
2022-07-16 23:40:53 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.NotNil(t, parser)
|
|
|
|
|
}
|
2022-09-19 13:52:14 +08:00
|
|
|
|
|
|
|
|
func TestNewRequestParserWithBadForm(t *testing.T) {
|
2022-10-17 06:30:58 +08:00
|
|
|
req := httptest.NewRequest("GET", "/val?a%1=b", http.NoBody)
|
2025-08-09 12:40:07 +08:00
|
|
|
parser, err := NewRequestParser(req, nil, false)
|
2022-09-19 13:52:14 +08:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
assert.Nil(t, parser)
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-09 12:40:07 +08:00
|
|
|
func TestRequestParser_buildJsonRequestParserFromMap(t *testing.T) {
|
|
|
|
|
parser, err := buildJsonRequestParserFromMap(map[string]any{"a": make(chan int)}, nil, false)
|
2022-09-19 13:52:14 +08:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
assert.Nil(t, parser)
|
|
|
|
|
}
|
2022-10-19 20:39:46 +08:00
|
|
|
|
2025-08-09 12:40:07 +08:00
|
|
|
// mockAnyResolver is a simple implementation of jsonpb.AnyResolver for testing
|
|
|
|
|
type mockAnyResolver struct{}
|
|
|
|
|
|
|
|
|
|
func (m *mockAnyResolver) Resolve(typeUrl string) (proto.Message, error) {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNewRequestParserWithIgnoreUnknownFields(t *testing.T) {
|
|
|
|
|
// Create a concrete resolver for testing
|
|
|
|
|
resolver := &mockAnyResolver{}
|
|
|
|
|
|
|
|
|
|
// Test case 1: No body, no vars - should work with both true and false
|
|
|
|
|
req1 := httptest.NewRequest("GET", "/", http.NoBody)
|
|
|
|
|
parser1, err1 := NewRequestParser(req1, resolver, true)
|
|
|
|
|
assert.Nil(t, err1)
|
|
|
|
|
assert.NotNil(t, parser1)
|
|
|
|
|
|
|
|
|
|
req2 := httptest.NewRequest("GET", "/", http.NoBody)
|
|
|
|
|
parser2, err2 := NewRequestParser(req2, resolver, false)
|
|
|
|
|
assert.Nil(t, err2)
|
|
|
|
|
assert.NotNil(t, parser2)
|
|
|
|
|
|
|
|
|
|
// Test case 2: With JSON body - tests the body parsing path
|
|
|
|
|
req3 := httptest.NewRequest("POST", "/", strings.NewReader(`{"field": "value"}`))
|
|
|
|
|
parser3, err3 := NewRequestParser(req3, resolver, true)
|
|
|
|
|
assert.Nil(t, err3)
|
|
|
|
|
assert.NotNil(t, parser3)
|
|
|
|
|
|
|
|
|
|
req4 := httptest.NewRequest("POST", "/", strings.NewReader(`{"field": "value"}`))
|
|
|
|
|
parser4, err4 := NewRequestParser(req4, resolver, false)
|
|
|
|
|
assert.Nil(t, err4)
|
|
|
|
|
assert.NotNil(t, parser4)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNewRequestParserWithVarsAndIgnoreUnknownFields(t *testing.T) {
|
|
|
|
|
resolver := &mockAnyResolver{}
|
|
|
|
|
|
|
|
|
|
// Test with path variables and ignoreUnknownFields = true
|
|
|
|
|
req := httptest.NewRequest("GET", "/", http.NoBody)
|
|
|
|
|
req = pathvar.WithVars(req, map[string]string{"a": "b"})
|
|
|
|
|
parser, err := NewRequestParser(req, resolver, true)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.NotNil(t, parser)
|
|
|
|
|
|
|
|
|
|
// Test with path variables and ignoreUnknownFields = false
|
|
|
|
|
req2 := httptest.NewRequest("GET", "/", http.NoBody)
|
|
|
|
|
req2 = pathvar.WithVars(req2, map[string]string{"c": "d"})
|
|
|
|
|
parser2, err2 := NewRequestParser(req2, resolver, false)
|
|
|
|
|
assert.Nil(t, err2)
|
|
|
|
|
assert.NotNil(t, parser2)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNewRequestParserWithBodyAndIgnoreUnknownFields(t *testing.T) {
|
|
|
|
|
resolver := &mockAnyResolver{}
|
|
|
|
|
|
|
|
|
|
// Test with body and ignoreUnknownFields = true
|
|
|
|
|
req := httptest.NewRequest("POST", "/", strings.NewReader(`{"a": "b"}`))
|
|
|
|
|
parser, err := NewRequestParser(req, resolver, true)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.NotNil(t, parser)
|
|
|
|
|
|
|
|
|
|
// Test with body and ignoreUnknownFields = false
|
|
|
|
|
req2 := httptest.NewRequest("POST", "/", strings.NewReader(`{"c": "d"}`))
|
|
|
|
|
parser2, err2 := NewRequestParser(req2, resolver, false)
|
|
|
|
|
assert.Nil(t, err2)
|
|
|
|
|
assert.NotNil(t, parser2)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNewRequestParserWithVarsBodyAndIgnoreUnknownFields(t *testing.T) {
|
|
|
|
|
resolver := &mockAnyResolver{}
|
|
|
|
|
|
|
|
|
|
// Test with both path variables and body, ignoreUnknownFields = true
|
|
|
|
|
req := httptest.NewRequest("POST", "/", strings.NewReader(`{"a": "b"}`))
|
|
|
|
|
req = pathvar.WithVars(req, map[string]string{"c": "d"})
|
|
|
|
|
parser, err := NewRequestParser(req, resolver, true)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.NotNil(t, parser)
|
|
|
|
|
|
|
|
|
|
// Test with both path variables and body, ignoreUnknownFields = false
|
|
|
|
|
req2 := httptest.NewRequest("POST", "/", strings.NewReader(`{"e": "f"}`))
|
|
|
|
|
req2 = pathvar.WithVars(req2, map[string]string{"g": "h"})
|
|
|
|
|
parser2, err2 := NewRequestParser(req2, resolver, false)
|
|
|
|
|
assert.Nil(t, err2)
|
|
|
|
|
assert.NotNil(t, parser2)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestBuildJsonRequestParserFromMapWithIgnoreUnknownFields(t *testing.T) {
|
|
|
|
|
resolver := &mockAnyResolver{}
|
|
|
|
|
|
|
|
|
|
// Test buildJsonRequestParserFromMap with ignoreUnknownFields = true
|
|
|
|
|
data := map[string]any{"key": "value"}
|
|
|
|
|
parser, err := buildJsonRequestParserFromMap(data, resolver, true)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.NotNil(t, parser)
|
|
|
|
|
|
|
|
|
|
// Test buildJsonRequestParserFromMap with ignoreUnknownFields = false
|
|
|
|
|
parser2, err2 := buildJsonRequestParserFromMap(data, resolver, false)
|
|
|
|
|
assert.Nil(t, err2)
|
|
|
|
|
assert.NotNil(t, parser2)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestBuildJsonRequestParserWithUnknownFields(t *testing.T) {
|
|
|
|
|
resolver := &mockAnyResolver{}
|
|
|
|
|
|
|
|
|
|
// Test buildJsonRequestParserWithUnknownFields
|
|
|
|
|
data := strings.NewReader(`{"test": "value"}`)
|
|
|
|
|
parser, err := buildJsonRequestParserWithUnknownFields(data, resolver)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.NotNil(t, parser)
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 20:39:46 +08:00
|
|
|
type badBody struct{}
|
|
|
|
|
|
|
|
|
|
func (badBody) Read([]byte) (int, error) { return 0, errors.New("something bad") }
|
|
|
|
|
func (badBody) Close() error { return nil }
|