2021-03-03 19:45:21 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestResponse(t *testing.T) {
|
|
|
|
e1 := errors.New("error one")
|
|
|
|
e2 := errors.New("error two")
|
2021-04-28 22:05:07 +00:00
|
|
|
r1 := Response{
|
2021-03-03 19:45:21 +00:00
|
|
|
body: []byte(`{"foo": "bar", "baz": 1}`),
|
|
|
|
}
|
2021-04-28 22:05:07 +00:00
|
|
|
r2 := Response{
|
2021-03-03 19:45:21 +00:00
|
|
|
status: "System notice: disabled for maintenance",
|
|
|
|
err: e1,
|
|
|
|
}
|
2021-04-28 22:05:07 +00:00
|
|
|
r3 := Response{
|
2021-03-03 19:45:21 +00:00
|
|
|
body: []byte(`{"oof": "RAB", "baz": 8}`),
|
|
|
|
status: "reversed polarity detected",
|
|
|
|
}
|
2021-04-28 22:05:07 +00:00
|
|
|
r4 := Response{
|
2021-03-03 19:45:21 +00:00
|
|
|
body: []byte(`{"whoops": true, "state": "total protonic reversal"}`),
|
|
|
|
status: "",
|
|
|
|
err: e2,
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("ok", func(t *testing.T) {
|
|
|
|
assert.True(t, r1.Ok())
|
|
|
|
assert.False(t, r2.Ok())
|
|
|
|
assert.True(t, r3.Ok())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("error", func(t *testing.T) {
|
|
|
|
assert.Equal(t,
|
|
|
|
"error one: \"System notice: disabled for maintenance\"",
|
|
|
|
r2.Error())
|
|
|
|
assert.Equal(t,
|
|
|
|
e2.Error(),
|
|
|
|
r4.Error())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("unwrap", func(t *testing.T) {
|
|
|
|
assert.Equal(t, e1, r2.Unwrap())
|
|
|
|
assert.Equal(t, e2, r4.Unwrap())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("status", func(t *testing.T) {
|
|
|
|
assert.Equal(t, "", r1.Status())
|
|
|
|
assert.Equal(t, "System notice: disabled for maintenance", r2.Status())
|
|
|
|
assert.Equal(t, "reversed polarity detected", r3.Status())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("end", func(t *testing.T) {
|
|
|
|
assert.Nil(t, r1.End())
|
|
|
|
assert.NotNil(t, r2.End())
|
|
|
|
assert.EqualValues(t, r2, r2.End())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("noStatus", func(t *testing.T) {
|
|
|
|
assert.EqualValues(t, r1, r1.NoStatus())
|
|
|
|
assert.EqualValues(t, r2, r2.NoStatus())
|
|
|
|
|
|
|
|
x := r3.NoStatus()
|
|
|
|
assert.EqualValues(t, ErrStatusNotEmpty, x.Unwrap())
|
|
|
|
assert.EqualValues(t, r3.Status(), x.Status())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("noBody", func(t *testing.T) {
|
2021-04-28 22:05:07 +00:00
|
|
|
x := r1.NoBody()
|
2021-03-03 19:45:21 +00:00
|
|
|
assert.EqualValues(t, ErrBodyNotEmpty, x.Unwrap())
|
|
|
|
assert.EqualValues(t, r1.Status(), x.Status())
|
|
|
|
|
2021-04-28 22:05:07 +00:00
|
|
|
assert.EqualValues(t, r2, r2.NoBody())
|
2021-03-03 19:45:21 +00:00
|
|
|
|
2021-04-28 22:05:07 +00:00
|
|
|
rtemp := Response{}
|
|
|
|
assert.EqualValues(t, rtemp, rtemp.NoBody())
|
2021-03-03 19:45:21 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("noData", func(t *testing.T) {
|
|
|
|
x := r1.NoData()
|
|
|
|
assert.EqualValues(t, ErrBodyNotEmpty, x.Unwrap())
|
|
|
|
assert.EqualValues(t, r1.Status(), x.Status())
|
|
|
|
|
|
|
|
x = r3.NoStatus()
|
|
|
|
assert.EqualValues(t, ErrStatusNotEmpty, x.Unwrap())
|
|
|
|
assert.EqualValues(t, r3.Status(), x.Status())
|
|
|
|
|
2021-04-28 22:05:07 +00:00
|
|
|
rtemp := Response{}
|
2021-03-03 19:45:21 +00:00
|
|
|
assert.EqualValues(t, rtemp, rtemp.NoData())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("filterDeprecated", func(t *testing.T) {
|
2021-04-28 22:05:07 +00:00
|
|
|
assert.EqualValues(t, r1, r1.FilterDeprecated())
|
|
|
|
assert.EqualValues(t, r2, r2.FilterDeprecated())
|
2021-03-03 19:45:21 +00:00
|
|
|
|
2021-04-28 22:05:07 +00:00
|
|
|
rtemp := Response{
|
2021-03-03 19:45:21 +00:00
|
|
|
status: "blorple call is deprecated and will be removed in a future release",
|
|
|
|
}
|
2021-04-28 22:05:07 +00:00
|
|
|
x := rtemp.FilterDeprecated()
|
2021-03-03 19:45:21 +00:00
|
|
|
assert.True(t, x.Ok())
|
|
|
|
assert.Nil(t, x.End())
|
|
|
|
assert.Equal(t, "", x.Status())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("unmarshal", func(t *testing.T) {
|
|
|
|
var v map[string]interface{}
|
|
|
|
assert.EqualValues(t, r1, r1.Unmarshal(&v))
|
|
|
|
assert.EqualValues(t, "bar", v["foo"])
|
|
|
|
|
|
|
|
assert.EqualValues(t, r2, r2.Unmarshal(&v))
|
|
|
|
|
2021-04-28 22:05:07 +00:00
|
|
|
rtemp := Response{body: []byte("foo!")}
|
2021-03-03 19:45:21 +00:00
|
|
|
x := rtemp.Unmarshal(&v)
|
|
|
|
assert.False(t, x.Ok())
|
|
|
|
assert.Contains(t, x.Error(), "invalid character")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("newResponse", func(t *testing.T) {
|
2021-04-28 22:05:07 +00:00
|
|
|
rtemp := NewResponse(nil, "x", e2)
|
2021-03-03 19:45:21 +00:00
|
|
|
assert.False(t, rtemp.Ok())
|
|
|
|
assert.Equal(t, "x", rtemp.Status())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("notImplemented", func(t *testing.T) {
|
2021-04-28 22:05:07 +00:00
|
|
|
rtemp := Response{
|
2021-03-03 19:45:21 +00:00
|
|
|
status: "No handler found for this function",
|
|
|
|
err: myCephError(-22),
|
|
|
|
}
|
|
|
|
if assert.False(t, rtemp.Ok()) {
|
|
|
|
err := rtemp.End()
|
|
|
|
assert.Error(t, err)
|
|
|
|
var n NotImplementedError
|
|
|
|
assert.True(t, errors.As(err, &n))
|
|
|
|
assert.Contains(t, err.Error(), "not implemented")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type myCephError int
|
|
|
|
|
|
|
|
func (myCephError) Error() string {
|
|
|
|
return "oops"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e myCephError) ErrorCode() int {
|
|
|
|
return int(e)
|
|
|
|
}
|