github.com/astaxie/beego@v1.12.3/context/context_test.go (about)

     1  // Copyright 2016 beego Author. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package context
    16  
    17  import (
    18  	"net/http"
    19  	"net/http/httptest"
    20  	"strings"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestXsrfReset_01(t *testing.T) {
    27  	r := &http.Request{}
    28  	c := NewContext()
    29  	c.Request = r
    30  	c.ResponseWriter = &Response{}
    31  	c.ResponseWriter.reset(httptest.NewRecorder())
    32  	c.Output.Reset(c)
    33  	c.Input.Reset(c)
    34  	c.XSRFToken("key", 16)
    35  	if c._xsrfToken == "" {
    36  		t.FailNow()
    37  	}
    38  	token := c._xsrfToken
    39  	c.Reset(&Response{ResponseWriter: httptest.NewRecorder()}, r)
    40  	if c._xsrfToken != "" {
    41  		t.FailNow()
    42  	}
    43  	c.XSRFToken("key", 16)
    44  	if c._xsrfToken == "" {
    45  		t.FailNow()
    46  	}
    47  	if token == c._xsrfToken {
    48  		t.FailNow()
    49  	}
    50  
    51  	ck := c.ResponseWriter.Header().Get("Set-Cookie")
    52  	assert.True(t, strings.Contains(ck, "Secure"))
    53  	assert.True(t, strings.Contains(ck, "HttpOnly"))
    54  }