@@ -5,23 +5,24 @@ import (
5
5
"fmt"
6
6
"net/http"
7
7
"net/http/httptest"
8
+ "strings"
8
9
"testing"
9
10
)
10
11
11
12
// Test base verification function works.
12
- func TestVerifyRequest (t * testing.T ) {
13
+ func TestIsValidSignatureSuccess (t * testing.T ) {
13
14
// Setup a simple body with a matching HMAC.
14
15
body := []byte (`{"key":"value"}` )
15
16
hmac := "7iASoA8WSbw19M/h+lgrLr2ly/LvgnE9bcLsk9gflvs="
16
17
17
18
// Create a signature
18
19
lhmac := newSignature ("secret" , body )
19
20
if ok := isValidSignature (lhmac , hmac ); ! ok {
20
- t .Errorf ("expected request data to verify" )
21
+ t .Error ("expected request data to verify" )
21
22
}
22
23
}
23
24
24
- func TestVerifyRequestError (t * testing.T ) {
25
+ func TestIsValidSignatureFailure (t * testing.T ) {
25
26
// Setup a simple body with a matching HMAC, but missing shop.
26
27
body := []byte (`{"key":"value"}` )
27
28
hmac := "ee2012a00f1649bc35f4cfe1fa582b2ebda5cbf2ef82713d6dc2ec93d81f96fb"
@@ -38,7 +39,7 @@ func TestVerifyRequestError(t *testing.T) {
38
39
// Create a signature
39
40
lhmac = newSignature ("secret" , body )
40
41
if ok := isValidSignature (lhmac , hmac ); ok {
41
- t .Errorf ("expected request data to not verify, but it did" )
42
+ t .Error ("expected request data to not verify, but it did" )
42
43
}
43
44
}
44
45
@@ -53,11 +54,11 @@ func TestNetHttpSuccess(t *testing.T) {
53
54
// Setup the server with our data.
54
55
rec , ran := setupServer (key , shop , hmac , body )
55
56
if c := rec .Code ; c != http .StatusOK {
56
- t .Errorf ("expected status code %v got %v" , http .StatusOK , c )
57
+ t .Errorf ("expected status code %d got %v" , http .StatusOK , c )
57
58
}
58
59
59
60
if ! ran {
60
- t .Errorf ("expected next handler to run but did not" )
61
+ t .Error ("expected next handler to run but did not" )
61
62
}
62
63
}
63
64
@@ -72,11 +73,55 @@ func TestNetHttpFailure(t *testing.T) {
72
73
// Setup the server with our data.
73
74
rec , ran := setupServer (key , shop , hmac , body )
74
75
if c := rec .Code ; c != http .StatusBadRequest {
75
- t .Errorf ("expected status code %v got %v" , http .StatusBadRequest , c )
76
+ t .Errorf ("expected status code %d got %v" , http .StatusBadRequest , c )
77
+ }
78
+
79
+ if ran == true {
80
+ t .Error ("expected next handler to not run but it did" )
81
+ }
82
+ }
83
+
84
+ // Test for missing HMAC header from request.
85
+ func TestMissingHeaderHMAC (t * testing.T ) {
86
+ // Set our data.
87
+ key := "secret"
88
+ body := `{"key":"value"}`
89
+ shop := "example.myshopify.com"
90
+
91
+ // Setup the server with our data. No shop.
92
+ rec , ran := setupServer (key , shop , "" , body )
93
+ if c := rec .Code ; c != http .StatusBadRequest {
94
+ t .Errorf ("expected status code %d got %v" , http .StatusBadRequest , c )
95
+ }
96
+
97
+ if b := rec .Body ; ! strings .Contains (b .String (), errMissingSignature ) {
98
+ t .Errorf ("expected '%s' body got '%v'" , errMissingSignature , b )
99
+ }
100
+
101
+ if ran == true {
102
+ t .Error ("expected next handler to not run but it did" )
103
+ }
104
+ }
105
+
106
+ // Test for missing shop header from request.
107
+ func TestMissingHeaderShop (t * testing.T ) {
108
+ // Set our data.
109
+ key := "secret"
110
+ body := `{"key":"value"}`
111
+ hmac := "ee2012a00f1649bc35f"
112
+
113
+ // Setup the server with our data. No shop.
114
+ rec , ran := setupServer (key , "" , hmac , body )
115
+ if c := rec .Code ; c != http .StatusBadRequest {
116
+ t .Errorf ("expected status code %d got %v" , http .StatusBadRequest , c )
117
+ }
118
+
119
+ if b := rec .Body ; ! strings .Contains (b .String (), errMissingShop ) {
120
+ t .Errorf ("expected '%s' body got '%v'" , errMissingShop , b )
76
121
}
77
122
78
123
if ran == true {
79
- t .Errorf ("expected next handler to not run but it did" )
124
+ t .Error ("expected next handler to not run but it did" )
80
125
}
81
126
}
82
127
0 commit comments