transport_test: added TestNilTokenSource + close res.Body per GET

+ TestTransportNilTokenSource added.
+ res.Body.Close() in tests to cut leaks
and also to increase test coverage for *onEOFReader.

Change-Id: I1b962b7ecbc4f6f8b5bcccc6870c025e89ad9c1a
Reviewed-on: https://go-review.googlesource.com/18141
Reviewed-by: Burcu Dogan <jbd@google.com>
This commit is contained in:
Emmanuel Odeke 2015-12-29 21:02:54 -07:00 committed by Brad Fitzpatrick
parent 191c6170f6
commit 8a57ed94ff
1 changed files with 26 additions and 4 deletions

View File

@ -13,6 +13,20 @@ func (t *tokenSource) Token() (*Token, error) {
return t.token, nil return t.token, nil
} }
func TestTransportNilTokenSource(t *testing.T) {
tr := &Transport{}
server := newMockServer(func(w http.ResponseWriter, r *http.Request) {})
defer server.Close()
client := &http.Client{Transport: tr}
res, err := client.Get(server.URL)
if err == nil {
t.Errorf("a nil Source was passed into the transport expected an error")
}
if res != nil {
t.Errorf("expected a nil response, got %v", res)
}
}
func TestTransportTokenSource(t *testing.T) { func TestTransportTokenSource(t *testing.T) {
ts := &tokenSource{ ts := &tokenSource{
token: &Token{ token: &Token{
@ -28,8 +42,12 @@ func TestTransportTokenSource(t *testing.T) {
} }
}) })
defer server.Close() defer server.Close()
client := http.Client{Transport: tr} client := &http.Client{Transport: tr}
client.Get(server.URL) res, err := client.Get(server.URL)
if err != nil {
t.Fatal(err)
}
res.Body.Close()
} }
// Test for case-sensitive token types, per https://github.com/golang/oauth2/issues/113 // Test for case-sensitive token types, per https://github.com/golang/oauth2/issues/113
@ -60,8 +78,12 @@ func TestTransportTokenSourceTypes(t *testing.T) {
} }
}) })
defer server.Close() defer server.Close()
client := http.Client{Transport: tr} client := &http.Client{Transport: tr}
client.Get(server.URL) res, err := client.Get(server.URL)
if err != nil {
t.Fatal(err)
}
res.Body.Close()
} }
} }