forked from cloudfoundry-attic/receptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task_handler_test.go
85 lines (66 loc) · 1.82 KB
/
task_handler_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
package task_handler_test
import (
"bytes"
"net/http"
"net/http/httptest"
"github.com/cloudfoundry-incubator/receptor/task_handler"
"github.com/cloudfoundry-incubator/runtime-schema/models"
"github.com/pivotal-golang/lager"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("TaskHandler", func() {
var (
enqueue chan models.Task
server *httptest.Server
payload []byte
response *http.Response
)
BeforeEach(func() {
logger := lager.NewLogger("task-watcher-test")
logger.RegisterSink(lager.NewWriterSink(GinkgoWriter, lager.INFO))
enqueue = make(chan models.Task, 100)
server = httptest.NewServer(task_handler.NewHandler(enqueue, logger))
})
AfterEach(func() {
server.Close()
})
JustBeforeEach(func() {
var err error
response, err = http.Post(server.URL, "application/json", bytes.NewBuffer(payload))
Ω(err).ShouldNot(HaveOccurred())
})
Describe("when the handler receives a task", func() {
task := models.Task{
TaskGuid: "some-guid",
Failed: true,
FailureReason: "'cause",
Result: "some result",
Stack: "java",
Domain: "some-domain",
Action: &models.RunAction{Path: "true"},
}
BeforeEach(func() {
var err error
payload, err = models.ToJSONArray(task)
Ω(err).ShouldNot(HaveOccurred())
})
It("enqueues the task on the worker", func() {
Ω(enqueue).Should(Receive(Equal(task)))
})
It("returns 202", func() {
Ω(response.StatusCode).Should(Equal(http.StatusAccepted))
})
})
Describe("when the handler receives a bogus payload", func() {
BeforeEach(func() {
payload = []byte("ß")
})
It("returns 400", func() {
Ω(response.StatusCode).Should(Equal(http.StatusBadRequest))
})
It("does not enqueue anything somehow", func() {
Ω(enqueue).ShouldNot(Receive())
})
})
})