blob: d02d27ecbccc2850698bc3291078630376c8efaf [file] [log] [blame]
package cookiefile
import (
"net/http"
"net/url"
"reflect"
"strings"
"testing"
"time"
"github.com/kylelemons/godebug/pretty"
)
func TestNewJar(t *testing.T) {
c := []*http.Cookie{
{
Name: "o",
Value: "git-bob.chromium.org=1/AAAAAAA",
Domain: "chromium.googlesource.com",
},
}
j, err := NewJar(c)
if err != nil {
t.Fatalf("NewJar returned error: %s", err)
}
u := url.URL{
Scheme: "https",
Host: "chromium.googlesource.com",
}
got := j.Cookies(&u)
want := []*http.Cookie{
{
Name: "o",
Value: "git-bob.chromium.org=1/AAAAAAA",
},
}
if diff := pretty.Compare(want, got); diff != "" {
t.Errorf("j.Cookies() differ -want +got, %s", diff)
}
}
func TestRead(t *testing.T) {
data := `# Netscape HTTP Cookie File
# https://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.
chromium.googlesource.com FALSE / TRUE 9876543210987 o git-bob.chromium.org=1/AAAAAAA`
r := strings.NewReader(data)
got, err := Read(r)
if err != nil {
t.Fatalf("Read() returned unexpected error %s", err)
}
want := []*http.Cookie{
{
Name: "o",
Value: "git-bob.chromium.org=1/AAAAAAA",
Domain: "chromium.googlesource.com",
Path: "/",
Expires: time.Unix(9876543210987, 0),
Secure: true,
},
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Read() = %#v; want %#v", got, want)
}
}