...

Source file src/github.com/gorilla/sessions/cookie_test.go

Documentation: github.com/gorilla/sessions

     1  package sessions
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  // Test for creating new http.Cookie from name, value and options
     8  func TestNewCookieFromOptions(t *testing.T) {
     9  	tests := []struct {
    10  		name     string
    11  		value    string
    12  		path     string
    13  		domain   string
    14  		maxAge   int
    15  		secure   bool
    16  		httpOnly bool
    17  	}{
    18  		{"", "bar", "/foo/bar", "foo.example.com", 3600, true, true},
    19  		{"foo", "", "/foo/bar", "foo.example.com", 3600, true, true},
    20  		{"foo", "bar", "", "foo.example.com", 3600, true, true},
    21  		{"foo", "bar", "/foo/bar", "", 3600, true, true},
    22  		{"foo", "bar", "/foo/bar", "foo.example.com", 0, true, true},
    23  		{"foo", "bar", "/foo/bar", "foo.example.com", 3600, false, true},
    24  		{"foo", "bar", "/foo/bar", "foo.example.com", 3600, true, false},
    25  	}
    26  	for i, v := range tests {
    27  		options := &Options{
    28  			Path:     v.path,
    29  			Domain:   v.domain,
    30  			MaxAge:   v.maxAge,
    31  			Secure:   v.secure,
    32  			HttpOnly: v.httpOnly,
    33  		}
    34  		cookie := newCookieFromOptions(v.name, v.value, options)
    35  		if cookie.Name != v.name {
    36  			t.Fatalf("%v: bad cookie name: got %q, want %q", i+1, cookie.Name, v.name)
    37  		}
    38  		if cookie.Value != v.value {
    39  			t.Fatalf("%v: bad cookie value: got %q, want %q", i+1, cookie.Value, v.value)
    40  		}
    41  		if cookie.Path != v.path {
    42  			t.Fatalf("%v: bad cookie path: got %q, want %q", i+1, cookie.Path, v.path)
    43  		}
    44  		if cookie.Domain != v.domain {
    45  			t.Fatalf("%v: bad cookie domain: got %q, want %q", i+1, cookie.Domain, v.domain)
    46  		}
    47  		if cookie.MaxAge != v.maxAge {
    48  			t.Fatalf("%v: bad cookie maxAge: got %q, want %q", i+1, cookie.MaxAge, v.maxAge)
    49  		}
    50  		if cookie.Secure != v.secure {
    51  			t.Fatalf("%v: bad cookie secure: got %v, want %v", i+1, cookie.Secure, v.secure)
    52  		}
    53  		if cookie.HttpOnly != v.httpOnly {
    54  			t.Fatalf("%v: bad cookie httpOnly: got %v, want %v", i+1, cookie.HttpOnly, v.httpOnly)
    55  		}
    56  	}
    57  }
    58  

View as plain text