~fnux/yggdrasil-go-coap

ref: e042d7baf5169b5c24e7261d57260a87506cb2db yggdrasil-go-coap/noresponsewriter_test.go -rw-r--r-- 2.6 KiB
e042d7ba — Jozef Kralik fix sending message when peerMaxMessageSize is not set 2 years ago
                                                                                
1
2
3
4
5
6
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package coap

import (
	"reflect"
	"testing"
)

func TestNoResponse2XXCodes(t *testing.T) {
	nr := newNoResponseWriter(ResponseWriter(&responseWriter{req: &Request{}}))
	codes := nr.decodeNoResponseOption(2)
	exp := resp2XXCodes
	if !reflect.DeepEqual(exp, codes) {
		t.Fatalf("Expected\n%#v\ngot\n%#v", exp, codes)
	}
}

func TestNoResponse4XXCodes(t *testing.T) {
	nr := newNoResponseWriter(ResponseWriter(&responseWriter{req: &Request{}}))
	codes := nr.decodeNoResponseOption(8)
	exp := resp4XXCodes
	if !reflect.DeepEqual(exp, codes) {
		t.Fatalf("Expected\n%#v\ngot\n%#v", exp, codes)
	}
}

func TestNoResponse5XXCodes(t *testing.T) {
	nr := newNoResponseWriter(ResponseWriter(&responseWriter{req: &Request{}}))
	codes := nr.decodeNoResponseOption(16)
	exp := resp5XXCodes
	if !reflect.DeepEqual(exp, codes) {
		t.Fatalf("Expected\n%#v\ngot\n%#v", exp, codes)
	}
}

func TestNoResponseCombinationXXCodes(t *testing.T) {
	nr := newNoResponseWriter(ResponseWriter(&responseWriter{req: &Request{}}))
	codes := nr.decodeNoResponseOption(18)
	exp := append(resp2XXCodes, resp5XXCodes...)
	if !reflect.DeepEqual(exp, codes) {
		t.Fatalf("Expected\n%#v\ngot\n%#v", exp, codes)
	}
}

func TestNoResponseAllCodes(t *testing.T) {
	nr := newNoResponseWriter(ResponseWriter(&responseWriter{req: &Request{}}))
	codes := nr.decodeNoResponseOption(0)
	exp := []COAPCode(nil)
	if !reflect.DeepEqual(exp, codes) {
		t.Fatalf("Expected\n%#v\ngot\n%#v", exp, codes)
	}
}

func testNoResponseHandler(t *testing.T, w ResponseWriter, r *Request) {
	msg := r.Client.NewMessage(MessageParams{
		Type:      Acknowledgement,
		Code:      NotFound,
		MessageID: r.Msg.MessageID(),
		Token:     r.Msg.Token(),
	})

	err := w.WriteMsg(msg)
	if err != nil {
		if err == ErrMessageNotInterested {
			t.Fatalf("server unable to write message: %v", err)
		}
		return
	}
}

func TestNoResponseBehaviour(t *testing.T) {
	// server creation
	s, addr, fin, err := RunLocalServerUDPWithHandler("udp", ":", false, BlockWiseSzx16, func(w ResponseWriter, r *Request) { testNoResponseHandler(t, w, r) })
	defer func() {
		s.Shutdown()
		<-fin
	}()
	if err != nil {
		t.Fatalf("Unexpected error '%v'", err)
	}

	// connect client
	c := Client{Net: "udp"}
	con, err := c.Dial(addr)
	if err != nil {
		t.Fatalf("Unexpected error '%v'", err)
	}

	// send client request
	req := &DgramMessage{
		MessageBase{
			typ:       NonConfirmable,
			code:      GET,
			messageID: 1234,
		}}

	// supressing 2XX code: example Content; No error when server sends 4XX response
	req.SetOption(NoResponse, 2)
	err = con.WriteMsg(req)
	if err != nil {
		t.Fatalf("client unable to write message: %v", err)
	}
}