response_test.go 1.21 KB
Newer Older
1 2 3 4 5
package http

import (
	"testing"

tavit ohanian's avatar
tavit ohanian committed
6
	cmds "gitlab.dms3.io/dms3/public/go-dms3-cmds"
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
)

type testResponseType struct {
	a int
	b int
}

type testDecoder struct {
	a *int
	b *int
}

func (td *testDecoder) Decode(value interface{}) error {
	me := value.(*cmds.MaybeError)
	o := me.Value.(*testResponseType)

	if td.a != nil {
		o.a = *td.a
	}

	if td.b != nil {
		o.b = *td.b
	}

	return nil
}

keks's avatar
keks committed
34
func TestNextDecodesIntoNewStruct(t *testing.T) {
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
	a1 := 1
	b1 := 2
	testCommand := &cmds.Command{
		Type: &testResponseType{},
	}
	decoder := &testDecoder{
		a: &a1,
		b: &b1,
	}
	r := &cmds.Request{
		Command: testCommand,
	}
	response := &Response{
		req: r,
		dec: decoder,
	}

keks's avatar
keks committed
52
	v, err := response.Next()
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
	if err != nil {
		t.Fatal("error decoding response", err)
	}

	tv := v.(*testResponseType)
	if tv.a != 1 {
		t.Errorf("tv.a is %#v, expected 1", tv.a)
	}
	if tv.b != 2 {
		t.Errorf("tv.b is %#v, expected 2", tv.b)
	}

	a2 := 3
	decoder.a = &a2
	decoder.b = nil

keks's avatar
keks committed
69
	v2, err := response.Next()
70 71 72 73 74
	if err != nil {
		t.Fatal("error decoding response", err)
	}

	tv2 := v2.(*testResponseType)
Alex Cruikshank's avatar
Alex Cruikshank committed
75
	if tv2.a != 3 {
76 77 78 79 80 81
		t.Errorf("tv2.a is %#v, expected 3", tv2.a)
	}
	if tv2.b != 0 {
		t.Errorf("tv.b is %#v, expected it to be reset to 0", tv2.b)
	}
}