-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathoauth_test.go
59 lines (48 loc) · 1.52 KB
/
oauth_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package auth
import (
"encoding/json"
"net/http"
"net/http/httptest"
"time"
"github.com/gin-gonic/gin"
"github.com/mitre/heart"
"github.com/pebbe/util"
. "gopkg.in/check.v1"
)
type OAuthSuite struct {
}
var _ = Suite(&OAuthSuite{})
func (o *OAuthSuite) TestIntrospection(c *C) {
server := httptest.NewServer(mockIntrospectionEndpoint(c))
defer server.Close()
rr := oauthRequest("my_client_id", "sekret", server.URL, c)
c.Assert(rr.Code, Equals, http.StatusOK)
}
func mockIntrospectionEndpoint(c *C) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c.Assert(r.Method, Equals, "POST")
clientID := r.FormValue("client_id")
c.Assert(clientID, Equals, "my_client_id")
token := r.FormValue("token")
c.Assert(token, Equals, "foo")
ir := &heart.IntrospectionResponse{Active: true, Scope: "foo bar", EXP: time.Now().Unix(), SUB: "steve", ClientID: "heart-watch"}
encoder := json.NewEncoder(w)
encoder.Encode(ir)
})
}
func oauthRequest(clientID, clientSecret, endpoint string, c *C) *httptest.ResponseRecorder {
r, err := http.NewRequest("GET", "/", nil)
util.CheckErr(err)
r.Header.Add("Content-Type", "application/json")
r.Header.Add("Authorization", "Bearer foo")
e := gin.New()
rw := httptest.NewRecorder()
noop := func(ctx *gin.Context) {
scopes, _ := ctx.Get("scopes")
c.Assert("bar", Equals, scopes.([]string)[1])
ctx.String(http.StatusOK, "Hello")
}
e.GET("/", OAuthIntrospectionHandler(clientID, clientSecret, endpoint), noop)
e.ServeHTTP(rw, r)
return rw
}