forked from Mastercard/oauth1-signer-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signer_test.go
executable file
·82 lines (71 loc) · 2.4 KB
/
signer_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
package oauth_test
import (
"bytes"
"encoding/json"
oauth "github.com/mastercard/oauth1-signer-go"
"github.com/mastercard/oauth1-signer-go/utils"
"net/http"
"testing"
)
const (
consumerKey = "WFQHgP6gI01ZxbpqUzdWQ_GpDVrym3dhY6Q9X3PZe4ba3850!3b9f3d6593d04a0cbefadaf8bb3975fb0000000000000000"
)
var (
signingKey, _ = utils.LoadSigningKey("testdata/test_key_container.p12", "Password1")
jsonData = map[string]string{"foo": "bår"}
jsonValue, _ = json.Marshal(jsonData)
request, _ = http.NewRequest("POST", "https://sandbox.api.mastercard.com/service", bytes.NewBuffer(jsonValue))
)
func TestHttpRequestSigning(t *testing.T) {
signer := &oauth.Signer{
ConsumerKey: consumerKey,
SigningKey: signingKey,
}
err := signer.Sign(request)
if err != nil {
t.Errorf("Expected to sign the http request, got %v", err)
}
authorizationVal := request.Header.Get(oauth.AuthorizationHeaderName)
if len(authorizationVal) == 0 {
t.Errorf("Expected the authorization header, got %v", authorizationVal)
}
}
func TestHttpRequestSigningWithInvalidInput(t *testing.T) {
// sign with nil consumer key and nil signing key
signer := &oauth.Signer{}
err := signer.Sign(request)
if err == nil {
t.Errorf("Expected to thrown an error in case of invalid signing data")
}
// sign with nil signing key
signer = &oauth.Signer{ConsumerKey: consumerKey}
err = signer.Sign(request)
if err == nil {
t.Errorf("Expected to thrown an error in case of invalid signing data")
}
// sign with nil http.Request
signer = &oauth.Signer{ConsumerKey: consumerKey, SigningKey: signingKey}
err = signer.Sign(nil)
if err == nil {
t.Errorf("Expected to thrown an error in case of Nil request")
}
// sign for http GET request
getRequest, _ := http.NewRequest("GET", "https://sandbox.api.mastercard.com/service", nil)
err = signer.Sign(getRequest)
if err != nil {
t.Errorf("Expected to sign http GET request, but got %v", err)
}
authorizationVal := getRequest.Header.Get(oauth.AuthorizationHeaderName)
if len(authorizationVal) == 0 {
t.Errorf("Expected the authorization header, got %v", authorizationVal)
}
// sign for http POST request
err = signer.Sign(request)
if err != nil {
t.Errorf("Expected to sign http POST request, but got %v", err)
}
authorizationVal = request.Header.Get(oauth.AuthorizationHeaderName)
if len(authorizationVal) == 0 {
t.Errorf("Expected the authorization header, got %v", authorizationVal)
}
}