forked from intngbl/oauthv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
70 lines (54 loc) · 1.39 KB
/
main_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
package oauthv
import (
"github.com/garyburd/go-oauth/oauth"
"net/http"
"net/url"
"strings"
"testing"
)
const (
testURL = `http://localhost:8080`
)
const (
testAppSecret = `ConsumerSecret`
testAppToken = `ConsumerKey`
testClientSecret = `ClientSecret`
testClientToken = `ClientToken`
)
var testClient = oauth.Client{
Credentials: oauth.Credentials{
Token: testAppToken,
Secret: testAppSecret,
},
}
var testRequest = oauth.Credentials{
Token: testClientToken,
Secret: testClientSecret,
}
func TestSampleVerifyHeader(t *testing.T) {
form := url.Values{"maxResults": {"100"}}
// The last element of path contains a "/".
path := "/document/encoding%2gizp"
// Create the request with the temporary path "/".
req, err := http.NewRequest("GET", "http://api.example.com/", strings.NewReader(form.Encode()))
if err != nil {
t.Fatal(err)
}
// Overwrite the temporary path with the actual request path.
req.URL.Opaque = path
// Sign the request.
header := testClient.AuthorizationHeader(&testRequest, "GET", req.URL, form)
var auth *Authorization
var valid bool
if auth, err = Parse(header); err != nil {
t.Fatal(err)
}
auth.SetClientSecret(testClient.Credentials.Secret)
auth.SetRequestSecret(testRequest.Secret)
if valid, err = auth.ValidateRequest("GET", req.URL, form); err != nil {
t.Fatal(err)
}
if !valid {
t.Fatal(`Expecting request to be valid.`)
}
}