func init() {
Describe("actionDispatcher", func() {
var (
logger *fakes.FakeLogger
taskService *faketask.FakeService
taskManager *faketask.FakeManager
actionFactory *fakeaction.FakeFactory
actionRunner *fakeaction.FakeRunner
dispatcher ActionDispatcher
)
BeforeEach(func() {
logger = &fakes.FakeLogger{}
taskService = faketask.NewFakeService()
taskManager = faketask.NewFakeManager()
actionFactory = fakeaction.NewFakeFactory()
actionRunner = &fakeaction.FakeRunner{}
dispatcher = NewActionDispatcher(logger, taskService, taskManager, actionFactory, actionRunner)
})
It("responds with exception when the method is unknown", func() {
actionFactory.RegisterActionErr("fake-action", errors.New("fake-create-error"))
req := boshhandler.NewRequest("fake-reply", "fake-action", []byte{})
resp := dispatcher.Dispatch(req)
boshassert.MatchesJSONString(GinkgoT(), resp, `{"exception":{"message":"unknown message fake-action"}}`)
})
Context("Action Payload Logging", func() {
var (
action *fakeaction.TestAction
req boshhandler.Request
)
Context("action is loggable", func() {
BeforeEach(func() {
req = boshhandler.NewRequest("fake-reply", "fake-action", []byte("fake-payload"))
action = &fakeaction.TestAction{Loggable: true}
actionFactory.RegisterAction("fake-action", action)
dispatcher.Dispatch(req)
})
It("logs the payload", func() {
Expect(logger.DebugWithDetailsCallCount()).To(Equal(1))
_, message, args := logger.DebugWithDetailsArgsForCall(0)
Expect(message).To(Equal("Payload"))
Expect(args[0]).To(Equal(req.Payload))
})
})
Context("action is not loggable", func() {
BeforeEach(func() {
req = boshhandler.NewRequest("fake-reply", "fake-action", []byte("fake-payload"))
action = &fakeaction.TestAction{Loggable: false}
actionFactory.RegisterAction("fake-action", action)
dispatcher.Dispatch(req)
})
It("does not log the payload", func() {
Expect(logger.DebugWithDetailsCallCount()).To(Equal(0))
})
})
})
Context("when action is synchronous", func() {
var (
req boshhandler.Request
)
BeforeEach(func() {
req = boshhandler.NewRequest("fake-reply", "fake-action", []byte("fake-payload"))
actionFactory.RegisterAction("fake-action", &fakeaction.TestAction{Asynchronous: false})
})
It("handles synchronous action", func() {
actionRunner.RunValue = "fake-value"
resp := dispatcher.Dispatch(req)
Expect(req.GetPayload()).To(Equal(actionRunner.RunPayload))
Expect(boshhandler.NewValueResponse("fake-value")).To(Equal(resp))
})
It("handles synchronous action when err", func() {
actionRunner.RunErr = errors.New("fake-run-error")
resp := dispatcher.Dispatch(req)
expectedJSON := fmt.Sprintf("{\"exception\":{\"message\":\"Action Failed %s: fake-run-error\"}}", req.Method)
boshassert.MatchesJSONString(GinkgoT(), resp, expectedJSON)
})
})
Context("when action is asynchronous", func() {
var (
req boshhandler.Request
action *fakeaction.TestAction
)
BeforeEach(func() {
req = boshhandler.NewRequest("fake-reply", "fake-action", []byte("fake-payload"))
action = &fakeaction.TestAction{Asynchronous: true}
//.........这里部分代码省略.........
请发表评论