-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmock_test.go
49 lines (40 loc) · 1008 Bytes
/
mock_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
package authy_test
import (
"github.com/gorilla/mux"
"net/http"
"net/http/httptest"
"net/url"
)
// a fake session object
type FakeSession struct {
items map[interface{}]interface{}
}
func (f *FakeSession) Get(key interface{}) interface{} {
return f.items[key]
}
func (f *FakeSession) Set(key interface{}, val interface{}) {
f.items[key] = val
}
func (f *FakeSession) Delete(key interface{}) {
delete(f.items, key)
}
// generate a fake http request
func FakeHttpRequest(requestUrl string) *http.Request {
parsedUrl, _ := url.Parse(requestUrl)
return &http.Request{
URL: parsedUrl,
}
}
// fake oauth2 service
func FakeOAuthServer() (s *httptest.Server) {
r := mux.NewRouter()
r.HandleFunc("/oauth2", func(rw http.ResponseWriter, r *http.Request) {
values := url.Values{}
values.Set("access_token", "fakeaccesstoken")
values.Set("scope", r.URL.Query().Get("scope"))
values.Set("token_type", "example")
rw.Write([]byte(values.Encode()))
})
s = httptest.NewServer(r)
return
}