dongmi9494 2014-09-25 10:04
浏览 37

使用CSRF测试发布方法

I am trying to write a test method for sign up on my revel application. Look at the following code

package tests

import "github.com/revel/revel"
import "github.com/PuerkitoBio/goquery"
import "bytes"
import "net/url"

//import "net/http"

var csrf string

type AccountTest struct {
    revel.TestSuite
}

func (self *AccountTest) Before() {
    //println("Set up")
}

func (self *AccountTest) TestGetSignUp() {
    self.Get("/signup")
    site := bytes.NewBuffer(self.ResponseBody)
    doc, _ := goquery.NewDocumentFromReader(site)
    doc.Find("input").Each(func(i int, s *goquery.Selection) {
        name, exists := s.Attr("name")
        if name == "csrf_token" && exists {
            csrf, _ = s.Attr("value")
        }
    })
    self.AssertOk()
    self.AssertContains("Sign Up")
    self.AssertContentType("text/html; charset=utf-8")
}

func (self *AccountTest) TestPostSignUp() {
    self.PostForm("/signup", url.Values{
        "name":         {"cormier"},
        "email":        {"cormisample.com"},
        "emailConfirm": {"cormier@sample.com"},
        "password":     {"Test!1234"},
        "termof":       {"true"},
        "csrf_token":   {csrf},
    })
    self.AssertOk()
    self.AssertContentType("text/html; charset=utf-8")
}

func (self *AccountTest) After() {
    //println("Tear down")
} 

The test does not pass by TestPostSignUp function, it seems like, that the request is rejected through csrf middleware that I implemented revel-csrf. As you can see above, I read csrf token and save into variable(csrf). By Postform request I passed the variable, but does not work.

My question is, how to make a test with post request that will pass csrf protection.

  • 写回答

1条回答 默认 最新

  • dpge74512 2014-09-25 11:36
    关注

    I solve the problem following:

    package tests
    
        import "github.com/revel/revel"
        import "github.com/PuerkitoBio/goquery"
        import "bytes"
        import "net/url"
    
        //import "net/http"
    
        var csrf string
    
        type AccountTest struct {
            revel.TestSuite
        }
    
        func (self *AccountTest) Before() {
    
            self.Get("/signup")
            site := bytes.NewBuffer(self.ResponseBody)
            doc, _ := goquery.NewDocumentFromReader(site)
            doc.Find("input").Each(func(i int, s *goquery.Selection) {
                name, exists := s.Attr("name")
                if name == "csrf_token" && exists {
                    csrf, _ = s.Attr("value")
                }
            })
    
        }
    
        func (self *AccountTest) TestSignUp() {
    
            self.PostForm("/signup", url.Values{
                "name":         {"cormier"},
                "email":        {"cormier@sample.com"},
                "emailConfirm": {"cormier@sample.com"},
                "password":     {"Test!1234"},
                "termof":       {"true"},
                "csrf_token":   {csrf},
            })
            self.AssertOk()
            self.AssertContentType("text/html; charset=utf-8")
        }
    
        func (self *AccountTest) After() {
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有人能看一下我宿舍管理系统的报修功能该怎么改啊?链表那里总是越界
  • ¥15 cs loadimage运行不了,easyx也下了,没有用
  • ¥15 r包runway详细安装教程
  • ¥15 Html中读取Json文件中数据并制作表格
  • ¥15 谁有RH342练习环境
  • ¥15 STM32F407 DMA中断问题
  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。
  • ¥20 关于变压器的具体案例分析