2021-03-19 14:17:53 +03:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2021-07-30 23:02:50 +03:00
|
|
|
func TestParsing(t *testing.T) {
|
|
|
|
type Message struct {
|
|
|
|
IIN string `protobuf:"bytes,1,opt,name=iin,proto3" json:"iin"`
|
|
|
|
}
|
|
|
|
|
|
|
|
omsg := &Message{IIN: "5555"}
|
|
|
|
|
|
|
|
for _, m := range []string{"POST"} {
|
|
|
|
body := ""
|
2021-10-25 19:59:37 +03:00
|
|
|
path, nmsg, err := newPathRequest("/users/iin/{iin}/push-notifications", m, body, omsg, []string{"protobuf", "json"}, nil)
|
2021-07-30 23:02:50 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
u, err := url.Parse(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_ = nmsg
|
|
|
|
if u.Path != "/users/iin/5555/push-notifications" {
|
|
|
|
t.Fatalf("newPathRequest invalid path %s", u.Path)
|
|
|
|
}
|
|
|
|
if nmsg != nil {
|
|
|
|
t.Fatalf("new message must be nil: %v\n", nmsg)
|
|
|
|
}
|
2021-03-23 00:29:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-19 14:17:53 +03:00
|
|
|
func TestNewPathRequest(t *testing.T) {
|
|
|
|
type Message struct {
|
2021-03-23 00:29:27 +03:00
|
|
|
Name string `json:"name"`
|
|
|
|
Val1 string `protobuf:"bytes,1,opt,name=val1,proto3" json:"val1"`
|
2021-03-19 14:17:53 +03:00
|
|
|
Val3 []string
|
2021-04-25 16:26:36 +03:00
|
|
|
Val2 int64
|
2021-03-19 14:17:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
omsg := &Message{Name: "test_name", Val1: "test_val1", Val2: 100, Val3: []string{"slice"}}
|
|
|
|
|
|
|
|
for _, m := range []string{"POST", "PUT", "PATCH", "GET", "DELETE"} {
|
2021-03-23 00:29:27 +03:00
|
|
|
body := ""
|
2021-10-25 19:59:37 +03:00
|
|
|
path, nmsg, err := newPathRequest("/v1/test", m, body, omsg, []string{"protobuf", "json"}, nil)
|
2021-03-19 14:17:53 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
u, err := url.Parse(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
vals := u.Query()
|
|
|
|
if v, ok := vals["name"]; !ok || v[0] != "test_name" {
|
2021-03-23 00:29:27 +03:00
|
|
|
t.Fatalf("invalid path: %v nmsg: %v", path, nmsg)
|
2024-03-26 11:09:38 +05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewPathRequestWithEmptyBody(t *testing.T) {
|
|
|
|
val := struct{}{}
|
2024-03-26 12:44:45 +05:00
|
|
|
cases := []string{
|
|
|
|
"",
|
|
|
|
"*",
|
|
|
|
"{}",
|
|
|
|
"nil",
|
|
|
|
`{"type": "invalid"}`,
|
|
|
|
}
|
2024-03-26 11:09:38 +05:00
|
|
|
|
2024-03-26 12:44:45 +05:00
|
|
|
for _, body := range cases {
|
|
|
|
for _, m := range []string{"POST", "PUT", "PATCH", "GET", "DELETE"} {
|
|
|
|
path, nmsg, err := newPathRequest("/v1/test", m, body, val, []string{"protobuf", "json"}, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if nmsg == nil {
|
|
|
|
t.Fatalf("invalid path: nil nmsg")
|
|
|
|
}
|
2024-03-26 11:09:38 +05:00
|
|
|
|
2024-03-26 12:44:45 +05:00
|
|
|
u, err := url.Parse(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
vals := u.Query()
|
|
|
|
if len(vals) != 0 {
|
|
|
|
t.Fatalf("invalid path: %v nmsg: %v", path, nmsg)
|
|
|
|
}
|
2021-03-23 00:29:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewPathVarRequest(t *testing.T) {
|
|
|
|
type Message struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Val1 string `protobuf:"bytes,1,opt,name=val1,proto3" json:"val1"`
|
|
|
|
Val3 []string
|
2021-04-25 16:26:36 +03:00
|
|
|
Val2 int64
|
2021-03-23 00:29:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
omsg := &Message{Name: "test_name", Val1: "test_val1", Val2: 100, Val3: []string{"slice"}}
|
|
|
|
|
|
|
|
for _, m := range []string{"POST", "PUT", "PATCH", "GET", "DELETE"} {
|
|
|
|
body := ""
|
|
|
|
if m != "GET" {
|
|
|
|
body = "*"
|
|
|
|
}
|
2021-10-25 19:59:37 +03:00
|
|
|
path, nmsg, err := newPathRequest("/v1/test/{val1}", m, body, omsg, []string{"protobuf", "json"}, nil)
|
2021-03-23 00:29:27 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
u, err := url.Parse(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if m != "GET" {
|
|
|
|
if _, ok := nmsg.(*Message); !ok {
|
|
|
|
t.Fatalf("invalid nmsg: %#+v\n", nmsg)
|
|
|
|
}
|
|
|
|
if nmsg.(*Message).Name != "test_name" {
|
|
|
|
t.Fatalf("invalid nmsg: %v nmsg: %v", path, nmsg)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
vals := u.Query()
|
|
|
|
if v, ok := vals["val2"]; !ok || v[0] != "100" {
|
|
|
|
t.Fatalf("invalid path: %v nmsg: %v", path, nmsg)
|
|
|
|
}
|
2021-03-19 14:17:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|