-
Notifications
You must be signed in to change notification settings - Fork 8
/
api_test.go
115 lines (90 loc) · 2.34 KB
/
api_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
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/bmizerany/assert"
)
type FakeSphero struct {
R, G, B uint8
}
func (s *FakeSphero) Start() {}
func (s *FakeSphero) Stop() {}
func (s *FakeSphero) SetRGB(r, g, b uint8) {
s.R, s.G, s.B = r, g, b
}
var (
fakeSphero *FakeSphero
api Api
server *httptest.Server
)
func setupMartini() {
fakeSphero = &FakeSphero{}
api = &ApiMartini{fakeSphero}
server = httptest.NewServer(api.Handler())
}
func setupPlain() {
fakeSphero = &FakeSphero{}
api = &ApiPlain{fakeSphero}
server = httptest.NewServer(api.Handler())
}
func tearDown() {
server.Close()
}
func TestApiPlain_InvalidRgb(t *testing.T) {
setupPlain()
defer tearDown()
url := fmt.Sprintf("%s/rgb/invalid", server.URL)
req, _ := http.NewRequest("PUT", url, nil)
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal("err should be nil")
}
if resp.StatusCode != 404 {
t.Fatalf("status code should be 404 but it's %s", resp.StatusCode)
}
}
func TestApiPlain_ValidRgb(t *testing.T) {
setupPlain()
defer tearDown()
url := fmt.Sprintf("%s/rgb/255,255,255", server.URL)
req, _ := http.NewRequest("PUT", url, nil)
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal("err should be nil")
}
if resp.StatusCode != 201 {
t.Fatalf("status code should be 404 but it's %s", resp.StatusCode)
}
if fakeSphero.R != (uint8)(255) {
t.Fatalf("R should be 255 but it's %s", fakeSphero.R)
}
if fakeSphero.G != (uint8)(255) {
t.Fatalf("G should be 255 but it's %s", fakeSphero.G)
}
if fakeSphero.B != (uint8)(255) {
t.Fatalf("B should be 255 but it's %s", fakeSphero.B)
}
}
func TestApiMartini_InvalidRgb(t *testing.T) {
setupMartini()
defer tearDown()
url := fmt.Sprintf("%s/rgb/invalid", server.URL)
req, _ := http.NewRequest("PUT", url, nil)
resp, err := http.DefaultClient.Do(req)
assert.Equal(t, nil, err)
assert.Equal(t, 400, resp.StatusCode)
}
func TestApiMartini_ValidRgb(t *testing.T) {
setupMartini()
defer tearDown()
url := fmt.Sprintf("%s/rgb/255,255,255", server.URL)
req, _ := http.NewRequest("PUT", url, nil)
resp, err := http.DefaultClient.Do(req)
assert.Equal(t, nil, err)
assert.Equal(t, 201, resp.StatusCode)
assert.Equal(t, (uint8)(255), fakeSphero.R)
assert.Equal(t, (uint8)(255), fakeSphero.G)
assert.Equal(t, (uint8)(255), fakeSphero.B)
}