Skip to content

Commit 3f92c0a

Browse files
authored
Removed the finalizer (due to it closing while reading) and added mor… (levigross#31)
* Removed the finalizer (due to it closing while reading) and added more tests Signed-off-by: Levi Gross <[email protected]> * Travis CI should do short tests Signed-off-by: Levi Gross <[email protected]> * Only test on production go version Signed-off-by: Levi Gross <[email protected]> * Fixed strip function and renabled tip (maybe this will work) Signed-off-by: Levi Gross <[email protected]> * Don't try and hit TIP the moving target Signed-off-by: Levi Gross <[email protected]>
1 parent a76c2fb commit 3f92c0a

File tree

4 files changed

+183
-12
lines changed

4 files changed

+183
-12
lines changed

.travis.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ go:
88
# - 1.4 This won't work with golint
99
- 1.5
1010
- 1.6
11-
- tip
11+
- 1.7
12+
# - tip
1213

1314
before_install:
1415
- go get -u -v github.com/axw/gocov/gocov
@@ -19,7 +20,7 @@ script:
1920
- diff -u <(echo -n) <(gofmt -s -d ./)
2021
- diff -u <(echo -n) <(go vet ./...)
2122
- diff -u <(echo -n) <(golint ./...)
22-
- go test -v -race -covermode=atomic -coverprofile=coverage.out
23+
- go test -short -v -race -covermode=atomic -coverprofile=coverage.out
2324

2425
after_success:
2526
- goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN

base_get_test.go

+171-2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,131 @@ type TestJSONCookies struct {
9494
} `json:"cookies"`
9595
}
9696

97+
type MassiveJSONBlob struct {
98+
Type string `json:"type"`
99+
Features []struct {
100+
Type string `json:"type"`
101+
Properties struct {
102+
MAPBLKLOT string `json:"MAPBLKLOT"`
103+
BLKLOT string `json:"BLKLOT"`
104+
BLOCKNUM string `json:"BLOCK_NUM"`
105+
LOTNUM string `json:"LOT_NUM"`
106+
FROMST string `json:"FROM_ST"`
107+
TOST string `json:"TO_ST"`
108+
STREET string `json:"STREET"`
109+
STTYPE interface{} `json:"ST_TYPE"`
110+
ODDEVEN string `json:"ODD_EVEN"`
111+
} `json:"properties"`
112+
Geometry struct {
113+
Type string `json:"type"`
114+
Coordinates []struct {
115+
Num0 []float64 `json:"0,omitempty"`
116+
Num1 []float64 `json:"1,omitempty"`
117+
Num2 []float64 `json:"2,omitempty"`
118+
Num3 []float64 `json:"3,omitempty"`
119+
Num4 []float64 `json:"4,omitempty"`
120+
Num5 []float64 `json:"5,omitempty"`
121+
Num6 []float64 `json:"6,omitempty"`
122+
Num7 []float64 `json:"7,omitempty"`
123+
Num8 []float64 `json:"8,omitempty"`
124+
Num9 []float64 `json:"9,omitempty"`
125+
Num10 []float64 `json:"10,omitempty"`
126+
} `json:"-"`
127+
} `json:"geometry"`
128+
} `json:"features"`
129+
}
130+
131+
type GithubSelfJSON struct {
132+
ID int `json:"id"`
133+
Name string `json:"name"`
134+
FullName string `json:"full_name"`
135+
Owner struct {
136+
Login string `json:"login"`
137+
ID int `json:"id"`
138+
AvatarURL string `json:"avatar_url"`
139+
GravatarID string `json:"gravatar_id"`
140+
URL string `json:"url"`
141+
HTMLURL string `json:"html_url"`
142+
FollowersURL string `json:"followers_url"`
143+
FollowingURL string `json:"following_url"`
144+
GistsURL string `json:"gists_url"`
145+
StarredURL string `json:"starred_url"`
146+
SubscriptionsURL string `json:"subscriptions_url"`
147+
OrganizationsURL string `json:"organizations_url"`
148+
ReposURL string `json:"repos_url"`
149+
EventsURL string `json:"events_url"`
150+
ReceivedEventsURL string `json:"received_events_url"`
151+
Type string `json:"type"`
152+
SiteAdmin bool `json:"site_admin"`
153+
} `json:"owner"`
154+
Private bool `json:"private"`
155+
HTMLURL string `json:"html_url"`
156+
Description string `json:"description"`
157+
Fork bool `json:"fork"`
158+
URL string `json:"url"`
159+
ForksURL string `json:"forks_url"`
160+
KeysURL string `json:"keys_url"`
161+
CollaboratorsURL string `json:"collaborators_url"`
162+
TeamsURL string `json:"teams_url"`
163+
HooksURL string `json:"hooks_url"`
164+
IssueEventsURL string `json:"issue_events_url"`
165+
EventsURL string `json:"events_url"`
166+
AssigneesURL string `json:"assignees_url"`
167+
BranchesURL string `json:"branches_url"`
168+
TagsURL string `json:"tags_url"`
169+
BlobsURL string `json:"blobs_url"`
170+
GitTagsURL string `json:"git_tags_url"`
171+
GitRefsURL string `json:"git_refs_url"`
172+
TreesURL string `json:"trees_url"`
173+
StatusesURL string `json:"statuses_url"`
174+
LanguagesURL string `json:"languages_url"`
175+
StargazersURL string `json:"stargazers_url"`
176+
ContributorsURL string `json:"contributors_url"`
177+
SubscribersURL string `json:"subscribers_url"`
178+
SubscriptionURL string `json:"subscription_url"`
179+
CommitsURL string `json:"commits_url"`
180+
GitCommitsURL string `json:"git_commits_url"`
181+
CommentsURL string `json:"comments_url"`
182+
IssueCommentURL string `json:"issue_comment_url"`
183+
ContentsURL string `json:"contents_url"`
184+
CompareURL string `json:"compare_url"`
185+
MergesURL string `json:"merges_url"`
186+
ArchiveURL string `json:"archive_url"`
187+
DownloadsURL string `json:"downloads_url"`
188+
IssuesURL string `json:"issues_url"`
189+
PullsURL string `json:"pulls_url"`
190+
MilestonesURL string `json:"milestones_url"`
191+
NotificationsURL string `json:"notifications_url"`
192+
LabelsURL string `json:"labels_url"`
193+
ReleasesURL string `json:"releases_url"`
194+
DeploymentsURL string `json:"deployments_url"`
195+
CreatedAt time.Time `json:"created_at"`
196+
UpdatedAt time.Time `json:"updated_at"`
197+
PushedAt time.Time `json:"pushed_at"`
198+
GitURL string `json:"git_url"`
199+
SSHURL string `json:"ssh_url"`
200+
CloneURL string `json:"clone_url"`
201+
SvnURL string `json:"svn_url"`
202+
Homepage string `json:"homepage"`
203+
Size int `json:"size"`
204+
StargazersCount int `json:"stargazers_count"`
205+
WatchersCount int `json:"watchers_count"`
206+
Language string `json:"language"`
207+
HasIssues bool `json:"has_issues"`
208+
HasDownloads bool `json:"has_downloads"`
209+
HasWiki bool `json:"has_wiki"`
210+
HasPages bool `json:"has_pages"`
211+
ForksCount int `json:"forks_count"`
212+
MirrorURL interface{} `json:"mirror_url"`
213+
OpenIssuesCount int `json:"open_issues_count"`
214+
Forks int `json:"forks"`
215+
OpenIssues int `json:"open_issues"`
216+
Watchers int `json:"watchers"`
217+
DefaultBranch string `json:"default_branch"`
218+
NetworkCount int `json:"network_count"`
219+
SubscribersCount int `json:"subscribers_count"`
220+
}
221+
97222
func TestGetNoOptions(t *testing.T) {
98223
resp, _ := Get("http://httpbin.org/get", nil)
99224
verifyOkResponse(resp, t)
@@ -950,6 +1075,39 @@ func TestGetRedirectSecretHeaderNoCopy(t *testing.T) {
9501075

9511076
}
9521077

1078+
func TestMassiveJSONFile(t *testing.T) {
1079+
if testing.Short() {
1080+
t.Skip("Skipping massive JSON file download because short was called")
1081+
}
1082+
resp, err := Get("https://raw.githubusercontent.com/levigross/sf-city-lots-json/master/citylots.json", nil)
1083+
if err != nil {
1084+
t.Error("Request to massive JSON blob failed", err)
1085+
}
1086+
1087+
myjson := &MassiveJSONBlob{}
1088+
1089+
if err := resp.JSON(myjson); err != nil {
1090+
t.Error("Unable to serialize massive JSON blob", err)
1091+
}
1092+
1093+
if myjson.Type != "FeatureCollection" {
1094+
t.Error("JSON did not properly serialize")
1095+
}
1096+
}
1097+
1098+
func TestGitHubSelfJSON(t *testing.T) {
1099+
resp, err := Get("https://api.github.com/repos/levigross/grequests", nil)
1100+
if err != nil {
1101+
t.Error("Request to reddit JSON blob failed", err)
1102+
}
1103+
1104+
myjson := &GithubSelfJSON{}
1105+
1106+
if err := resp.JSON(myjson); err != nil {
1107+
t.Error("Unable to serialize reddit JSON blob", err)
1108+
}
1109+
}
1110+
9531111
func TestUnlimitedRedirects(t *testing.T) {
9541112
srv := httptest.NewServer(http.DefaultServeMux)
9551113
http.HandleFunc("/bar", func(w http.ResponseWriter, req *http.Request) {
@@ -973,15 +1131,26 @@ func TestAuthStripOnRedirect(t *testing.T) {
9731131
srv := httptest.NewServer(http.DefaultServeMux)
9741132
http.HandleFunc("/test/", func(w http.ResponseWriter, req *http.Request) {
9751133
if req.Header.Get("Authorization") != "" {
976-
http.Error(w, "Found Auth:", http.StatusInternalServerError)
1134+
http.Error(w, "Found Auth: "+req.Header.Get("Authorization"), http.StatusInternalServerError)
9771135
return
9781136
}
1137+
1138+
if req.Header.Get("WWW-Authenticate") != "" {
1139+
http.Error(w, "Found Auth: "+req.Header.Get("WWW-Authenticate"), http.StatusInternalServerError)
1140+
return
1141+
}
1142+
1143+
if req.Header.Get("Proxy-Authorization") != "" {
1144+
http.Error(w, "Found Auth: "+req.Header.Get("Proxy-Authorization"), http.StatusInternalServerError)
1145+
return
1146+
}
1147+
9791148
io.WriteString(w, "OK")
9801149
})
9811150

9821151
resp, err := Get(srv.URL+"/test", &RequestOptions{
9831152
Auth: []string{"one ", "two"},
984-
Headers: map[string]string{"WWW-Authenticate": "foo"},
1153+
Headers: map[string]string{"WWW-Authenticate": "foo", "Proxy-Authorization": "bar"},
9851154
})
9861155

9871156
if err != nil {

response.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func buildResponse(resp *http.Response, err error) (*Response, error) {
4747
Header: resp.Header,
4848
internalByteBuffer: bytes.NewBuffer([]byte{}),
4949
}
50-
EnsureResponseFinalized(goodResp)
50+
// EnsureResponseFinalized(goodResp) This will come back in 1.0
5151
return goodResp, nil
5252
}
5353

utils.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ var (
4040
// wish to set this on a request by request basis, set it within the
4141
// `RequestOptions` structure
4242
SensitiveHTTPHeaders = map[string]struct{}{
43-
"WWW-Authenticate": {},
43+
"Www-Authenticate": {},
4444
"Authorization": {},
4545
"Proxy-Authorization": {},
4646
}
@@ -67,7 +67,7 @@ func addRedirectFunctionality(client *http.Client, ro *RequestOptions) {
6767

6868
for k, vv := range via[0].Header {
6969
// Is this a sensitive header?
70-
if _, found := ro.SensitiveHTTPHeaders[k]; found && !ro.RedirectLocationTrusted {
70+
if _, found := ro.SensitiveHTTPHeaders[k]; found && ro.RedirectLocationTrusted == false {
7171
continue
7272
}
7373

@@ -91,8 +91,9 @@ func EnsureTransporterFinalized(httpTransport *http.Transport) {
9191

9292
// EnsureResponseFinalized will ensure that when the Response is GCed
9393
// the request body is closed so we aren't leaking fds
94-
func EnsureResponseFinalized(httpResp *Response) {
95-
runtime.SetFinalizer(&httpResp, func(httpResponseInt **Response) {
96-
(*httpResponseInt).RawResponse.Body.Close()
97-
})
98-
}
94+
// func EnsureResponseFinalized(httpResp *Response) {
95+
// runtime.SetFinalizer(&httpResp, func(httpResponseInt **Response) {
96+
// (*httpResponseInt).RawResponse.Body.Close()
97+
// })
98+
// }
99+
// This will come back in 1.0

0 commit comments

Comments
 (0)