cookie jar go
This commit is contained in:
@@ -1,21 +1,59 @@
|
||||
package broker
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"local/storage"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
func TestDoCookies(t *testing.T) {
|
||||
// func _setCookies(db storage.DB, host string, cookies []*http.Cookie) error {
|
||||
// func _getCookies(db storage.DB, host string) cookies, error {
|
||||
limiter = rate.NewLimiter(rate.Limit(20.0), 1)
|
||||
calls := 0
|
||||
db := storage.NewMap()
|
||||
host := "host"
|
||||
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
calls += 1
|
||||
if calls == 1 {
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: "name",
|
||||
Value: "value",
|
||||
Expires: time.Now().Add(time.Hour),
|
||||
})
|
||||
} else {
|
||||
if !strings.Contains(fmt.Sprint(r.Header["Cookie"]), "name=value") {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
t.Error("cookie not set on calls after first")
|
||||
}
|
||||
}
|
||||
}))
|
||||
defer s.Close()
|
||||
req, _ := http.NewRequest(http.MethodGet, s.URL, nil)
|
||||
|
||||
if cookies, err := _getCookies(db, host); err == nil {
|
||||
if cookies, err := getCookies(db, "cookies_"+req.URL.Host); err == nil {
|
||||
t.Fatal(err)
|
||||
} else if len(cookies) != 0 {
|
||||
t.Fatal(cookies)
|
||||
}
|
||||
|
||||
t.Fatal("not impl")
|
||||
for i := 0; i < 3; i++ {
|
||||
resp, err := _do(db, req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatal(resp.StatusCode)
|
||||
}
|
||||
resp.Body.Close()
|
||||
|
||||
if cookies, err := getCookies(db, "cookies_"+req.URL.Host); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if len(cookies) == 0 {
|
||||
t.Fatal(cookies)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user