-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathproject_test.go
More file actions
119 lines (102 loc) · 2.96 KB
/
project_test.go
File metadata and controls
119 lines (102 loc) · 2.96 KB
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
package forecast_test
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/joefitzgerald/forecast"
. "github.com/onsi/gomega"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
)
func TestProject(t *testing.T) {
spec.Run(t, "Project", testProject, spec.Report(report.Terminal{}))
}
func testProject(t *testing.T, when spec.G, it spec.S) {
var (
server *httptest.Server
handler http.Handler
api *forecast.API
)
it.Before(func() {
RegisterTestingT(t)
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if handler != nil {
handler.ServeHTTP(w, r)
}
}))
api = forecast.New(server.URL, "test-token", "987654")
})
it.After(func() {
api = nil
if server == nil {
return
}
server.Close()
server = nil
})
when("Projects()", func() {
when("when a response is returned from the server", func() {
it.Before(func() {
handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response := ReadFile("projects.json")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "%s", response)
})
})
it("should return projects and a nil error", func() {
projects, err := api.Projects()
Expect(projects).ShouldNot(BeNil())
Expect(err).ShouldNot(HaveOccurred())
})
})
when("when an error is returned from the server", func() {
it.Before(func() {
handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response := "error"
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "%s", response)
})
})
it("should return an error", func() {
projects, err := api.Projects()
Expect(projects).Should(BeNil())
Expect(err).Should(HaveOccurred())
})
})
})
when("Project()", func() {
when("when a response is returned from the server", func() {
it.Before(func() {
handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response := ReadFile("project.json")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "%s", response)
})
})
it("should return projects and a nil error", func() {
projects, err := api.Project(111111)
Expect(projects).ShouldNot(BeNil())
Expect(err).ShouldNot(HaveOccurred())
})
})
when("when an error is returned from the server", func() {
it.Before(func() {
handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response := "error"
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "%s", response)
})
})
it("should return an error", func() {
projects, err := api.Project(111111)
Expect(projects).Should(BeNil())
Expect(err).Should(HaveOccurred())
})
})
})
}