-
Notifications
You must be signed in to change notification settings - Fork 40
/
fake_getter_test.go
102 lines (80 loc) · 2.71 KB
/
fake_getter_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package openid
import (
"bufio"
"bytes"
"errors"
"net/http"
"net/url"
)
type fakeGetter struct {
urls map[string]string
redirects map[string]string
}
var testGetter = &fakeGetter{
make(map[string]string), make(map[string]string)}
var testInstance = &OpenID{urlGetter: testGetter}
func (f *fakeGetter) Get(uri string, headers map[string]string) (resp *http.Response, err error) {
key := uri
for k, v := range headers {
key += "#" + k + "#" + v
}
if doc, ok := f.urls[key]; ok {
request, err := http.NewRequest("GET", uri, nil)
if err != nil {
return nil, err
}
return http.ReadResponse(bufio.NewReader(
bytes.NewBuffer([]byte(doc))), request)
}
if uri, ok := f.redirects[key]; ok {
return f.Get(uri, headers)
}
return nil, errors.New("404 not found")
}
func (f *fakeGetter) Post(uri string, form url.Values) (resp *http.Response, err error) {
return f.Get("POST@"+uri, nil)
}
func init() {
// Prepare (http#header#header-val --> http response) pairs.
// === For Yadis discovery ==================================
// Directly reffers a valid XRDS document
testGetter.urls["http://example.com/xrds#Accept#application/xrds+xml"] = `HTTP/1.0 200 OK
Content-Type: application/xrds+xml; charset=UTF-8
<?xml version="1.0" encoding="UTF-8"?>
<xrds:XRDS xmlns:xrds="xri://$xrds" xmlns="xri://$xrd*($v*2.0)"
xmlns:openid="http://openid.net/xmlns/1.0">
<XRD>
<Service xmlns="xri://$xrd*($v*2.0)">
<Type>http://specs.openid.net/auth/2.0/signon</Type>
<URI>foo</URI>
<LocalID>bar</LocalID>
</Service>
</XRD>
</xrds:XRDS>`
// Uses a X-XRDS-Location header to redirect to the valid XRDS document.
testGetter.urls["http://example.com/xrds-loc#Accept#application/xrds+xml"] = `HTTP/1.0 200 OK
X-XRDS-Location: http://example.com/xrds
nothing interesting here`
// Html document, with meta tag X-XRDS-Location. Points to the
// previous valid XRDS document.
testGetter.urls["http://example.com/xrds-meta#Accept#application/xrds+xml"] = `HTTP/1.0 200 OK
Content-Type: text/html
<html>
<head>
<meta http-equiv="X-XRDS-Location" content="http://example.com/xrds">`
// === For HTML discovery ===================================
testGetter.urls["http://example.com/html"] = `HTTP/1.0 200 OK
<html>
<head>
<link rel="openid2.provider" href="example.com/openid">
<link rel="openid2.local_id" href="bar-name">`
testGetter.redirects["http://example.com/html-redirect"] = "http://example.com/html"
testGetter.urls["http://example.com/html-multi-rel"] = `HTTP/1.0 200 OK
Content-Type: text/html
<html>
<head>
<link rel="openid2.provider openid.server"
href="http://www.livejournal.com/openid/server.bml">
<link rel="openid2.local_id openid.delegate"
href="http://exampleuser.livejournal.com/">`
}