/
todo_test.go
125 lines (94 loc) · 3.45 KB
/
todo_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
package main
import (
"encoding/json"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/jingweno/conf"
"github.com/pengux/web"
. "github.com/smartystreets/goconvey/convey"
)
func TestTodo(t *testing.T) {
defaults := map[string]interface{}{
"ENV": "testing",
"API_PORT": 9090,
"API_PREFIX": "",
}
config, err := conf.NewLoader().Env().Argv().File("./config_test.json").Defaults(defaults).Load()
if err != nil {
log.Fatalln(err)
}
// Init DB service
dbService := &InMemoryDBService{}
dbService.Init()
rootRouter := web.NewWithPrefix(Context{}, config.Get("API_PREFIX").(string))
rootRouter.Subrouter(TodoContext{}, "/todos").
Get("/", (*TodoContext).ReadMany).
Get("/:id", (*TodoContext).Read).
Post("/", (*TodoContext).Create).
Put("/:id", (*TodoContext).Replace).
Delete("/:id", (*TodoContext).Delete).
Middleware((*TodoContext).Init)
Convey("Given the todos endpoint", t, func() {
var testTodo Todo
Convey("When adding a todo to the collection", func() {
recorder := httptest.NewRecorder()
request, _ := http.NewRequest("POST", config.Get("API_PREFIX").(string)+"/todos", strings.NewReader(`{
"title": "Master Go",
"done": false
}`))
rootRouter.ServeHTTP(recorder, request)
Convey("It should returns a 201 Created status", func() {
So(recorder.Code, ShouldEqual, 201)
})
})
Convey("When listing all todos from the collection", func() {
recorder := httptest.NewRecorder()
request, _ := http.NewRequest("GET", config.Get("API_PREFIX").(string)+"/todos", nil)
rootRouter.ServeHTTP(recorder, request)
Convey("It should contains the 'Testtodo' object(s)", func() {
var todos []Todo
json.Unmarshal(recorder.Body.Bytes(), &todos)
testTodo = todos[0]
So(testTodo.Title, ShouldEqual, "Master Go")
})
})
Convey("When updating the 'Testtodo'", func() {
testTodo.Done = true
data, _ := json.Marshal(testTodo)
recorder := httptest.NewRecorder()
request, _ := http.NewRequest("PUT", config.Get("API_PREFIX").(string)+"/todos"+"/"+testTodo.Id, strings.NewReader(string(data)))
rootRouter.ServeHTTP(recorder, request)
Convey("the data should persist", func() {
recorder := httptest.NewRecorder()
request, _ := http.NewRequest("GET", config.Get("API_PREFIX").(string)+"/todos"+"/"+testTodo.Id, nil)
rootRouter.ServeHTTP(recorder, request)
var testTodoUpdated Todo
json.Unmarshal(recorder.Body.Bytes(), &testTodoUpdated)
So(testTodoUpdated.Done, ShouldEqual, true)
})
})
Convey("When deleting the 'Testtodo'", func() {
recorder := httptest.NewRecorder()
request, _ := http.NewRequest("DELETE", config.Get("API_PREFIX").(string)+"/todos"+"/"+testTodo.Id, nil)
rootRouter.ServeHTTP(recorder, request)
Convey("it should returns 200 Status", func() {
So(recorder.Code, ShouldEqual, 200)
})
Convey("and deleting it again should return 404 Not Found", func() {
recorder := httptest.NewRecorder()
request, _ := http.NewRequest("DELETE", config.Get("API_PREFIX").(string)+"/todos/"+testTodo.Id, nil)
rootRouter.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 404)
})
Convey("and accessing the object should gives 404 Not Found", func() {
recorder := httptest.NewRecorder()
request, _ := http.NewRequest("GET", config.Get("API_PREFIX").(string)+"/todos/"+testTodo.Id, nil)
rootRouter.ServeHTTP(recorder, request)
So(recorder.Code, ShouldEqual, 404)
})
})
})
}