2021-01-19 04:12:31 +03:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2024-05-20 08:55:24 +03:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-02-06 19:05:55 +03:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2021-01-19 04:12:31 +03:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2024-05-20 08:55:24 +03:00
|
|
|
func TestNestedPathPost(t *testing.T) {
|
|
|
|
req := &request{Name: "first", Field1: "fieldval"}
|
|
|
|
p, m, err := newPathRequest("/api/v1/xxxx", "POST", "*", req, []string{"json", "protobuf"}, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
u, err := url.Parse(p)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if s := u.String(); s != "/api/v1/xxxx" {
|
|
|
|
t.Fatalf("nested path error %s", s)
|
|
|
|
}
|
|
|
|
_ = m
|
|
|
|
}
|
|
|
|
|
2024-05-02 14:40:12 +03:00
|
|
|
type request struct {
|
2024-05-20 08:55:24 +03:00
|
|
|
NestedTest *request `json:"nested_test,omitempty"`
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Field1 string `json:"field1,omitempty"`
|
|
|
|
ClientID string `json:",omitempty"`
|
|
|
|
Field2 string `json:",omitempty"`
|
|
|
|
Field3 int64 `json:",omitempty"`
|
2021-10-25 19:59:37 +03:00
|
|
|
}
|
|
|
|
|
2024-05-02 14:40:12 +03:00
|
|
|
func TestNestedPath(t *testing.T) {
|
2024-05-04 15:05:21 +03:00
|
|
|
req := &request{Name: "first", NestedTest: &request{Name: "second"}, Field1: "fieldval"}
|
|
|
|
p, m, err := newPathRequest("/api/v1/{name}/{nested_test.name}", "PUT", "*", req, []string{"json", "protobuf"}, nil)
|
2024-05-02 14:40:12 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
u, err := url.Parse(p)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-05-20 08:55:24 +03:00
|
|
|
if s := u.String(); s != "/api/v1/first/second" {
|
2024-05-04 13:41:59 +03:00
|
|
|
t.Fatalf("nested path error %s", s)
|
2024-05-02 14:40:12 +03:00
|
|
|
}
|
2024-05-20 08:55:24 +03:00
|
|
|
b, err := json.Marshal(m)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fmt.Printf("m %#+v %s\n", m, b)
|
2024-05-02 14:40:12 +03:00
|
|
|
}
|
|
|
|
|
2021-10-25 19:59:37 +03:00
|
|
|
func TestPathWithHeader(t *testing.T) {
|
2024-05-02 14:40:12 +03:00
|
|
|
req := &request{Name: "vtolstov", Field1: "field1", ClientID: "1234567890"}
|
2021-10-25 19:59:37 +03:00
|
|
|
p, m, err := newPathRequest(
|
|
|
|
"/api/v1/test?Name={name}&Field1={field1}",
|
|
|
|
"POST",
|
|
|
|
"*",
|
|
|
|
req,
|
|
|
|
nil,
|
|
|
|
map[string]map[string]string{"header": {"ClientID": "true"}},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
u, err := url.Parse(p)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if m != nil {
|
|
|
|
t.Fatal("new struct must be nil")
|
|
|
|
}
|
|
|
|
if u.Query().Get("Name") != "vtolstov" || u.Query().Get("Field1") != "field1" {
|
|
|
|
t.Fatalf("invalid values %v", u.Query())
|
|
|
|
}
|
2021-01-19 04:12:31 +03:00
|
|
|
}
|
|
|
|
|
2021-09-01 02:06:01 +03:00
|
|
|
func TestPathValues(t *testing.T) {
|
2024-05-02 14:40:12 +03:00
|
|
|
req := &request{Name: "vtolstov", Field1: "field1"}
|
2021-10-25 19:59:37 +03:00
|
|
|
p, m, err := newPathRequest("/api/v1/test?Name={name}&Field1={field1}", "POST", "*", req, nil, nil)
|
2021-09-01 02:06:01 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
u, err := url.Parse(p)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_ = m
|
|
|
|
if u.Query().Get("Name") != "vtolstov" || u.Query().Get("Field1") != "field1" {
|
|
|
|
t.Fatalf("invalid values %v", u.Query())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-19 04:12:31 +03:00
|
|
|
func TestValidPath(t *testing.T) {
|
2024-05-02 14:40:12 +03:00
|
|
|
req := &request{Name: "vtolstov", Field1: "field1", Field2: "field2", Field3: 10}
|
2021-10-25 19:59:37 +03:00
|
|
|
p, m, err := newPathRequest("/api/v1/{name}/list", "GET", "", req, nil, nil)
|
2021-01-19 04:12:31 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-02-06 19:05:55 +03:00
|
|
|
u, err := url.Parse(p)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_ = m
|
|
|
|
parts := strings.Split(u.RawQuery, "&")
|
|
|
|
if len(parts) != 3 {
|
|
|
|
t.Fatalf("invalid path: %v", parts)
|
|
|
|
}
|
2021-01-19 04:12:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidPath(t *testing.T) {
|
2024-05-02 14:40:12 +03:00
|
|
|
req := &request{Name: "vtolstov", Field1: "field1", Field2: "field2", Field3: 10}
|
2024-05-04 13:41:59 +03:00
|
|
|
s, _, err := newPathRequest("/api/v1/{xname}/list", "GET", "", req, nil, nil)
|
2021-01-19 04:12:31 +03:00
|
|
|
if err == nil {
|
2024-05-04 13:41:59 +03:00
|
|
|
t.Fatalf("path param must not be filled: %s", s)
|
2021-01-19 04:12:31 +03:00
|
|
|
}
|
|
|
|
}
|