Request creation simplified and basic auth test added.

This commit is contained in:
Musab Gültekin 2019-06-17 13:53:34 +03:00
parent a5ec28664d
commit 4177f10de9
4 changed files with 27 additions and 6 deletions

View File

@ -12,6 +12,7 @@ import (
"log"
"math/rand"
"net/http"
"net/http/cookiejar"
"os"
"runtime/debug"
"sync"
@ -49,7 +50,7 @@ func init() {
// If options provided, options
func NewGeziyor(opt *Options) *Geziyor {
geziyor := &Geziyor{
Client: internal.NewClient(opt.CookiesDisabled),
Client: internal.NewClient(),
Opt: opt,
Exports: make(chan interface{}),
requestMiddlewares: []RequestMiddleware{
@ -75,6 +76,9 @@ func NewGeziyor(opt *Options) *Geziyor {
if opt.Timeout != 0 {
geziyor.Client.Timeout = opt.Timeout
}
if !opt.CookiesDisabled {
geziyor.Client.Jar, _ = cookiejar.New(nil)
}
if opt.ConcurrentRequests != 0 {
geziyor.semGlobal = make(chan struct{}, opt.ConcurrentRequests)
}

View File

@ -146,3 +146,13 @@ func TestCookies(t *testing.T) {
CookiesDisabled: true,
}).Start()
}
func TestBasicAuth(t *testing.T) {
geziyor.NewGeziyor(&geziyor.Options{
StartRequestsFunc: func(g *geziyor.Geziyor) {
req, _ := geziyor.NewRequest("GET", "https://httpbin.org/anything", nil)
req.SetBasicAuth("username", "password")
g.Do(req, nil)
},
}).Start()
}

View File

@ -4,7 +4,6 @@ import (
"errors"
"net"
"net/http"
"net/http/cookiejar"
"net/url"
"time"
)
@ -20,7 +19,7 @@ type Client struct {
}
// NewClient creates http.Client with modified values for typical web scraper
func NewClient(cookiesDisabled bool) *Client {
func NewClient() *Client {
client := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
@ -37,9 +36,6 @@ func NewClient(cookiesDisabled bool) *Client {
},
Timeout: time.Second * 180, // Google's timeout
}
if !cookiesDisabled {
client.Jar, _ = cookiejar.New(nil)
}
return &Client{Client: client}
}

View File

@ -1,6 +1,7 @@
package geziyor
import (
"io"
"net/http"
)
@ -11,3 +12,13 @@ type Request struct {
Rendered bool
Cancelled bool
}
// NewRequest returns a new Request given a method, URL, and optional body.
func NewRequest(method, url string, body io.Reader) (*Request, error) {
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
return &Request{Request: req}, nil
}