2015-03-09 18:38:50 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRequestSetJSONBody(t *testing.T) {
|
|
|
|
var r Request
|
|
|
|
raw := map[string]interface{}{"foo": "bar"}
|
|
|
|
if err := r.SetJSONBody(raw); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := `{"foo":"bar"}`
|
2018-06-01 13:12:43 +00:00
|
|
|
actual := strings.TrimSpace(string(r.BodyBytes))
|
2015-03-09 18:38:50 +00:00
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: %s", actual)
|
|
|
|
}
|
|
|
|
}
|
2015-04-20 17:44:51 +00:00
|
|
|
|
|
|
|
func TestRequestResetJSONBody(t *testing.T) {
|
|
|
|
var r Request
|
|
|
|
raw := map[string]interface{}{"foo": "bar"}
|
|
|
|
if err := r.SetJSONBody(raw); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := r.ResetJSONBody(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-06-01 13:12:43 +00:00
|
|
|
buf := make([]byte, len(r.BodyBytes))
|
|
|
|
copy(buf, r.BodyBytes)
|
2015-04-20 17:44:51 +00:00
|
|
|
|
|
|
|
expected := `{"foo":"bar"}`
|
2018-06-01 13:12:43 +00:00
|
|
|
actual := strings.TrimSpace(string(buf))
|
2015-04-20 17:44:51 +00:00
|
|
|
if actual != expected {
|
2018-06-01 13:12:43 +00:00
|
|
|
t.Fatalf("bad: actual %s, expected %s", actual, expected)
|
2015-04-20 17:44:51 +00:00
|
|
|
}
|
|
|
|
}
|