1
1
package oauth2
2
2
3
3
import (
4
+ "errors"
5
+ "io"
4
6
"net/http"
5
7
"net/http/httptest"
6
8
"testing"
@@ -27,6 +29,64 @@ func TestTransportNilTokenSource(t *testing.T) {
27
29
}
28
30
}
29
31
32
+ type readCloseCounter struct {
33
+ CloseCount int
34
+ ReadErr error
35
+ }
36
+
37
+ func (r * readCloseCounter ) Read (b []byte ) (int , error ) {
38
+ return 0 , r .ReadErr
39
+ }
40
+
41
+ func (r * readCloseCounter ) Close () error {
42
+ r .CloseCount ++
43
+ return nil
44
+ }
45
+
46
+ func TestTransportCloseRequestBody (t * testing.T ) {
47
+ tr := & Transport {}
48
+ server := newMockServer (func (w http.ResponseWriter , r * http.Request ) {})
49
+ defer server .Close ()
50
+ client := & http.Client {Transport : tr }
51
+ body := & readCloseCounter {
52
+ ReadErr : errors .New ("readCloseCounter.Read not implemented" ),
53
+ }
54
+ resp , err := client .Post (server .URL , "application/json" , body )
55
+ if err == nil {
56
+ t .Errorf ("got no errors, want an error with nil token source" )
57
+ }
58
+ if resp != nil {
59
+ t .Errorf ("Response = %v; want nil" , resp )
60
+ }
61
+ if expected := 1 ; body .CloseCount != expected {
62
+ t .Errorf ("Body was closed %d times, expected %d" , body .CloseCount , expected )
63
+ }
64
+ }
65
+
66
+ func TestTransportCloseRequestBodySuccess (t * testing.T ) {
67
+ tr := & Transport {
68
+ Source : StaticTokenSource (& Token {
69
+ AccessToken : "abc" ,
70
+ }),
71
+ }
72
+ server := newMockServer (func (w http.ResponseWriter , r * http.Request ) {})
73
+ defer server .Close ()
74
+ client := & http.Client {Transport : tr }
75
+ body := & readCloseCounter {
76
+ ReadErr : io .EOF ,
77
+ }
78
+ resp , err := client .Post (server .URL , "application/json" , body )
79
+ if err != nil {
80
+ t .Errorf ("got error %v; expected none" , err )
81
+ }
82
+ if resp == nil {
83
+ t .Errorf ("Response is nil; expected non-nil" )
84
+ }
85
+ if expected := 1 ; body .CloseCount != expected {
86
+ t .Errorf ("Body was closed %d times, expected %d" , body .CloseCount , expected )
87
+ }
88
+ }
89
+
30
90
func TestTransportTokenSource (t * testing.T ) {
31
91
ts := & tokenSource {
32
92
token : & Token {
0 commit comments