open-vault/api/request_test.go
Hamid Ghaf 27bb03bbc0
adding copyright header (#19555)
* adding copyright header

* fix fmt and a test
2023-03-15 09:00:52 -07:00

45 lines
916 B
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
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))
if actual != expected {
t.Fatalf("bad: %s", actual)
}
}
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)
expected := `{"foo":"bar"}`
actual := strings.TrimSpace(string(buf))
if actual != expected {
t.Fatalf("bad: actual %s, expected %s", actual, expected)
}
}