~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/golang.org/x/oauth2/transport_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package oauth2
 
2
 
 
3
import (
 
4
        "net/http"
 
5
        "net/http/httptest"
 
6
        "testing"
 
7
        "time"
 
8
)
 
9
 
 
10
type tokenSource struct{ token *Token }
 
11
 
 
12
func (t *tokenSource) Token() (*Token, error) {
 
13
        return t.token, nil
 
14
}
 
15
 
 
16
func TestTransportTokenSource(t *testing.T) {
 
17
        ts := &tokenSource{
 
18
                token: &Token{
 
19
                        AccessToken: "abc",
 
20
                },
 
21
        }
 
22
        tr := &Transport{
 
23
                Source: ts,
 
24
        }
 
25
        server := newMockServer(func(w http.ResponseWriter, r *http.Request) {
 
26
                if r.Header.Get("Authorization") != "Bearer abc" {
 
27
                        t.Errorf("Transport doesn't set the Authorization header from the fetched token")
 
28
                }
 
29
        })
 
30
        defer server.Close()
 
31
        client := http.Client{Transport: tr}
 
32
        client.Get(server.URL)
 
33
}
 
34
 
 
35
func TestTokenValidNoAccessToken(t *testing.T) {
 
36
        token := &Token{}
 
37
        if token.Valid() {
 
38
                t.Errorf("Token should not be valid with no access token")
 
39
        }
 
40
}
 
41
 
 
42
func TestExpiredWithExpiry(t *testing.T) {
 
43
        token := &Token{
 
44
                Expiry: time.Now().Add(-5 * time.Hour),
 
45
        }
 
46
        if token.Valid() {
 
47
                t.Errorf("Token should not be valid if it expired in the past")
 
48
        }
 
49
}
 
50
 
 
51
func newMockServer(handler func(w http.ResponseWriter, r *http.Request)) *httptest.Server {
 
52
        return httptest.NewServer(http.HandlerFunc(handler))
 
53
}