open-vault/api/request_test.go

45 lines
916 B
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
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"}`
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)
}
buf := make([]byte, len(r.BodyBytes))
copy(buf, r.BodyBytes)
2015-04-20 17:44:51 +00:00
expected := `{"foo":"bar"}`
actual := strings.TrimSpace(string(buf))
2015-04-20 17:44:51 +00:00
if actual != expected {
t.Fatalf("bad: actual %s, expected %s", actual, expected)
2015-04-20 17:44:51 +00:00
}
}