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

View File

@ -146,3 +146,13 @@ func TestCookies(t *testing.T) {
CookiesDisabled: true, CookiesDisabled: true,
}).Start() }).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" "errors"
"net" "net"
"net/http" "net/http"
"net/http/cookiejar"
"net/url" "net/url"
"time" "time"
) )
@ -20,7 +19,7 @@ type Client struct {
} }
// NewClient creates http.Client with modified values for typical web scraper // NewClient creates http.Client with modified values for typical web scraper
func NewClient(cookiesDisabled bool) *Client { func NewClient() *Client {
client := &http.Client{ client := &http.Client{
Transport: &http.Transport{ Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment, Proxy: http.ProxyFromEnvironment,
@ -37,9 +36,6 @@ func NewClient(cookiesDisabled bool) *Client {
}, },
Timeout: time.Second * 180, // Google's timeout Timeout: time.Second * 180, // Google's timeout
} }
if !cookiesDisabled {
client.Jar, _ = cookiejar.New(nil)
}
return &Client{Client: client} return &Client{Client: client}
} }

View File

@ -1,6 +1,7 @@
package geziyor package geziyor
import ( import (
"io"
"net/http" "net/http"
) )
@ -11,3 +12,13 @@ type Request struct {
Rendered bool Rendered bool
Cancelled 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
}