-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers_test.go
67 lines (61 loc) · 1.46 KB
/
handlers_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
package go_ws
import (
"github.com/jerome-laforge/go_ws/dao/test"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func init() {
Dao = &test.FakeRepo
}
func TestTodoCreate(t *testing.T) {
test.FakeRepo.Clear()
if len(test.FakeRepo.RepoGetTodos()) != 0 {
t.Fatalf("repository is not empty")
}
r, err := http.NewRequest("POST", "/todos", strings.NewReader(`{"name":"New Todo 0"}`))
if err != nil {
t.Fatal(err)
}
w := httptest.NewRecorder()
TodoCreate(w, r)
if w.Code != http.StatusCreated {
t.Fatalf("Bad status code [expected: %d] [actual: %d]", http.StatusCreated, w.Code)
}
if len(test.FakeRepo.RepoGetTodos()) != 1 {
t.Fatalf("repository length incositency")
}
}
func TestTodoCreateBad(t *testing.T) {
test.FakeRepo.Clear()
if len(test.FakeRepo.RepoGetTodos()) != 0 {
t.Fatalf("repository is not empty")
}
r, err := http.NewRequest("POST", "/todos", strings.NewReader(`{"name":"New Todo 0"`))
if err != nil {
t.Fatal(err)
}
w := httptest.NewRecorder()
TodoCreate(w, r)
if w.Code != 422 {
t.Fatalf("Bad status code [expected: %d] [actual: %d]", 422, w.Code)
}
if len(test.FakeRepo.RepoGetTodos()) != 1 {
t.Fatalf("repository length incositency")
}
}
func TestIndex(t *testing.T) {
test.FakeRepo.Clear()
w := httptest.NewRecorder()
Index(w, nil)
if w.Body.String() != "Welcome!\n" {
t.Fail()
}
}
func TestTodoIndex(_ *testing.T) {
test.FakeRepo.Clear()
test.FakeRepo.Init()
w := httptest.NewRecorder()
TodoIndex(w, nil)
}