-
Notifications
You must be signed in to change notification settings - Fork 41
/
server_test.go
150 lines (145 loc) · 4 KB
/
server_test.go
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package canopus
import "testing"
// TODO Redo this entire test suite
func TestServerInstantiate(t *testing.T) {
//var s CoapServer
//s = NewServer()
//assert.NotNil(t, s)
//assert.Equal(t, 1000, s.GetLocalAddress().Port)
//assert.Equal(t, "udp", s.GetLocalAddress().Network())
//
//s = NewLocalServer("TestServer")
//assert.NotNil(t, s)
//assert.Equal(t, 5683, s.GetLocalAddress().Port)
//assert.Equal(t, "udp", s.GetLocalAddress().Network())
}
//func TestDiscoveryService(t *testing.T) {
// server := NewCoapServer(":5684")
// assert.NotNil(t, server)
// assert.Equal(t, 5684, server.localAddr.Port)
// assert.Equal(t, "udp", server.localAddr.Network())
//
// go server.Start()
// client := NewCoapClient()
// client.OnStart(func(server *CoapServer) {
// tok := "abc123"
// client.Dial("localhost:5684")
//
// req := NewRequest(TYPE_CONFIRMABLE, GET, GenerateMessageId())
// req.SetToken(tok)
// req.SetRequestURI(".well-known/core")
// resp, err := client.Send(req)
// assert.Nil(t, err)
//
// assert.Equal(t, tok, resp.GetMessage().GetTokenString())
// client.Stop()
// })
// client.Start()
//}
//func TestClientServerRequestResponse(t *testing.T) {
// server := NewLocalServer()
//
// server.Get("/ep", func (req CoapRequest) CoapResponse {
// msg := ContentMessage(req.GetMessage().MessageId, TYPE_ACKNOWLEDGEMENT)
// msg.SetStringPayload("ACK GET")
// res := NewResponse(msg, nil)
//
// return res
// })
//
// server.Post("/ep", func (req CoapRequest) CoapResponse {
// msg := ContentMessage(req.GetMessage().MessageId, TYPE_ACKNOWLEDGEMENT)
// msg.SetStringPayload("ACK POST")
// res := NewResponse(msg, nil)
//
// return res
// })
//
// server.Put("/ep", func (req CoapRequest) CoapResponse {
// msg := ContentMessage(req.GetMessage().MessageId, TYPE_ACKNOWLEDGEMENT)
// msg.SetStringPayload("ACK PUT")
// res := NewResponse(msg, nil)
//
// return res
// })
//
// server.Delete("/ep", func (req CoapRequest) CoapResponse {
// msg := ContentMessage(req.GetMessage().MessageId, TYPE_ACKNOWLEDGEMENT)
// msg.SetStringPayload("ACK DELETE")
// res := NewResponse(msg, nil)
//
// return res
// })
//
// go server.Start()
//
// client := NewCoapClient()
//
// client.OnStart(func(server *CoapServer) {
// client.Dial("localhost:5683")
// token := "tok1234"
//
// var req CoapRequest
// var resp CoapResponse
// var err error
//
// // 404 Test
// req = NewConfirmableGetRequest()
// req.SetToken(token)
// req.SetRequestURI("ep-404")
// resp, err = client.Send(req)
// assert.Equal(t, COAPCODE_404_NOT_FOUND, resp.GetMessage().Code)
//
// // GET
// req = NewConfirmableGetRequest()
// req.SetToken(token)
// req.SetRequestURI("ep")
// resp, err = client.Send(req)
//
// assert.Nil(t, err)
// assert.Equal(t, "ACK GET", resp.GetMessage().Payload.String())
// assert.Equal(t, token, resp.GetMessage().GetTokenString())
//
// // POST
// req = NewConfirmablePostRequest()
// req.SetToken(token)
// req.SetRequestURI("ep")
// resp, err = client.Send(req)
//
// assert.Nil(t, err)
// assert.Equal(t, "ACK POST", resp.GetMessage().Payload.String())
// assert.Equal(t, token, resp.GetMessage().GetTokenString())
//
// // PUT
// req = NewConfirmablePutRequest()
// req.SetToken(token)
// req.SetRequestURI("ep")
// resp, err = client.Send(req)
//
// assert.Nil(t, err)
// assert.Equal(t, "ACK PUT", resp.GetMessage().Payload.String())
// assert.Equal(t, token, resp.GetMessage().GetTokenString())
//
// // DELETE
// req = NewConfirmableDeleteRequest()
// req.SetToken(token)
// req.SetRequestURI("ep")
// resp, err = client.Send(req)
//
// assert.Nil(t, err)
// assert.Equal(t, "ACK DELETE", resp.GetMessage().Payload.String())
// assert.Equal(t, token, resp.GetMessage().GetTokenString())
//
// // Test default token set
// req = NewConfirmableGetRequest()
// req.SetRequestURI("ep")
// resp, err = client.Send(req)
//
// assert.Nil(t, err)
// assert.Equal(t, "ACK GET", resp.GetMessage().Payload.String())
// assert.NotEmpty(t, resp.GetMessage().GetTokenString())
//
// client.Stop()
// })
// client.Start()
//}