mirror of
https://github.com/foomo/gotsrpc.git
synced 2025-10-16 12:35:35 +00:00
69 lines
1.3 KiB
Go
69 lines
1.3 KiB
Go
package gotsrpc
|
|
|
|
import (
|
|
"bytes"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestLoadArgsFail(t *testing.T) {
|
|
jsonBytes := []byte(`["a"", ["a", "b", "c"]]`)
|
|
|
|
foo := ""
|
|
bar := []string{}
|
|
args := []interface{}{&foo, &bar}
|
|
|
|
r := httptest.NewRequest("GET", "/", bytes.NewBuffer(jsonBytes))
|
|
|
|
err := LoadArgs(&args, nil, r)
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestLoadArgs(t *testing.T) {
|
|
jsonBytes := []byte(`["a", ["a", "b", "c"]]`)
|
|
foo := ""
|
|
bar := []string{}
|
|
args := []interface{}{&foo, &bar}
|
|
errLoad := loadArgs(&args, jsonBytes)
|
|
if errLoad != nil {
|
|
t.Fatal(errLoad)
|
|
}
|
|
if foo != "a" {
|
|
t.Fatal("foo should have been a")
|
|
}
|
|
if len(bar) != 3 {
|
|
t.Fatal("bar len wrong", len(bar), "!=", len(bar))
|
|
}
|
|
if bar[1] != "b" {
|
|
t.Fatal("bar[1] (", bar[1], ") != b")
|
|
}
|
|
}
|
|
|
|
func TestLoadInterfaceArgs(t *testing.T) {
|
|
jsonBytes := []byte(`["a", ["a", "b", "c"], 1.3]`)
|
|
var (
|
|
foo interface{}
|
|
bar []interface{}
|
|
floaty interface{}
|
|
)
|
|
args := []interface{}{&foo, &bar, &floaty}
|
|
errLoad := loadArgs(&args, jsonBytes)
|
|
if errLoad != nil {
|
|
t.Fatal(errLoad)
|
|
}
|
|
if foo != "a" {
|
|
t.Fatal("foo should have been a")
|
|
}
|
|
if len(bar) != 3 {
|
|
t.Fatal("bar len wrong", len(bar), "!=", len(bar))
|
|
}
|
|
if bar[1] != "b" {
|
|
t.Fatal("bar[1] (", bar[1], ") != b")
|
|
}
|
|
if floaty != 1.3 {
|
|
t.Fatal("floaty mismatch", floaty)
|
|
}
|
|
}
|