我正在尝试将名为 event 的模拟域实例传递给控制器的 show()方法,但是 show()找不到有问题的事件因此返回null。
请注意,以下代码段仍在进行中。
def "trying to show an event containing malicous code"() {
given: "An event named with malicous code"
mockDomain(Event)
def event = Mock(Event)
event.title >> "<script type=\"text/javascript\">alert(\"XSS\");</script>"
event.id >> 1
// Do I have to actually create a full-blown event considering all
// constraints here?
when: "I try to show that event"
controller.params.id = 1
def result = controller.show()
then: "The resulting title will be encoded HTML"
result.eventInstance.title == event.title.encodeAsHTML()
}
这是控制器的show()方法的开头:
def show = {
def eventInstance = Event.get(params.id)
// The event exists
if (eventInstance) {
// some processing here
return [eventInstance: eventInstance, isSubscribed: sub ? true:false, sidebar: 'sidebar']
}
答案 0 :(得分:1)
尝试以下列方式模拟Event对象:
def event = new Event()
event.title = "<script type=\"text/javascript\">alert(\"XSS\");</script>"
event.id = 1 // optional
mockDomain Event, [event]
除非您在event
电话中添加mockDomain
的实例,否则您将无法使用get