-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwol_rest_test.go
78 lines (59 loc) · 1.88 KB
/
wol_rest_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
package main
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
func Test_shouldFailOnMissingMAC(t *testing.T) {
test = t
server := NewServer()
response := httptest.NewRecorder()
request := newRequest("POST", "/wake")
server.ServeHTTP(response, request)
responseBody := bodyAsString(response)
assertThatInt(response.Code, isInt(http.StatusBadRequest))
assertThatString(responseBody, containsString("mac is missing"))
}
func Test_shouldFailOnUnparsableMAC(t *testing.T) {
test = t
server := NewServer()
response := httptest.NewRecorder()
request := newRequest("POST", "/wake?mac=XXX")
server.ServeHTTP(response, request)
responseBody := bodyAsString(response)
assertThatInt(response.Code, isInt(http.StatusBadRequest))
assertThatString(responseBody, containsString("failed to parse XXX as a MAC adress"))
}
func Test_shouldFailOnUnparsableIP(t *testing.T) {
test = t
server := NewServer()
response := httptest.NewRecorder()
request := newRequest("POST", "/wake?mac=12:34:56:78:9A:BC&broadcastIP=XXX")
server.ServeHTTP(response, request)
responseBody := bodyAsString(response)
assertThatInt(response.Code, isInt(http.StatusBadRequest))
assertThatString(responseBody, containsString("failed to parse XXX as a IPv4 adress"))
}
func Test_shouldSendMagicPacket(t *testing.T) {
test = t
server := NewServer()
response := httptest.NewRecorder()
request := newRequest("POST", "/wake?mac=12:34:56:78:9A:BC&broadcastIP=127.0.0.255")
server.ServeHTTP(response, request)
assertThatInt(response.Code, isInt(http.StatusNoContent))
}
func newRequest(method, url string) *http.Request {
request, error := http.NewRequest(method, url, nil)
if error != nil {
test.Fatal(error)
}
return request
}
func bodyAsString(response *httptest.ResponseRecorder) string {
body, error := ioutil.ReadAll(response.Body)
if error != nil {
test.Fatal(error)
}
return string(body)
}