parent
60c88375ad
commit
ba2156133a
|
|
@ -26,6 +26,10 @@ type Broker interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
func do(r *http.Request) (*http.Response, error) {
|
func do(r *http.Request) (*http.Response, error) {
|
||||||
|
return _do(config.Get().DB(), r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func _do(db storage.DB, r *http.Request) (*http.Response, error) {
|
||||||
limiter.Wait(context.Background())
|
limiter.Wait(context.Background())
|
||||||
if strings.Contains(strings.ToLower(r.URL.Path), "login") {
|
if strings.Contains(strings.ToLower(r.URL.Path), "login") {
|
||||||
authlimiter.Wait(context.Background())
|
authlimiter.Wait(context.Background())
|
||||||
|
|
@ -39,8 +43,8 @@ func do(r *http.Request) (*http.Response, error) {
|
||||||
}
|
}
|
||||||
client.Jar = newjar
|
client.Jar = newjar
|
||||||
|
|
||||||
cookieJarKey := r.URL.Host
|
cookieJarKey := "cookies_" + r.URL.Host
|
||||||
cookies, err := getCookies(cookieJarKey)
|
cookies, err := getCookies(db, cookieJarKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logtr.Errorf("failed to load cookies: %v", err)
|
logtr.Errorf("failed to load cookies: %v", err)
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -50,17 +54,13 @@ func do(r *http.Request) (*http.Response, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err := setCookies(cookieJarKey, client.Jar.Cookies(r.URL)); err != nil {
|
if err := setCookies(db, cookieJarKey, client.Jar.Cookies(r.URL)); err != nil {
|
||||||
logtr.Errorf("failed to set cookies: %v", err)
|
logtr.Errorf("failed to set cookies: %v", err)
|
||||||
}
|
}
|
||||||
return resp, err
|
return resp, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCookies(host string) ([]*http.Cookie, error) {
|
func getCookies(db storage.DB, host string) ([]*http.Cookie, error) {
|
||||||
return _getCookies(config.Get().DB(), host)
|
|
||||||
}
|
|
||||||
|
|
||||||
func _getCookies(db storage.DB, host string) ([]*http.Cookie, error) {
|
|
||||||
b, err := db.Get(host)
|
b, err := db.Get(host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -70,11 +70,7 @@ func _getCookies(db storage.DB, host string) ([]*http.Cookie, error) {
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func setCookies(host string, cookies []*http.Cookie) error {
|
func setCookies(db storage.DB, host string, cookies []*http.Cookie) error {
|
||||||
return _setCookies(config.Get().DB(), host, cookies)
|
|
||||||
}
|
|
||||||
|
|
||||||
func _setCookies(db storage.DB, host string, cookies []*http.Cookie) error {
|
|
||||||
b, err := json.Marshal(cookies)
|
b, err := json.Marshal(cookies)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
|
|
@ -1,21 +1,59 @@
|
||||||
package broker
|
package broker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"local/storage"
|
"local/storage"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/time/rate"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDoCookies(t *testing.T) {
|
func TestDoCookies(t *testing.T) {
|
||||||
// func _setCookies(db storage.DB, host string, cookies []*http.Cookie) error {
|
limiter = rate.NewLimiter(rate.Limit(20.0), 1)
|
||||||
// func _getCookies(db storage.DB, host string) cookies, error {
|
calls := 0
|
||||||
db := storage.NewMap()
|
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)
|
t.Fatal(err)
|
||||||
} else if len(cookies) != 0 {
|
} else if len(cookies) != 0 {
|
||||||
t.Fatal(cookies)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue