summaryrefslogtreecommitdiff
path: root/src/oauth/auth_header_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/oauth/auth_header_test.go')
-rw-r--r--src/oauth/auth_header_test.go186
1 files changed, 186 insertions, 0 deletions
diff --git a/src/oauth/auth_header_test.go b/src/oauth/auth_header_test.go
new file mode 100644
index 0000000..332b3de
--- /dev/null
+++ b/src/oauth/auth_header_test.go
@@ -0,0 +1,186 @@
+package oauth
+
+import (
+ "net/http"
+ "strings"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestString_CardmarketDemoValues_Success(t *testing.T) {
+ t.Parallel()
+
+ // Arrange
+ method := http.MethodGet
+ baseURL := "https://apiv2.cardmarket.com/ws/v2.0/account"
+ consumerToken := "bfaD9xOU0SXBhtBP"
+ consumerSecret := "pChvrpp6AEOEwxBIIUBOvWcRG3X9xL4Y"
+ accessToken := "lBY1xptUJ7ZJSK01x4fNwzw8kAe5b10Q"
+ accessSecret := "hc1wJAOX02pGGJK2uAv1ZOiwS7I9Tpoe"
+ nonce := "53eb1f44909d6"
+ timestamp := "1407917892"
+
+ ah := authHeader{
+ method: method,
+ baseURL: baseURL,
+ queryParams: nil,
+ tokens: Tokens{
+ Consumer: ConsumerToken{
+ Token: consumerToken,
+ Secret: consumerSecret,
+ },
+ Access: AccessToken{
+ Token: accessToken,
+ Secret: accessSecret,
+ },
+ },
+ nonce: nonce,
+ timestamp: timestamp,
+ signatureMethod: "HMAC-SHA1",
+ version: "1.0",
+ }
+
+ // Act
+ actual := ah.String()
+
+ // Assert
+ assert.True(t, strings.HasPrefix(actual, "OAuth "))
+
+ actualParts := strings.Split(strings.TrimPrefix(actual, "OAuth "), ", ")
+ actualKVs := map[string]string{}
+ for _, ap := range actualParts {
+ kv := strings.Split(ap, "=")
+ actualKVs[kv[0]] = kv[1]
+ }
+
+ expected := map[string]string{
+ "realm": `"https://apiv2.cardmarket.com/ws/v2.0/account"`,
+ "oauth_consumer_key": `"bfaD9xOU0SXBhtBP"`,
+ "oauth_token": `"lBY1xptUJ7ZJSK01x4fNwzw8kAe5b10Q"`,
+ "oauth_nonce": `"53eb1f44909d6"`,
+ "oauth_timestamp": `"1407917892"`,
+ "oauth_signature_method": `"HMAC-SHA1"`,
+ "oauth_version": `"1.0"`,
+ "oauth_signature": `"mRdSwq4vJcmCByd990zE1NTPWJ0%3D"`,
+ }
+ for expectedKey, expectedValue := range expected {
+ assert.Equal(t, expectedValue, actualKVs[expectedKey])
+ }
+}
+
+func TestBuildSignature(t *testing.T) {
+ t.Skip()
+}
+
+func TestBuildBaseString(t *testing.T) {
+ t.Parallel()
+
+ // Arrange
+ method := http.MethodGet
+ baseURL := "https://apiv2.cardmarket.com/ws/v2.0/account"
+ consumerToken := "bfaD9xOU0SXBhtBP"
+ consumerSecret := "pChvrpp6AEOEwxBIIUBOvWcRG3X9xL4Y"
+ accessToken := "lBY1xptUJ7ZJSK01x4fNwzw8kAe5b10Q"
+ accessSecret := "hc1wJAOX02pGGJK2uAv1ZOiwS7I9Tpoe"
+ nonce := "53eb1f44909d6"
+ timestamp := "1407917892"
+
+ ah := authHeader{
+ method: method,
+ baseURL: baseURL,
+ queryParams: nil,
+ tokens: Tokens{
+ Consumer: ConsumerToken{
+ Token: consumerToken,
+ Secret: consumerSecret,
+ },
+ Access: AccessToken{
+ Token: accessToken,
+ Secret: accessSecret,
+ },
+ },
+ nonce: nonce,
+ timestamp: timestamp,
+ signatureMethod: "HMAC-SHA1",
+ version: "1.0",
+ }
+
+ // Act
+ actual := ah.buildBaseString()
+
+ // Assert
+ expected := `GET&https%3A%2F%2Fapiv2.cardmarket.com%2Fws%2Fv2.0%2Faccount&`
+ assert.Equal(t, expected, actual)
+}
+
+func TestBuildParameterString(t *testing.T) {
+ t.Parallel()
+
+ // Arrange
+ method := http.MethodGet
+ baseURL := "https://apiv2.cardmarket.com/ws/v2.0/account"
+ consumerToken := "bfaD9xOU0SXBhtBP"
+ consumerSecret := "pChvrpp6AEOEwxBIIUBOvWcRG3X9xL4Y"
+ accessToken := "lBY1xptUJ7ZJSK01x4fNwzw8kAe5b10Q"
+ accessSecret := "hc1wJAOX02pGGJK2uAv1ZOiwS7I9Tpoe"
+ nonce := "53eb1f44909d6"
+ timestamp := "1407917892"
+
+ ah := authHeader{
+ method: method,
+ baseURL: baseURL,
+ queryParams: nil,
+ tokens: Tokens{
+ Consumer: ConsumerToken{
+ Token: consumerToken,
+ Secret: consumerSecret,
+ },
+ Access: AccessToken{
+ Token: accessToken,
+ Secret: accessSecret,
+ },
+ },
+ nonce: nonce,
+ timestamp: timestamp,
+ signatureMethod: "HMAC-SHA1",
+ version: "1.0",
+ }
+
+ // Act
+ actual := ah.buildParameterString()
+
+ // Assert
+ expected := `oauth_consumer_key%3DbfaD9xOU0SXBhtBP%26oauth_nonce%3D53eb1f44909d6%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1407917892%26oauth_token%3DlBY1xptUJ7ZJSK01x4fNwzw8kAe5b10Q%26oauth_version%3D1.0`
+ assert.Equal(t, expected, actual)
+}
+
+func TestPercentEncode(t *testing.T) {
+ t.Parallel()
+
+ tcs := []struct {
+ name string
+ str string
+ expected string
+ }{
+ {
+ name: "space",
+ str: " ",
+ expected: "%20",
+ },
+ {
+ name: "forward-slash",
+ str: "/",
+ expected: "%2F",
+ },
+ }
+ for _, tc := range tcs {
+ t.Run(tc.name, func(t *testing.T) {
+ // Act
+ actual := percentEncode(tc.str)
+
+ // Assert
+ assert.Equal(t, tc.expected, actual)
+ })
+ }
+}