Skip to content

Commit 77f0e7b

Browse files
committed
Init sessions
Signed-off-by: Levi Gross <[email protected]> Rebased off master Signed-off-by: Levi Gross <[email protected]> Go fmt Signed-off-by: Levi Gross <[email protected]> Updated tests for sessions Signed-off-by: Levi Gross <[email protected]> Removed Async methods Signed-off-by: Levi Gross <[email protected]> Added the cookie jar and publicsuffix Signed-off-by: Levi Gross <[email protected]> Unbroke tests Signed-off-by: Levi Gross <[email protected]> Restored Code Coverage Signed-off-by: Levi Gross <[email protected]> Fixed broken test Signed-off-by: Levi Gross <[email protected]>
1 parent 88dc4d3 commit 77f0e7b

11 files changed

+844
-205
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Features
1212
========
1313

1414
- Asynchronous and synchronous functionality built in
15-
- Doesn't depend on external libraries (functionality is designed to complement `net/http`)
1615
- Works with every version of Go from 1.3
1716
- Responses can be serialized into JSON and XML
1817
- Easy file uploads

base.go

+7-63
Original file line numberDiff line numberDiff line change
@@ -6,109 +6,53 @@ package grequests // import "github.com/levigross/grequests"
66
// 2. A RequestOptions struct
77
// If you do not intend to use the `RequestOptions` you can just pass nil
88
func Get(url string, ro *RequestOptions) (*Response, error) {
9-
return doRequest("GET", url, ro)
10-
}
11-
12-
// GetAsync takes 2 parameters and returns a Response channel. These two options are:
13-
// 1. A URL
14-
// 2. A RequestOptions struct
15-
// If you do not intend to use the `RequestOptions` you can just pass nil
16-
func GetAsync(url string, ro *RequestOptions) chan *Response {
17-
return doAsyncRequest("GET", url, ro)
9+
return doRegularRequest("GET", url, ro)
1810
}
1911

2012
// Put takes 2 parameters and returns a Response struct. These two options are:
2113
// 1. A URL
2214
// 2. A RequestOptions struct
2315
// If you do not intend to use the `RequestOptions` you can just pass nil
2416
func Put(url string, ro *RequestOptions) (*Response, error) {
25-
return doRequest("PUT", url, ro)
26-
}
27-
28-
// PutAsync takes 2 parameters and returns a Response channel. These two options are:
29-
// 1. A URL
30-
// 2. A RequestOptions struct
31-
// If you do not intend to use the `RequestOptions` you can just pass nil
32-
func PutAsync(url string, ro *RequestOptions) chan *Response {
33-
return doAsyncRequest("PUT", url, ro)
17+
return doRegularRequest("PUT", url, ro)
3418
}
3519

3620
// Patch takes 2 parameters and returns a Response struct. These two options are:
3721
// 1. A URL
3822
// 2. A RequestOptions struct
3923
// If you do not intend to use the `RequestOptions` you can just pass nil
4024
func Patch(url string, ro *RequestOptions) (*Response, error) {
41-
return doRequest("PATCH", url, ro)
42-
}
43-
44-
// PatchAsync takes 2 parameters and returns a Response channel. These two options are:
45-
// 1. A URL
46-
// 2. A RequestOptions struct
47-
// If you do not intend to use the `RequestOptions` you can just pass nil
48-
func PatchAsync(url string, ro *RequestOptions) chan *Response {
49-
return doAsyncRequest("PATCH", url, ro)
25+
return doRegularRequest("PATCH", url, ro)
5026
}
5127

5228
// Delete takes 2 parameters and returns a Response struct. These two options are:
5329
// 1. A URL
5430
// 2. A RequestOptions struct
5531
// If you do not intend to use the `RequestOptions` you can just pass nil
5632
func Delete(url string, ro *RequestOptions) (*Response, error) {
57-
return doRequest("DELETE", url, ro)
58-
}
59-
60-
// DeleteAsync takes 2 parameters and returns a Response channel. These two options are:
61-
// 1. A URL
62-
// 2. A RequestOptions struct
63-
// If you do not intend to use the `RequestOptions` you can just pass nil
64-
func DeleteAsync(url string, ro *RequestOptions) chan *Response {
65-
return doAsyncRequest("DELETE", url, ro)
33+
return doRegularRequest("DELETE", url, ro)
6634
}
6735

6836
// Post takes 2 parameters and returns a Response channel. These two options are:
6937
// 1. A URL
7038
// 2. A RequestOptions struct
7139
// If you do not intend to use the `RequestOptions` you can just pass nil
7240
func Post(url string, ro *RequestOptions) (*Response, error) {
73-
return doRequest("POST", url, ro)
74-
}
75-
76-
// PostAsync takes 2 parameters and returns a Response channel. These two options are:
77-
// 1. A URL
78-
// 2. A RequestOptions struct
79-
// If you do not intend to use the `RequestOptions` you can just pass nil
80-
func PostAsync(url string, ro *RequestOptions) chan *Response {
81-
return doAsyncRequest("POST", url, ro)
41+
return doRegularRequest("POST", url, ro)
8242
}
8343

8444
// Head takes 2 parameters and returns a Response channel. These two options are:
8545
// 1. A URL
8646
// 2. A RequestOptions struct
8747
// If you do not intend to use the `RequestOptions` you can just pass nil
8848
func Head(url string, ro *RequestOptions) (*Response, error) {
89-
return doRequest("HEAD", url, ro)
90-
}
91-
92-
// HeadAsync takes 2 parameters and returns a Response struct. These two options are:
93-
// 1. A URL
94-
// 2. A RequestOptions struct
95-
// If you do not intend to use the `RequestOptions` you can just pass nil
96-
func HeadAsync(url string, ro *RequestOptions) chan *Response {
97-
return doAsyncRequest("HEAD", url, ro)
49+
return doRegularRequest("HEAD", url, ro)
9850
}
9951

10052
// Options takes 2 parameters and returns a Response struct. These two options are:
10153
// 1. A URL
10254
// 2. A RequestOptions struct
10355
// If you do not intend to use the `RequestOptions` you can just pass nil
10456
func Options(url string, ro *RequestOptions) (*Response, error) {
105-
return doRequest("OPTIONS", url, ro)
106-
}
107-
108-
// OptionsAsync takes 2 parameters and returns a Response channel. These two options are:
109-
// 1. A URL
110-
// 2. A RequestOptions struct
111-
// If you do not intend to use the `RequestOptions` you can just pass nil
112-
func OptionsAsync(url string, ro *RequestOptions) chan *Response {
113-
return doAsyncRequest("OPTIONS", url, ro)
57+
return doRegularRequest("OPTIONS", url, ro)
11458
}

base_delete_test.go

+74-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package grequests
22

33
import (
4+
"net/url"
45
"testing"
56
)
67

@@ -16,14 +17,83 @@ func TestBasicDeleteRequest(t *testing.T) {
1617
}
1718
}
1819

19-
func TestBasicAsyncDeleteRequest(t *testing.T) {
20-
resp := <-DeleteAsync("http://httpbin.org/delete", nil)
20+
func TestDeleteSession(t *testing.T) {
21+
session := NewSession(nil)
2122

22-
if resp.Error != nil {
23-
t.Error("Unable to make request", resp.Error)
23+
resp, err := session.Get("http://httpbin.org/cookies/set", &RequestOptions{Params: map[string]string{"one": "two"}})
24+
25+
if err != nil {
26+
t.Fatal("Cannot set cookie: ", err)
27+
}
28+
29+
if resp.Ok != true {
30+
t.Error("Request did not return OK")
31+
}
32+
33+
resp, err = session.Get("http://httpbin.org/cookies/set", &RequestOptions{Params: map[string]string{"two": "three"}})
34+
35+
if err != nil {
36+
t.Fatal("Cannot set cookie: ", err)
37+
}
38+
39+
if resp.Ok != true {
40+
t.Error("Request did not return OK")
41+
}
42+
43+
resp, err = session.Get("http://httpbin.org/cookies/set", &RequestOptions{Params: map[string]string{"three": "four"}})
44+
45+
if err != nil {
46+
t.Fatal("Cannot set cookie: ", err)
2447
}
2548

2649
if resp.Ok != true {
2750
t.Error("Request did not return OK")
2851
}
52+
53+
resp, err = session.Delete("http://httpbin.org/delete", nil)
54+
55+
if err != nil {
56+
t.Fatal("Cannot set cookie: ", err)
57+
}
58+
59+
if resp.Ok != true {
60+
t.Error("Request did not return OK")
61+
}
62+
63+
cookieURL, err := url.Parse("http://httpbin.org")
64+
if err != nil {
65+
t.Error("We (for some reason) cannot parse the cookie URL")
66+
}
67+
68+
if len(session.HTTPClient.Jar.Cookies(cookieURL)) != 3 {
69+
t.Error("Invalid number of cookies provided: ", resp.RawResponse.Cookies())
70+
}
71+
72+
for _, cookie := range session.HTTPClient.Jar.Cookies(cookieURL) {
73+
switch cookie.Name {
74+
case "one":
75+
if cookie.Value != "two" {
76+
t.Error("Cookie value is not valid", cookie)
77+
}
78+
case "two":
79+
if cookie.Value != "three" {
80+
t.Error("Cookie value is not valid", cookie)
81+
}
82+
case "three":
83+
if cookie.Value != "four" {
84+
t.Error("Cookie value is not valid", cookie)
85+
}
86+
default:
87+
t.Error("We should not have any other cookies: ", cookie)
88+
}
89+
}
90+
91+
}
92+
93+
func TestDeleteInvalidURLSession(t *testing.T) {
94+
session := NewSession(nil)
95+
96+
if _, err := session.Delete("%../dir/", nil); err == nil {
97+
t.Error("Some how the request was valid to make request ", err)
98+
}
2999
}

0 commit comments

Comments
 (0)